Question: import java.io.*; import java.net.*; import java.util.Arrays; public class SThread extends Thread { private Object[][] RTable; // routing table private PrintWriter out, outTo; // writers (for
import java.io.*; import java.net.*; import java.util.Arrays; public class SThread extends Thread { private Object[][] RTable; // routing table private PrintWriter out, outTo; // writers (for writing back to the machine and to destination) private BufferedReader in; // reader (for reading from the machine connected to) private String inputLine, outputLine, destination, addr; // communication strings private Socket outSocket; // socket for communicating with a destination private int ind; // indext in the routing table // Constructor SThread(Object[][] Table, Socket toClient, int index) throws IOException { out = new PrintWriter(toClient.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(toClient.getInputStream())); RTable = Table; addr = toClient.getInetAddress().getHostAddress(); RTable[index][0] = addr+":"+Integer.toString(toClient.getPort()); // IP addresses RTable[index][1] = toClient; // sockets for communication ind = index; for (Object[] row : RTable) { if (row[0] == null) continue; String a = (String) row[0]; Socket s = (Socket) row[1]; String ip = s.getInetAddress().getHostAddress(); String port = Integer.toString(s.getPort()); System.out.println(String.format("%10s : %10s %10s", a, ip, port)); } } // Run method (will run for each machine that connects to the ServerRouter) public void run() { try { // Initial sends/receives destination = in.readLine(); // initial read (the destination for writing) System.out.println("Forwarding to " + destination); out.println("Connected to the router."); // confirmation of connection // waits 10 seconds to let the routing table fill with all machines' information try { Thread.currentThread().sleep(10000); System.out.println("Waiting"); this.sleep(10000); System.out.println("Finished"); } catch (InterruptedException ie) { System.out.println("Thread interrupted"); } // loops through the routing table to find the destination for (int i = 0; i < 10; i++) { if (destination.equals(RTable[i][0])) { outSocket = (Socket) RTable[i][1]; // gets the socket for communication from the table System.out.println("Found destination: " + destination); outTo = new PrintWriter(outSocket.getOutputStream(), true); // assigns a writer } } // Communication loop while ((inputLine = in.readLine()) != null) { System.out.println("Client/Server said: " + inputLine); if (inputLine.equals("Bye.")) // exit statement break; outputLine = inputLine; // passes the input from the machine to the output string for the destination if (outSocket != null) { outTo.println(outputLine); // writes to the destination } }// end while }// end try catch (IOException e) { System.err.println("Could not listen to socket."); System.exit(1); } } }
import java.io.*; import java.net.*; import java.util.ArrayList; public class TCPServer { public static void main(String[] args) throws IOException { // Variables for setting up connection and communication Socket Socket = null; // socket to connect with ServerRouter PrintWriter out = null; // for writing to ServerRouter BufferedReader in = null; // for reading form ServerRouter InetAddress addr = InetAddress.getLocalHost(); String host = addr.getHostAddress(); // Server machine's IP String routerName = "macs-air.lan"; // ServerRouter host name int SockNum = 5555; // port number // Tries to connect to the ServerRouter try { Socket = new Socket(routerName, SockNum); out = new PrintWriter(Socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(Socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about router: " + routerName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: " + routerName); System.exit(1); } // Variables for message passing String fromServer; // messages sent to ServerRouter String fromClient; // messages received from ServerRouter String address ="10.5.3.196"; // destination IP (Client) // Communication process (initial sends/receives) out.println(address);// initial send (IP of the destination Client) fromClient = in.readLine();// initial receive from router (verification of connection) System.out.println("ServerRouter: " + fromClient); // Communication while loop while ((fromClient = in.readLine()) != null) { System.out.println("Client said: " + fromClient); if (fromClient.equals("Bye.")) // exit statement break; fromServer = fromClient.toUpperCase(); // converting received message to upper case System.out.println("Server said: " + fromServer); out.println(fromServer); // sending the converted message back to the Client via ServerRouter } // closing connections out.close(); in.close(); Socket.close(); } }
import java.io.*; import java.net.*; public class TCPClient { public static void main(String[] args) throws IOException { // Variables for setting up connection and communication Socket Socket = null; // socket to connect with ServerRouter PrintWriter out = null; // for writing to ServerRouter BufferedReader in = null; // for reading form ServerRouter InetAddress addr = InetAddress.getLocalHost(); String host = addr.getHostAddress(); // Client machine's IP String routerName = "macs-air.lan"; // ServerRouter host name int SockNum = 5555; // port number // Tries to connect to the ServerRouter try { Socket = new Socket(routerName, SockNum); out = new PrintWriter(Socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(Socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about router: " + routerName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: " + routerName); System.exit(1); } /* out = new PrintWriter(getTcpSocket().getOutputStream(), true); in = new BufferedReader(new InputStreamReader(getTcpSocket().getInputStream()));*/ // Variables for message passing String path = new File(".").getAbsolutePath(); System.out.println(path); Reader reader = new FileReader("file.txt"); BufferedReader fromFile = new BufferedReader(reader); // reader for the string file String fromServer; // messages received from ServerRouter String fromUser; // messages sent to ServerRouter String address ="10.5.2.109"; // destination IP (Server) long t0, t1, t; // Communication process (initial sends/receives out.println(address);// initial send (IP of the destination Server) fromServer = in.readLine();//initial receive from router (verification of connection) System.out.println("ServerRouter: " + fromServer); out.println(host); // Client sends the IP of its machine as initial send t0 = System.currentTimeMillis(); // Communication while loop while ((fromServer = in.readLine()) != null) { System.out.println("Server: " + fromServer); t1 = System.currentTimeMillis(); if (fromServer.equals("Bye.")) // exit statement break; t = t1 - t0; System.out.println("Cycle time: " + t); fromUser = fromFile.readLine(); // reading strings from a file if (fromUser != null) { System.out.println("Client: " + fromUser); out.println(fromUser); // sending the strings to the Server via ServerRouter t0 = System.currentTimeMillis(); } } // closing connections out.close(); in.close(); Socket.close(); } }
import java.net.*; import java.io.*; public class TCPServerRouter { public static void main(String[] args) throws IOException { Socket clientSocket = null; // socket for the thread Object [][] RoutingTable = new Object [10][2]; // routing table int SockNum = 5555; // port number Boolean Running = true; int ind = 0; // indext in the routing table //Accepting connections ServerSocket serverSocket = null; // server socket for accepting connections try { serverSocket = new ServerSocket(5555); System.out.println("ServerRouter is Listening on port: 5555."); } catch (IOException e) { System.err.println("Could not listen on port: 5555."); System.exit(1); } // Creating threads with accepted connections while (Running == true) { try { clientSocket = serverSocket.accept(); SThread t = new SThread(RoutingTable, clientSocket, ind); // creates a thread with a random port t.start(); // starts the thread ind++; // increments the index System.out.println("ServerRouter connected with Client/Server: " + clientSocket.getInetAddress().getHostAddress()); } catch (IOException e) { System.err.println("Client/Server failed to connect."); System.exit(1); } }//end while //closing connections clientSocket.close(); serverSocket.close(); } }
Add code to record and display/print data such as average message sizes, average transmission time, average time for routing-table, etc.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
