Question: Modify the EchoServer.java code to enable the server to serve each connection in a separate thread. That is, make the server multi-threaded. Allow the multi-threaded

Modify the EchoServer.java code to enable the server to serve each connection in a separate thread. That is, make the server multi-threaded.

Allow the multi-threaded server to stop accepting new requests when any of the active clients sends the message stop. In addition, the server should terminate when all clients have exited by waiting for the threads to finish.

import java.net.*;
import java.io.*;
public class EchoServer2b extends Thread
{
protected static boolean serverContinue = true;
protected Socket clientSocket;
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket.accept();
System.out.println ("Connection Socket Created");
try {
while (serverContinue)
{
System.out.println ("Waiting for Connection");
new EchoServer2b (serverSocket.accept());
}
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
}
catch (IOException e)
{
System.err.println("Could not listen on port: 10008.");
System.exit(1);
}
finally
{
try {
serverSocket.close();
}
catch (IOException e)
{
System.err.println("Could not close port: 10008.");
System.exit(1);
}
}
}
private EchoServer2b (Socket clientSoc)
{
clientSocket = clientSoc;
start();
}
public void run()
{
System.out.println ("New Communication Thread Started");
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
if (inputLine.equals("End Server."))
serverContinue = false;
}
out.close();
in.close();
clientSocket.close();
}
catch (IOException e)
{
System.err.println("Problem with Communication Server");
System.exit(1);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!