package MV3500Cohort2018JulySeptember.homework1; import java.io.*; import java.net.*; /** * Very slightly more complex than example1. The only thing this does * differently is introduce a loop into the response, so you don't * have to restart the program after one response. Also, it prints * out the socket pair the server sees. Run the program via telnet * several times and compare the socket pairs. * * telnet localhost 2317-2326 * * If you're sophisticated you can contact the instructor's computer * while running this program. * * telnet ipOfServersLaptop 2317 * telnet ipOfServersLaptop 2318 * telnet ipOfServersLaptop 2319 * telnet ipOfServersLaptop 2320 * telnet ipOfServersLaptop 2321 * telnet ipOfServersLaptop 2322 * telnet ipOfServersLaptop 2323 * telnet ipOfServersLaptop 2324 * telnet ipOfServersLaptop 2325 * telnet ipOfServersLaptop 2326 * * And have him display the socket pairs he got. * @author mcgredo */ public class FurrAssignment2 { /** * Default constructor to silence javadoc warning * @see <a href="https://stackoverflow.com/questions/4488716/java-default-constructor" target="_blank">StackOverflow: Java default constructor</a> */ public FurrAssignment2 () { // default initializations occur here } /** run the program * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { // ServerSocket waits for a connection from a client. // Notice that it is outside the loop; ServerSocket // needs to be made only once. ServerSocket[] serverSocket = new ServerSocket[10]; // created an array for 10 ports to be open int connectionCount = 0; // state int j = 0; //number of ports to open for(int i=2317; j<10; i++){ serverSocket[j] = new ServerSocket(i); //open the ports System.out.println("server established for port # " +i +" in Array slot " +j); j++; } j=0; // Loop, infinitely, waiting for client connections. // Stop the program somewhere else. Socket clientConnections[] = new Socket[10]; //not used, but wanted to create connections. //clientConnections[j] = serverSocket[j].accept(); //Socket clientConnection; boolean wait = true; //not used, tried to find a way to see what port had a pending connection while(true) { //Loop to look for the port that is being connected to, can't find the variable or bool to see what is pending connection // while(wait) // { // for(int i =0; i<10; i++){ // int z = 2317; // if(serverSocket[i].isClosed()){ // //try( // Socket clientConnection = serverSocket[i].accept(); // //) // z++; // wait=false; // return; // } // } // } // for(int i=0; i<10; i++){ // if(serverSocket[i].isClosed()) // j=i; // } try ( Socket clientConnection = serverSocket[j].accept(); // blocks! then proceeds once a connection is "accept"ed //will only follow on for port 2317 since that is in slot 0 in the array ) { connectionCount++; // got another one! OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println("This client response was written by server FurrAssignment2"); // to remote client //Changed name System.out.println("This server response was written by server FurrAssignment2"); // to server console // Changed name // Print some information locally about the Socket // connection. This includes the port and IP numbers // on both sides (the socket pair.) InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress remoteAddress = clientConnection.getInetAddress(); int localPort = clientConnection.getLocalPort(); int remotePort = clientConnection.getPort(); // My socket pair connection looks like this, to localhost: // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) // // Why is the first IP/port the same, while the second set has // different ports? System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))"); System.out.println("got another connection, #" + connectionCount); // report progress j++; // Notice the use of flush() and close(). Without // the close() to Socket object may stay open for // a while after the client has stopped needing this // connection. Close() explicitly ends the connection. ps.flush(); } // got another one! } } catch(Exception e) { System.out.println("problem with networking: " + e); } } }