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

In-class commenting and improvements

parent e5cff56c
No related branches found
No related tags found
No related merge requests found
......@@ -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)
{
......
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
......
......@@ -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
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