/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package MV3500Cohort2018JulySeptember.homework1; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * * @author garrettloeffelman */ public class LoeffelmanAssignment1Tcp2 { /** * @param args command-line arguments */ public static void main(String[] args) { // TODO code application logic here try { // ServerSocket waits for a connection from a client. // Notice that it is outside the loop; ServerSocket // needs to be made only once. int connectionCount = 0; // state int portNumber = 2317; ServerSocket serverSocket = new ServerSocket(portNumber); // server decides here what port to listen on. // of interest: often client doesn't care what port it uses locally when connecting to that server port. // Loop, infinitely, waiting for client connections. // Stop the program somewhere else. while(true) { Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed connectionCount++; // got another one! OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); if(connectionCount < 2){ ps.println("Nice job! You (the client) connected to port " + portNumber + ", lets see if it stays open!"); // to remote client ps.println("type 'nc -z -v {host-name-here} 2315-2320'"); System.out.println("A client connected to this server and was instructed to check open ports"); // to server console } if(connectionCount > 2){ ps.println("WOW loeffelmanExample2 Port 2317 is still open, Don't worry, when we close the server it will close"); } System.out.println("This is the server response by LoeffelmanExample2"); ps.println("You were connection #" + connectionCount + ", by my count"); // 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(); // remember the prior question, why are 2 ports different? // 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 )) note IPv6 // 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 // 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(); clientConnection.close(); } } catch(Exception e) { System.out.println("problem with networking: " + e); } } }