In: Computer Science
In the provided client and server code, the server can serve to single client at a time. You have to change server.java code so that it can connect and serve multiple clients at the same time. Use multithreading. =============================================================================== import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { String serverHostname = new String ("127.0.0.1"); if (args.length > 0) { //pass the hsotname through cmd argument serverHostname = args[0]; } System.out.println ("Attemping to connect to host " + serverHostname + " on port 10007."); Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { //Connect to server and open IO stream echoSocket = new Socket(serverHostname, 10007); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + serverHostname); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; System.out.print ("input-comand: "); while ((userInput = stdIn.readLine()) != null) { //send to server using socket out.println(userInput); //read reply from server String replyFromServer = in.readLine(); System.out.println("Answer: " + replyFromServer); if(replyFromServer.trim().toLowerCase().equals("bye")){ break; } System.out.print ("input-command: "); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } }
//=================================================
import java.net.*; import java.io.*; import java.util.Scanner; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(10007); } catch (IOException e) { System.err.println("Could not listen on port: 10007."); System.exit(1); } Socket clientSocket = null; System.out.println ("Waiting for connection ..."); try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } System.out.println ("Connection successful"); System.out.println ("Waiting for input ..."); //open IO streams PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); String inputLine; //wait and read input from client. Exit when input from socket is Bye Scanner scanFile = new Scanner(new File("DNS-table.txt")); boolean found =false; while ((inputLine = in.readLine()) != null) { System.out.println ("Server: " + inputLine); String line =""; if (inputLine.trim().toLowerCase().equals("bye")) { out.println(inputLine); break; } while (scanFile.hasNextLine()) { //reply back to client line = scanFile.nextLine(); String[] tokens = line.split(" "); if(inputLine.equalsIgnoreCase(tokens[0]) || inputLine.equalsIgnoreCase(tokens[1])) { found = true; break; } else found = false; } if(found) { out.println(line); } else { out.println("Not found..."); } } //close IO streams and at the end socket scanFile.close(); out.close(); in.close(); clientSocket.close(); serverSocket.close(); } }
-------------------------------------------------------------
text
www.google.com 192.168.73.1
www.uncc.edu 192.168.73.2
www.abc.com 192.168.73.3
www.espn.com 192.168.73.4
www.cyberDNA.com 192.168.73.55
www.ccaa.uncc.edu 192.168.73.5
www.ccigrad.uncc 192.168.73.13
www.cis.uncc.edu 192.168.73.14
www.unc.edu 192.168.73.12
import java.net.*; import java.io.*; import java.util.Scanner; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(10007); } catch (IOException e) { System.err.println("Could not listen on port: 10007."); System.exit(1); } while (true) { Socket clientSocket = null; System.out.println("Waiting for connection ..."); try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } new ConnectionHandler(clientSocket).start(); } } static class ConnectionHandler extends Thread { private Socket clientSocket; public ConnectionHandler(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { System.out.println("Connection successful"); System.out.println("Waiting for input ..."); // open IO streams try { PrintWriter out; out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; // wait and read input from client. Exit when input from socket is Bye Scanner scanFile = new Scanner(new File("DNS-table.txt")); boolean found = false; while ((inputLine = in.readLine()) != null) { System.out.println("Server: " + inputLine); String line = ""; if (inputLine.trim().toLowerCase().equals("bye")) { out.println(inputLine); break; } while (scanFile.hasNextLine()) { // reply back to client line = scanFile.nextLine(); String[] tokens = line.split(" "); if (inputLine.equalsIgnoreCase(tokens[0]) || inputLine.equalsIgnoreCase(tokens[1])) { found = true; break; } else found = false; } if (found) { out.println(line); } else { out.println("Not found..."); } } // close IO streams and at the end socket scanFile.close(); out.close(); in.close(); clientSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.