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

rename, improve documentation for clarity

parent 40ad1bb6
No related branches found
No related tags found
No related merge requests found
...@@ -5,11 +5,13 @@ import java.net.*; ...@@ -5,11 +5,13 @@ import java.net.*;
//import java.time.LocalTime; // conversion? //import java.time.LocalTime; // conversion?
/** /**
* This is a slightly different client. It establishes a connection * This client program establishes a socket connection
* to the server, then checks how long it takes to read the single * to the dispatch server, then checks how long it takes to read the single
* line it expects. * line it expects.
* *
* @author mcgredo * @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/ */
public class TcpExample4Client public class TcpExample4Client
{ {
...@@ -43,9 +45,8 @@ public class TcpExample4Client ...@@ -43,9 +45,8 @@ public class TcpExample4Client
InputStream is = socket.getInputStream(); InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is); InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
System.out.println("client point 1...");
String serverMessage = br.readLine(); // blocks? String serverMessage = br.readLine(); // blocks
System.out.println("client point 2...");
long readTime = System.currentTimeMillis(); long readTime = System.currentTimeMillis();
long timeLength = readTime - startTime; long timeLength = readTime - startTime;
......
...@@ -4,12 +4,14 @@ import java.io.IOException; ...@@ -4,12 +4,14 @@ import java.io.IOException;
import java.net.*; import java.net.*;
/** /**
* An example of using threads to handle multiple connections * A server example that creates and dispaches a new thread to handle
* at the same time. * multiple connections one after another, running in parallel.
* *
* @author mcgredo * @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/ */
public class TcpExample4ThreadServer { public class TcpExample4DispatchServer {
public static void main(String[] args) // execution starts here public static void main(String[] args) // execution starts here
{ {
...@@ -19,22 +21,22 @@ public class TcpExample4ThreadServer { ...@@ -19,22 +21,22 @@ public class TcpExample4ThreadServer {
int connectionCount = 0; // state variable int connectionCount = 0; // state variable
System.out.println("TcpExample4ThreadServer ready to accept socket connections..."); System.out.println("TcpExample4DispatchServer ready to accept socket connections...");
while(true) // infinite loop while(true) // infinite loop
{ {
Socket clientConnection = serverSocket.accept(); // block until connected Socket clientConnection = serverSocket.accept(); // block until connected
connectionCount++; // unblocked, got another connection connectionCount++; // unblocked, got another connection
System.out.println("============================================================="); System.out.println("=============================================================");
System.out.println("TcpExample4ThreadServer.handlerThread invocation for connection #" + connectionCount + "..."); System.out.println("TcpExample4DispatchServer.handlerThread invocation for connection #" + connectionCount + "...");
TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection); TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection);
handlerThread.start(); // invokes the run() method in that object handlerThread.start(); // invokes the run() method in that object
System.out.println("TcpExample4ThreadServer.handlerThread is launched, awaiting another connection..."); System.out.println("TcpExample4DispatchServer.handlerThread is launched, awaiting another connection...");
} }
} }
catch(IOException e) catch(IOException e)
{ {
System.out.println("Problem with TcpExample4ThreadServer networking:"); // describe what is happening System.out.println("Problem with TcpExample4DispatchServer networking:"); // describe what is happening
System.out.println("Error: " + e); System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time // Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException) if (e instanceof java.net.BindException)
......
...@@ -4,12 +4,15 @@ import java.io.*; ...@@ -4,12 +4,15 @@ import java.io.*;
import java.net.*; import java.net.*;
/** /**
* Handles all the logic associated with one connection * A program that handles all logic associated with one socket connection
* by running in a thread of its own. This is the server * by running in a thread of its own. This is the server
* portion as well, so we artificially invent what happens * portion as well, so we artificially invent what happens
* if the server can't respond to a connection for 10 sec. * if the server can't respond to a connection for several seconds.
* Warning: do not run this class! It is created automatically by TcpExample4DispatchServer.
* *
* @author Don McGregor * @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/ */
public class TcpExample4HandlerThread extends Thread public class TcpExample4HandlerThread extends Thread
......
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