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

Java Vlab Assignment10

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)
52 views3 pages

Java Vlab Assignment10

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
You are on page 1/ 3

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

You might also like