Question: Java ServerClient Help: My current assignment; Write a Java TCP Stream server and client. The client should allow the user to connect and type strings

Java Server\Client Help: My current assignment; Write a Java TCP Stream server and client. The client should allow the user to connect and type strings that get sent to the server, which it then echoes back to the specific client that sent it. Write the server to that it is possible to connect 5 different clients to it and it echos back the string that a particular client sent to it back to that specific client. Write code in the clients such that if the server is busy the client catches an exception and tries again.

I still cant limit the users to 5 even though i set the ServerSocket to a backlog of 5. Additionally, I need help getting the client to wait for a connection if the server is busy.

// MultiEchoServer.java

import java.net.*; import java.io.*; public class MultiEchoServer { public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage: java MultiEchoServer "); System.exit(1); } int portNumber = Integer.parseInt(args[0]); try (ServerSocket serverSocket = new ServerSocket(portNumber,5)) { while (true) { new MultiEchoServerThread(serverSocket.accept()).start(); System.out.println("Connected: " + serverSocket.getLocalSocketAddress()); } } catch (IOException e) { System.err.println("Server Exception on port: " + portNumber); System.exit(-1); } } }

// MultiEchoServerThread.java

import java.net.*; import java.io.*; public class MultiEchoServerThread extends Thread { private Socket socket = null; public MultiEchoServerThread(Socket socket) { super("MultiEchoServerThread"); this.socket = socket; } public void run() { try ( PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())) ) { String inputLine, outputLine; outputLine = "Welcome, Use keyword 'EXIT' to disconnect"; out.println(outputLine); while ((inputLine = in.readLine()) != null) { outputLine = inputLine; out.println(outputLine); if (outputLine.equalsIgnoreCase("exit")) System.out.println("Disconnected (EXIT): " + socket); break; } socket.close(); } catch (SocketException e) { System.err.println("User Interrupt: " + socket); } catch (IOException e) { e.printStackTrace(); } } }

// MultiEchoClient.java

import java.io.*; import java.net.*; public class MultiEchoClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient  "); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket clientSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)) ) { String fromServer; String fromUser; while ((fromServer = in.readLine()) != null) { System.out.println("Server(Echo): " + fromServer); if (fromServer.equalsIgnoreCase("exit")) break; fromUser = stdIn.readLine(); if (fromUser != null) { out.println(fromUser); } } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); 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!