From b61ad19c3de67f0b63eaf920e835e3977ab9431d Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Mon, 12 Feb 2018 16:21:28 -0800 Subject: [PATCH] keep track of connectionCount --- projects/TcpExample2/src/tcpexample2/TcpExample2.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/projects/TcpExample2/src/tcpexample2/TcpExample2.java b/projects/TcpExample2/src/tcpexample2/TcpExample2.java index 74bd756619..74b098cda5 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 -- GitLab