diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index 158c837d050f5a59f0c2a58e58d4e350e89f0194..988aac7e8f7a288b1ea201bf208a239c5748d999 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java +++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java @@ -54,10 +54,10 @@ public class TcpExample2ConnectionCounting OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); - ps.println("This client response was written by server TcpExample2ConnectionCounting"); // to remote client - System.out.println("This server response was written by server TcpExample2ConnectionCounting"); // to server console + 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.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. // This includes the port and IP numbers on both sides (the socket pair.) @@ -89,7 +89,7 @@ public class TcpExample2ConnectionCounting } 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); // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java index 86dde95e2fe549741736854eadaee4bac5e890c1..94ca31012128a28f2253a854ac7cdeb6fa812e36 100644 --- a/examples/src/TcpExamples/TcpExample3Client.java +++ b/examples/src/TcpExamples/TcpExample3Client.java @@ -18,7 +18,8 @@ public class TcpExample3Client { /** 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/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"; /** @@ -30,7 +31,7 @@ public class TcpExample3Client { // Local variables/fields Socket socket = null; InputStream is; - InputStreamReader isr; + Reader isr; BufferedReader br; String serverMessage; int clientLoopCount = 0; @@ -39,7 +40,7 @@ public class TcpExample3Client { while (true) { 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 // port number at that IP (2317). This establishes @@ -70,7 +71,7 @@ public class TcpExample3Client { } 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); // Provide more helpful information to user if exception occurs due to running twice at one time @@ -87,7 +88,7 @@ public class TcpExample3Client { // program exit: tell somebody about that happening. Likely cause: server drops connection. System.out.println(); - System.out.println("TcpExample3Client exit"); + System.out.println(TcpExample3Client.class.getName() + " exit"); } } } diff --git a/examples/src/TcpExamples/TcpExample3Server.java b/examples/src/TcpExamples/TcpExample3Server.java index 7af645130fa7096bd8ca2bdd38e1b6fb55a4a2d6..0e7a3f29f14da43c766d9a9f083a01a21c0358c3 100644 --- a/examples/src/TcpExamples/TcpExample3Server.java +++ b/examples/src/TcpExamples/TcpExample3Server.java @@ -36,7 +36,7 @@ public class TcpExample3Server { // ServerSocket waits for a connection from a client. // Notice that it is outside the loop; ServerSocket // 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); OutputStream os; @@ -74,7 +74,7 @@ public class TcpExample3Server { // 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? - 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(" (( " + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); @@ -92,7 +92,7 @@ public class TcpExample3Server { } } } 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 if (e instanceof java.net.BindException) { diff --git a/examples/src/TcpExamples/TcpExample4Client.java b/examples/src/TcpExamples/TcpExample4Client.java index 464fd7be1323669a305427490783633bbfa6bb48..d23536a8887d420f74cb2725b66e2be0c957c34d 100644 --- a/examples/src/TcpExamples/TcpExample4Client.java +++ b/examples/src/TcpExamples/TcpExample4Client.java @@ -22,11 +22,11 @@ public class TcpExample4Client { */ public static void main(String[] args) { 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("=================================================="); 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 // port number at that IP (2317). This establishes @@ -44,22 +44,22 @@ public class TcpExample4Client { // 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); + Reader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String serverMessage = br.readLine(); // blocks long readTime = System.currentTimeMillis(); long timeLength = readTime - startTime; - System.out.println("TcpExample4Client: message received from server='" + serverMessage + "'"); - System.out.println("TcpExample4Client: time msec required for read=" + timeLength); + System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'"); + System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength); System.out.println("=================================================="); // 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 } 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); // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) { diff --git a/examples/src/TcpExamples/TcpExample4DispatchServer.java b/examples/src/TcpExamples/TcpExample4DispatchServer.java index 705da361b13ef48cf70d54805766a776f176cc35..e5e2df5efdd220f35f72a5a49ae00a7dfd0d30cb 100644 --- a/examples/src/TcpExamples/TcpExample4DispatchServer.java +++ b/examples/src/TcpExamples/TcpExample4DispatchServer.java @@ -21,23 +21,25 @@ public class TcpExample4DispatchServer { { try { ServerSocket serverSocket = new ServerSocket(2317); + TcpExample4HandlerThread handlerThread; + Socket clientConnection; 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 { - Socket clientConnection = serverSocket.accept(); // block until connected + clientConnection = serverSocket.accept(); // block until connected connectionCount++; // unblocked, got another connection System.out.println("============================================================="); - System.out.println("TcpExample4DispatchServer.handlerThread invocation for connection #" + connectionCount + "..."); - TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection); + System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread invocation for connection #" + connectionCount + "..."); + handlerThread = new TcpExample4HandlerThread(clientConnection); 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) { - 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); // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) { diff --git a/examples/src/TcpExamples/TcpExample4HandlerThread.java b/examples/src/TcpExamples/TcpExample4HandlerThread.java index a1e4882b06f81fd6ca06046d5090d82b945213ca..dc6d3f144b72c8df871ff694a1b7c8c5df701e49 100644 --- a/examples/src/TcpExamples/TcpExample4HandlerThread.java +++ b/examples/src/TcpExamples/TcpExample4HandlerThread.java @@ -14,8 +14,7 @@ import java.net.*; * @author Don Brutzman * @author MV3500 class */ - -public class TcpExample4HandlerThread extends Thread +class TcpExample4HandlerThread extends Thread { /** The socket connection to a client */ Socket socket; @@ -26,7 +25,7 @@ public class TcpExample4HandlerThread extends Thread * * @param socket The socket connection handled by this thread */ - public TcpExample4HandlerThread(Socket socket) + TcpExample4HandlerThread(Socket socket) { this.socket = socket; } @@ -35,31 +34,33 @@ public class TcpExample4HandlerThread extends Thread * to handling the connection with a sleep(). This means * the client won't see a server connection response for ten seconds (default). */ - // @overriding run() method in Java Thread class is deliberate - @Override + // @overriding run() method in Java Thread class is deliberate + @Override public void run() { 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. - OutputStream os = socket.getOutputStream(); - PrintStream ps = new PrintStream(os); + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); - 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); + final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds + System.out.println(TcpExample4HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug + 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"); // 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 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 { - 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); + // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) System.out.println("*** Be sure to stop any other running instances of programs using this port!");