10.Develop a MultiThreaded Echo Server using Swings and use appropriate Listener.
import java.io.*;
import java.net.*;
import javax.swing.*;
public class EchoServer extends JFrame {
private JTextArea textArea;
public EchoServer() {
super("Echo Server");
textArea = new JTextArea();
add(new JScrollPane(textArea));
setSize(400, 300);
setVisible(true);
public void startServer() {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
textArea.append("Server started at " + new java.util.Date() + '\n');
// Create a socket for each connection and start a new thread
while (true) {
Socket socket = serverSocket.accept();
textArea.append("New client accepted at " + new java.util.Date() + '\n');
ClientThread thread = new ClientThread(socket);
thread.start();
}
}
catch(IOException ex) {
ex.printStackTrace();
public static void main(String[] args) {
EchoServer server = new EchoServer();
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.startServer();
// Inner class for the client thread
class ClientThread extends Thread {
private Socket socket;
private DataInputStream inputFromClient;
private DataOutputStream outputToClient;
public ClientThread(Socket socket) {
this.socket = socket;
try {
// Create data input and output streams
inputFromClient = new DataInputStream(
socket.getInputStream());
outputToClient = new DataOutputStream(
socket.getOutputStream());
catch(IOException ex) {
ex.printStackTrace();
}
public void run() {
try {
while (true) {
// Receive message from the client
String message = inputFromClient.readUTF();
// Send the message back to the client
outputToClient.writeUTF(message);
// Display to the text area
textArea.append(message + '\n');
catch(IOException ex) {
ex.printStackTrace();