Skip to content
Snippets Groups Projects
Commit df10c5aa authored by terryd.norbraten's avatar terryd.norbraten
Browse files

[Terry N.] Use explicit class name calls. Minimize object creation in

loops. Use static field declarations
parent 39e28599
No related branches found
No related tags found
No related merge requests found
...@@ -54,10 +54,10 @@ public class TcpExample2ConnectionCounting ...@@ -54,10 +54,10 @@ public class TcpExample2ConnectionCounting
OutputStream os = clientConnection.getOutputStream(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server TcpExample2ConnectionCounting"); // to remote client ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client
System.out.println("This server response was written by server TcpExample2ConnectionCounting"); // to server console System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
ps.println("You were connection #" + connectionCount + ", by my count"); ps.println("You were connection #" + connectionCount + ", by my count");
// 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.)
...@@ -89,7 +89,7 @@ public class TcpExample2ConnectionCounting ...@@ -89,7 +89,7 @@ public class TcpExample2ConnectionCounting
} }
catch(IOException e) catch(IOException e)
{ {
System.err.println("Problem with TcpExample2ConnectionCounting networking:"); // describe what is happening System.err.println("Problem with " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening
System.err.println("Error: " + e); System.err.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)
......
...@@ -18,7 +18,8 @@ public class TcpExample3Client { ...@@ -18,7 +18,8 @@ public class TcpExample3Client {
/** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 /** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1
* @see <a href="https://en.wikipedia.org/wiki/localhost">https://en.wikipedia.org/wiki/localhost</a> * @see <a href="https://en.wikipedia.org/wiki/localhost">https://en.wikipedia.org/wiki/localhost</a>
* @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a> */ * @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a>
*/
public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
/** /**
...@@ -30,7 +31,7 @@ public class TcpExample3Client { ...@@ -30,7 +31,7 @@ public class TcpExample3Client {
// Local variables/fields // Local variables/fields
Socket socket = null; Socket socket = null;
InputStream is; InputStream is;
InputStreamReader isr; Reader isr;
BufferedReader br; BufferedReader br;
String serverMessage; String serverMessage;
int clientLoopCount = 0; int clientLoopCount = 0;
...@@ -39,7 +40,7 @@ public class TcpExample3Client { ...@@ -39,7 +40,7 @@ public class TcpExample3Client {
while (true) while (true)
{ {
clientLoopCount++; // increment at beginning of loop for reliability clientLoopCount++; // increment at beginning of loop for reliability
System.out.println("TcpExample3Client creating socket..."); System.out.println(TcpExample3Client.class.getName() + " creating socket...");
// We request an IP to connect to ("localhost") and // We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes // port number at that IP (2317). This establishes
...@@ -68,7 +69,7 @@ public class TcpExample3Client { ...@@ -68,7 +69,7 @@ public class TcpExample3Client {
} }
catch (IOException e) catch (IOException e)
{ {
System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening System.err.println("Problem with " + TcpExample3Client.class.getName() + " networking:"); // describe what is happening
System.err.println("Error: " + e); System.err.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
...@@ -85,7 +86,7 @@ public class TcpExample3Client { ...@@ -85,7 +86,7 @@ public class TcpExample3Client {
// program exit: tell somebody about that happening. Likely cause: server drops connection. // program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println(); System.out.println();
System.out.println("TcpExample3Client exit"); System.out.println(TcpExample3Client.class.getName() + " exit");
} }
} }
} }
...@@ -34,7 +34,7 @@ public class TcpExample3Server { ...@@ -34,7 +34,7 @@ public class TcpExample3Server {
// ServerSocket waits for a connection from a client. // ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket // Notice that it is outside the loop; ServerSocket
// needs to be made only once. // needs to be made only once.
System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first System.out.println(TcpExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os; OutputStream os;
...@@ -72,7 +72,7 @@ public class TcpExample3Server { ...@@ -72,7 +72,7 @@ public class TcpExample3Server {
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
// Why is the first IP/port the same, while the second set has different ports? // Why is the first IP/port the same, while the second set has different ports?
System.out.println("TcpExample3Server socket pair showing host name, address, port:"); System.out.println(TcpExample3Server.class.getName() + " socket pair showing host name, address, port:");
System.out.println(" (( " + System.out.println(" (( " +
localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
...@@ -90,7 +90,7 @@ public class TcpExample3Server { ...@@ -90,7 +90,7 @@ public class TcpExample3Server {
} }
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("Problem with TcpExample3Server networking: " + e); System.err.println("Problem with " + TcpExample3Server.class.getName() + " networking: " + 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) {
......
...@@ -22,11 +22,11 @@ public class TcpExample4Client { ...@@ -22,11 +22,11 @@ public class TcpExample4Client {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
try { try {
System.out.println("TcpExample4Client start, loop " + MAX_LOOP_COUNT + " times"); System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
System.out.println("=================================================="); System.out.println("==================================================");
for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
{ {
System.out.println("TcpExample4Client creating socket #" + loopCount + "..."); System.out.println(TcpExample4Client.class.getName() + " creating socket #" + loopCount + "...");
// We request an IP to connect to ("localhost") and // We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes // port number at that IP (2317). This establishes
...@@ -44,22 +44,22 @@ public class TcpExample4Client { ...@@ -44,22 +44,22 @@ public class TcpExample4Client {
// We'd do things a bit differently if many lines to be read // We'd do things a bit differently if many lines to be read
// from the server, instead of one only. // from the server, instead of one only.
InputStream is = socket.getInputStream(); InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is); Reader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine(); // blocks String serverMessage = br.readLine(); // blocks
long readTime = System.currentTimeMillis(); long readTime = System.currentTimeMillis();
long timeLength = readTime - startTime; long timeLength = readTime - startTime;
System.out.println("TcpExample4Client: message received from server='" + serverMessage + "'"); System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
System.out.println("TcpExample4Client: time msec required for read=" + timeLength); System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
System.out.println("=================================================="); System.out.println("==================================================");
// To push this further, launch multiple copies of TcpExample4Client simultaneously // To push this further, launch multiple copies of TcpExample4Client simultaneously
} }
System.out.println("TcpExample4Client complete"); System.out.println(TcpExample4Client.class.getName() + " complete");
// main method now exits // main method now exits
} catch (IOException e) { } catch (IOException e) {
System.out.println("Problem with TcpExample4Client networking:networking:"); // describe what is happening System.out.println("Problem with " + TcpExample4Client.class.getName() + " networking: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) {
......
...@@ -21,23 +21,25 @@ public class TcpExample4DispatchServer { ...@@ -21,23 +21,25 @@ public class TcpExample4DispatchServer {
{ {
try { try {
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
TcpExample4HandlerThread handlerThread;
Socket clientConnection;
int connectionCount = 0; // state variable int connectionCount = 0; // state variable
System.out.println("TcpExample4DispatchServer ready to accept socket connections..."); System.out.println(TcpExample4DispatchServer.class.getName() + " ready to accept socket connections...");
while (true) // infinite loop while (true) // infinite loop
{ {
Socket clientConnection = serverSocket.accept(); // block until connected 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("TcpExample4DispatchServer.handlerThread invocation for connection #" + connectionCount + "..."); System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread invocation for connection #" + connectionCount + "...");
TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection); 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("TcpExample4DispatchServer.handlerThread is launched, awaiting another connection..."); System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread is launched, awaiting another connection...");
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println("Problem with TcpExample4DispatchServer networking:"); // describe what is happening System.out.println("Problem with " + TcpExample4DispatchServer.class.getName() + " 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) {
......
...@@ -14,8 +14,7 @@ import java.net.*; ...@@ -14,8 +14,7 @@ import java.net.*;
* @author Don Brutzman * @author Don Brutzman
* @author MV3500 class * @author MV3500 class
*/ */
class TcpExample4HandlerThread extends Thread
public class TcpExample4HandlerThread extends Thread
{ {
/** The socket connection to a client */ /** The socket connection to a client */
Socket socket; Socket socket;
...@@ -26,7 +25,7 @@ public class TcpExample4HandlerThread extends Thread ...@@ -26,7 +25,7 @@ public class TcpExample4HandlerThread extends Thread
* *
* @param socket The socket connection handled by this thread * @param socket The socket connection handled by this thread
*/ */
public TcpExample4HandlerThread(Socket socket) TcpExample4HandlerThread(Socket socket)
{ {
this.socket = socket; this.socket = socket;
} }
...@@ -35,31 +34,33 @@ public class TcpExample4HandlerThread extends Thread ...@@ -35,31 +34,33 @@ public class TcpExample4HandlerThread extends Thread
* to handling the connection with a sleep(). This means * to handling the connection with a sleep(). This means
* the client won't see a server connection response for ten seconds (default). * the client won't see a server connection response for ten seconds (default).
*/ */
// @overriding run() method in Java Thread class is deliberate // @overriding run() method in Java Thread class is deliberate
@Override @Override
public void run() public void run()
{ {
try try
{ {
System.out.println("TcpExample4HandlerThread starting to handle a thread..."); System.out.println(TcpExample4HandlerThread.class.getName() + " starting to handle a thread...");
// get the connection output stream, then wait a period of time. // get the connection output stream, then wait a period of time.
OutputStream os = socket.getOutputStream(); OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
System.out.println("TcpExample4HandlerThread pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug System.out.println(TcpExample4HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug
Thread.sleep(TIMEOUT); Thread.sleep(TIMEOUT);
// ps is the PrintStream is the Java way to use System.print() to pass data along the socket. // 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"); // TODO insert socket count here! ps.println("This message was written by the server " + TcpExample4HandlerThread.class.getName()); // TODO insert socket count here!
ps.flush(); // make sure that it indeed escapes current process and reaches the client ps.flush(); // make sure that it indeed escapes current process and reaches the client
socket.close(); // all clear, no longer need socket socket.close(); // all clear, no longer need socket
System.out.println("TcpExample4HandlerThread finished handling a thread, now exit."); System.out.println(TcpExample4HandlerThread.class.getName() + " finished handling a thread, now exit.");
} }
catch(IOException | InterruptedException e) // either a networking or a threading problem catch(IOException | InterruptedException e) // either a networking or a threading problem
{ {
System.out.println("Problem with TcpExample4HandlerThread networking:"); // describe what is happening System.out.println("Problem with " + TcpExample4HandlerThread.class.getName() + " 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)
System.out.println("*** Be sure to stop any other running instances of programs using this port!"); System.out.println("*** Be sure to stop any other running instances of programs using this port!");
......
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