diff --git a/projects/Assignments/2018JulySeptember/homework1/CainAssignment1.java b/projects/Assignments/2018JulySeptember/homework1/CainAssignment1.java new file mode 100644 index 0000000000000000000000000000000000000000..ff41a3c6df969e3f7f16b57b8a03bea186e80b2c --- /dev/null +++ b/projects/Assignments/2018JulySeptember/homework1/CainAssignment1.java @@ -0,0 +1,68 @@ +package CainAssignment1; + +import java.io.*; +import java.net.*; + +/** + * changed telnet # from 2317 to 2318. This won't affect the protocol handshake + * as long as client inputs the correct telnet #. + * + * telnet localhost 2318 + * + * ask for the ip address of the server + * <code>telnet ipOfServersLaptop 2318</code> + */ +public class CainAssignment1 { + + public static void main(String[] args) { + try { + int popularityCount = 0; // state + + ServerSocket serverSocket = new ServerSocket(2318); // 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 CainAssignment1"); // to remote client + System.out.println("This server response was written by server CainAssignment1"); // 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 + " ))"); + System.out.println("you'er server is blowing up! Now serving #" + popularityCount); // report progress + + ps.flush(); + clientConnection.close(); + } + } catch (Exception e) { + /** + * added to the println for fun + */ + System.out.println("you got all sorts of problems with your networking: " + e); + } + } +}