Write a Multithread Echo Client Server Program
PROGRAM TO DEMONSTRATE MULTITHREADING ECHO SERVER
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiEchoServer
{
private static ServerSocket servsoc;
private static final int PORT = 1234;
public static void main(String[] args) throws IOException
{
try
{
servsoc = new ServerSocket(PORT);
}
catch (IOException ioEx)
{
System.out.println("\nUnable to set up port!");
System.exit(1);
}
System.out.println("\nWaiting for Client.\n");
do
{
//Wait for client…
Socket client = servsoc.accept();
System.out.println("\nNew client accepted.\n");
//Create a thread to handle communication with this client and pass the constructor for //this thread a reference to the relevant socket…
ClientHandler handler = new ClientHandler(client);
handler.start();
}while (true);
}
}
class ClientHandler extends Thread
{
private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket soc)
{
//Set up reference to associated socket…
client = soc;
try
{
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(),true);
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
public void run()
{
String received;
do
{
//Accept message from client on the socket's input stream…
received = input.nextLine();
//Echo message back to client on the socket's output stream…
output.println("ECHO: " + received);
//Repeat above until 'QUIT' sent by client…
}while (!received.equals("QUIT"));
try
{
if (client!=null)
{
System.out.println("client disconnected");
client.close();
}
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
}
}
}
OUTPUT OF SERVER :
PROGRAM TO DEMONSTRATE MULTITHREADING ECHO CLIENT
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiEchoClient
{
private static InetAddress host;
private static final int PORT = 1234;
public static void main(String[] args)
{
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
System.exit(1);
}
sendMessages();
}
private static void sendMessages()
{
Socket soc = null;
try
{
soc = new Socket(host,PORT);
Scanner networkInput =
new Scanner(soc.getInputStream());
PrintWriter networkOutput = new PrintWriter(soc.getOutputStream(),true);
//Set up stream for keyboard entry…
Scanner userEntry = new Scanner(System.in);
String message, response;
do
{
System.out.print(
"Enter message ('QUIT' to exit): ");
message = userEntry.nextLine();
//Send message to server on the socket's output stream…
//Accept response from server on the socket's intput stream…
networkOutput.println(message);
response = networkInput.nextLine();
//Display server's response to user…
System.out.println("\nSERVER> " + response);
}while (!message.equals("QUIT"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println("\nClosing connection");
soc.close();
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
OUTPUT OF SERVER :
OUTPUT OF CLIENT :
Page | 39