0% found this document useful (0 votes)
59 views3 pages

MultiThreaded Echo Server in Java

This document describes a multi-threaded echo server created using Swing and listeners in Java. The echo server creates a server socket to listen for client connections. When a new client connects, a new thread is started to handle communication with that client. The client thread receives messages from the client using a data input stream and sends the messages back to the client using a data output stream. All communication is displayed in a text area window.

Uploaded by

Chatterjee Bubun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

MultiThreaded Echo Server in Java

This document describes a multi-threaded echo server created using Swing and listeners in Java. The echo server creates a server socket to listen for client connections. When a new client connects, a new thread is started to handle communication with that client. The client thread receives messages from the client using a data input stream and sends the messages back to the client using a data output stream. All communication is displayed in a text area window.

Uploaded by

Chatterjee Bubun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[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]();

You might also like