diff --git a/projects/TcpExample2/src/tcpexample2/TcpExample2.java b/projects/TcpExample2/src/tcpexample2/TcpExample2.java index ecee5383381d04ecb04ad353639b5268f27ab840..ecb0538d40f33cfa367e0b2335db00f3e3336fa9 100644 --- a/projects/TcpExample2/src/tcpexample2/TcpExample2.java +++ b/projects/TcpExample2/src/tcpexample2/TcpExample2.java @@ -1,4 +1,3 @@ - package tcpexample2; import java.io.*; @@ -16,7 +15,7 @@ import java.net.*; * If you're sophisticated you can contact the instructor's computer * while running this program. * - * telnet <ipOfServersLaptop> 2317 + * <code>telnet ipOfServersLaptop 2317</code> * * And have him display the socket pairs he got. * @author mcgredo @@ -34,7 +33,8 @@ public class TcpExample2 int connectionCount = 0; // state - ServerSocket serverSocket = new ServerSocket(2317); + 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. @@ -49,27 +49,28 @@ public class TcpExample2 ps.println("This client response was written by server TcpExample2"); // to remote client System.out.println("This server response was written by server TcpExample2"); // to server console + + 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 localAddress = clientConnection.getLocalAddress(); InetAddress remoteAddress = clientConnection.getInetAddress(); - int localPort = clientConnection.getLocalPort(); - int remotePort = clientConnection.getPort(); + 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 )) + // 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 + " ))"); + remoteAddress.toString() + ", " + remotePort + " ))"); System.out.println("got another connection, #" + connectionCount); // report progress @@ -85,7 +86,5 @@ public class TcpExample2 { System.out.println("problem with networking: " + e); } - } - }