[Link] a MultiThreaded Echo Server using Swings and use appropriate Listener.
import [Link].*;
import [Link].*;
import [Link].*;
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);
[Link]("Server started at " + new [Link]() + '\n');
// Create a socket for each connection and start a new thread
while (true) {
Socket socket = [Link]();
[Link]("New client accepted at " + new [Link]() + '\n');
ClientThread thread = new ClientThread(socket);
[Link]();
}
}
catch(IOException ex) {
[Link]();
public static void main(String[] args) {
EchoServer server = new EchoServer();
[Link](JFrame.EXIT_ON_CLOSE);
[Link]();
// Inner class for the client thread
class ClientThread extends Thread {
private Socket socket;
private DataInputStream inputFromClient;
private DataOutputStream outputToClient;
public ClientThread(Socket socket) {
[Link] = socket;
try {
// Create data input and output streams
inputFromClient = new DataInputStream(
[Link]());
outputToClient = new DataOutputStream(
[Link]());
catch(IOException ex) {
[Link]();
}
public void run() {
try {
while (true) {
// Receive message from the client
String message = [Link]();
// Send the message back to the client
[Link](message);
// Display to the text area
[Link](message + '\n');
catch(IOException ex) {
[Link]();