diff --git a/projects/TcpExample2/src/tcpexample2/TcpExample2.java b/projects/TcpExample2/src/tcpexample2/TcpExample2.java index 74bd7566196b2c58a68a7e0fa977e9e7acd3bbbf..74b098cda5c43a882e7673ce3590c3c3d242ff8d 100644 --- a/projects/TcpExample2/src/tcpexample2/TcpExample2.java +++ b/projects/TcpExample2/src/tcpexample2/TcpExample2.java @@ -31,6 +31,8 @@ public class TcpExample2 // 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 ServerSocket serverSocket = new ServerSocket(2317); @@ -38,7 +40,10 @@ public class TcpExample2 // Stop the program somewhere else. while(true) { - Socket clientConnection = serverSocket.accept(); + 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); @@ -66,6 +71,8 @@ public class TcpExample2 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