/* * 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; /** * homework assignment * @author John */ public class JacksonAssignment1 { /** * 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 JacksonAssignment1 () { // default initializations occur here } /** run the program * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { System.out.println("Hello invoker, this server process is now running"); // reporting for duty int popularityCount = 0; // state ServerSocket serverSocket = new ServerSocket(2317); // 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 /** * changed connectionCount to popularityCount */ popularityCount++; OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println("This client response was written by server JacksonAssignment1"); // to remote client System.out.println("This server response was written by server JacksonAssignment1"); // to server console ps.println("You were connection #" + popularityCount + ", 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 )) System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))"); if (popularityCount == 1){ System.out.println("Your server has received " + popularityCount + " hit"); // report progress } else { System.out.println("Your server has received " + popularityCount + " hits"); // report progress } ps.flush(); clientConnection.close(); } } catch (Exception e) { /** * added to the println for fun */ System.out.println("Error code: " + e); } } }