Skip to content
Snippets Groups Projects
Commit b61ad19c authored by Brutzman, Don's avatar Brutzman, Don
Browse files

keep track of connectionCount

parent b4cfc9e5
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,8 @@ public class TcpExample2 ...@@ -31,6 +31,8 @@ public class TcpExample2
// ServerSocket waits for a connection from a client. // ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket // Notice that it is outside the loop; ServerSocket
// needs to be made only once. // needs to be made only once.
int connectionCount = 0; // state
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
...@@ -38,7 +40,10 @@ public class TcpExample2 ...@@ -38,7 +40,10 @@ public class TcpExample2
// Stop the program somewhere else. // Stop the program somewhere else.
while(true) 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(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
...@@ -66,6 +71,8 @@ public class TcpExample2 ...@@ -66,6 +71,8 @@ public class TcpExample2
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))"); remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("got another connection, #" + connectionCount); // report progress
// Notice the use of flush() and close(). Without // Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for // the close() to Socket object may stay open for
// a while after the client has stopped needing this // a while after the client has stopped needing this
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment