Skip to content
Snippets Groups Projects
Commit ea86f17d authored by brutzman's avatar brutzman
Browse files

put loop counters on both client and server for comparison

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