diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java index 468abacc3f9bbaed594d18413523d4a59d0cbc6b..a5841c8d2804b061eca53741ddd6943b9b660d74 100644 --- a/examples/src/TcpExamples/TcpExample3Client.java +++ b/examples/src/TcpExamples/TcpExample3Client.java @@ -27,9 +27,12 @@ public class TcpExample3Client { InputStreamReader isr; BufferedReader br; String serverMessage; + int clientLoopCount = 0; try { - while (true) { + while (true) + { + clientLoopCount++; // increment at beginning of loop for reliability System.out.println("TcpExample3Client creating socket..."); // We request an IP to connect to ("localhost") and @@ -49,12 +52,16 @@ public class TcpExample3Client { // from the server instead of one only. serverMessage = br.readLine(); System.out.println("=================================================="); - System.out.println("Now we're talking!"); + + System.out.print ("Client loop " + clientLoopCount + ": "); + System.out.println("now we're talking!"); System.out.println("The message the server sent was: '" + serverMessage + "'"); // socket gets closed, either automatically/silently by this code (or possibly by the server) } // end while(true) - } catch (IOException e) { + } + catch (IOException e) + { System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening System.err.println("Error: " + e); @@ -62,10 +69,12 @@ public class TcpExample3Client { if (e instanceof java.net.BindException) { System.err.println("*** Be sure to stop any other running instances of programs using this port!"); } - } finally { - - // program exit: tell somebody about that - System.out.println("\nclient exit"); + } + finally // occurs after any other activity when shutting down + { + // program exit: tell somebody about that happening. Likely cause: server drops connection. + System.out.println(); + System.out.println("TcpExample3Client exit"); } } } diff --git a/examples/src/TcpExamples/TcpExample3Server.java b/examples/src/TcpExamples/TcpExample3Server.java index 409a3a9281c474dd9e53ddfb773da90dcdfb68e3..5741aa7eea5b96d4c1561252fa70189e72df44f3 100644 --- a/examples/src/TcpExamples/TcpExample3Server.java +++ b/examples/src/TcpExamples/TcpExample3Server.java @@ -37,6 +37,7 @@ public class TcpExample3Server { PrintStream ps; InetAddress localAddress, remoteAddress; int localPort, remotePort; + int serverLoopCount = 0; // Server is up and waiting (i.e. "blocked" or paused) // Loop, infinitely, waiting for client connections. @@ -44,12 +45,14 @@ public class TcpExample3Server { while (true) { // block until connected to a client - try (Socket clientConnectionSocket = serverSocket.accept()) { + try (Socket clientConnectionSocket = serverSocket.accept()) + { + serverLoopCount++; // increment at beginning of loop for reliability // Now hook everything up (i.e. set up the streams), Java style: os = clientConnectionSocket.getOutputStream(); ps = new PrintStream(os); - ps.println("This message was produced by the server."); // this gets sent back to client! + ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client! // Print some information locally about the Socket connection. // This includes the port and IP numbers on both sides (the socket pair). @@ -57,6 +60,8 @@ public class TcpExample3Server { remoteAddress = clientConnectionSocket.getInetAddress(); localPort = clientConnectionSocket.getLocalPort(); remotePort = clientConnectionSocket.getPort(); + + System.out.print ("Server loop " + serverLoopCount + ": "); // 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 )) diff --git a/examples/src/TcpExamples/TcpExample3TerminalLog.txt b/examples/src/TcpExamples/TcpExample3TerminalLog.txt index af6c6b8bec360baec1855ac17bf99dc7aab4da7d..e605f2b16642c80c07efb567943f5168c99b9b88 100644 --- a/examples/src/TcpExamples/TcpExample3TerminalLog.txt +++ b/examples/src/TcpExamples/TcpExample3TerminalLog.txt @@ -5,32 +5,40 @@ Invocation instructions: Program responses: =================================================== -run: +run-single: TcpExample3Server has started... -TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49239 )) -TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49240 )) -TcpExample3Server socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49241 )) +Server loop 1: TcpExample3Server socket pair showing host name, address, port: + (( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 2317 ), ( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 54180 )) + note HostName matches address if host has no DNS name +Server loop 2: TcpExample3Server socket pair showing host name, address, port: + (( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 2317 ), ( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 54181 )) + note HostName matches address if host has no DNS name +Server loop 3: TcpExample3Server socket pair showing host name, address, port: + (( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 2317 ), ( 0:0:0:0:0:0:0:1=0:0:0:0:0:0:0:1, 54182 )) + note HostName matches address if host has no DNS name [etc.] [kill process to exit] =================================================== -run: +run-single: TcpExample3Client creating socket... ================================================== -Now we're talking! -The message the server sent was This was written by the server +Client loop 1: now we're talking! +The message the server sent was: 'This is response 1 produced by the server.' TcpExample3Client creating socket... ================================================== -Now we're talking! -The message the server sent was This was written by the server +Client loop 2: now we're talking! +The message the server sent was: 'This is response 2 produced by the server.' TcpExample3Client creating socket... ================================================== -Now we're talking! -The message the server sent was This was written by the server +Client loop 3: now we're talking! +The message the server sent was: 'This is response 3 produced by the server.' TcpExample3Client creating socket... ================================================== [etc.] +TcpExample3Client exit You must kill this process to get it to stop, each is in an infinite loop. -If you kill the server, that will kill the client automatically when the connection drops. +If you kill the server, that will kill the client automatically when the +connection drops, because the client software program was written that way.