diff --git a/examples/src/TcpExamples/TcpExample4Client.java b/examples/src/TcpExamples/TcpExample4Client.java index c5049c45374e5ba9b3a45d775a6ff8d5a0c59d25..cb5655aa823a70c674e59a8415f013893d834e68 100644 --- a/examples/src/TcpExamples/TcpExample4Client.java +++ b/examples/src/TcpExamples/TcpExample4Client.java @@ -13,13 +13,15 @@ import java.net.*; */ public class TcpExample4Client { + static int MAX_LOOP_COUNT = 4; + public static void main(String[] args) { try { - System.out.println("TcpExample4Client start"); + System.out.println("TcpExample4Client start, loop " + MAX_LOOP_COUNT + " times"); System.out.println("=================================================="); - for (int loopCount=1; loopCount <= 4; loopCount++) // loop 4 times + for (int loopCount=1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit { System.out.println("TcpExample4Client creating socket #" + loopCount + "..."); @@ -32,16 +34,18 @@ public class TcpExample4Client // line after 10 sec. long startTime = System.currentTimeMillis(); - Socket socket = new Socket("localhost", 2317); + // open a socket for each loop + Socket socket = new Socket("localhost", 2317); - // Read the single line written by the server. We'd - // do things a bit differently if many lines to be read + // Setup. Read the single line written by the server. + // We'd do things a bit differently if many lines to be read // from the server, instead of one only. InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); - - String serverMessage = br.readLine(); +System.out.println("client point 1..."); + String serverMessage = br.readLine(); // blocks? +System.out.println("client point 2..."); long readTime = System.currentTimeMillis(); long timeLength = readTime - startTime; @@ -51,6 +55,7 @@ public class TcpExample4Client // To push this further, launch multiple copies of TcpExample4Client simultaneously } System.out.println("TcpExample4Client complete"); + // main method now exits } catch(IOException e) { diff --git a/examples/src/TcpExamples/TcpExample4HandlerThread.java b/examples/src/TcpExamples/TcpExample4HandlerThread.java index 8518f02a8066111ab52396850d499dd939f0464b..6b67f7b8a3b8fe91bc9f84fc4c2ae3fea0284c2b 100644 --- a/examples/src/TcpExamples/TcpExample4HandlerThread.java +++ b/examples/src/TcpExamples/TcpExample4HandlerThread.java @@ -1,5 +1,3 @@ - - package TcpExamples; import java.io.*; @@ -47,11 +45,12 @@ public class TcpExample4HandlerThread extends Thread final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds System.out.println("TcpExample4HandlerThread pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug - Thread.sleep(TIMEOUT); // 10 seconds + Thread.sleep(TIMEOUT); + // ps is the PrintStream is the Java way to use System.print() to pass data along the socket. ps.println("This message was written by the server TcpExample4HandlerThread"); - ps.flush(); - socket.close(); + ps.flush(); // make sure that it indeed escapes current process and reaches the client + socket.close(); // all clear, no longer need socket System.out.println("TcpExample4HandlerThread finished handling a thread, now exit."); } catch(IOException | InterruptedException e) // either a networking or a threading problem diff --git a/examples/src/TcpExamples/TcpExample4TerminalLog.txt b/examples/src/TcpExamples/TcpExample4TerminalLog.txt index e83515bca0bf06c98994598ad431bab2b6cacd28..b44a8a50bdb103a5b24fc819ebd102aa9fe0fb33 100644 --- a/examples/src/TcpExamples/TcpExample4TerminalLog.txt +++ b/examples/src/TcpExamples/TcpExample4TerminalLog.txt @@ -3,15 +3,16 @@ Invocation instructions: 2. don't run TcpExample4HandlerThread since it is launched as needed 3. run/debug TcpExample4Client.java -Program responses: +Two program response logs: +=================================================== =================================================== run: TcpExample4ThreadServer ready to accept socket connections... ============================================================= TcpExample4ThreadServer.handlerThread invocation for connection #1... -TcpExample4ThreadServer.handlerThread is launched, awaiting another connection... -TcpExample4HandlerThread starting to handle a thread... +TcpExample4ThreadServer.handlerThread is launched, awaiting another connection...T +cpExample4HandlerThread starting to handle a thread... TcpExample4HandlerThread pausing for TIMEOUT=10000ms TcpExample4HandlerThread finished handling a thread, now exit. ============================================================= @@ -34,6 +35,7 @@ TcpExample4HandlerThread pausing for TIMEOUT=10000ms TcpExample4HandlerThread finished handling a thread, now exit. BUILD STOPPED (total time: 1 minute 7 seconds) +=================================================== =================================================== run: TcpExample4Client start @@ -56,3 +58,6 @@ TcpExample4Client: time msec required for read=10003 ================================================== TcpExample4Client complete BUILD SUCCESSFUL (total time: 40 seconds) + +=================================================== +=================================================== \ No newline at end of file