diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorCopyTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java similarity index 85% rename from assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorCopyTcpExample1Telnet.java rename to assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java index 1a85abb0931d94778ea70ae647ce1df1377e902d..ab8e1ff23b6adcd30f63084d2b543bc6d753da6e 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorCopyTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java @@ -29,7 +29,7 @@ import java.net.*; * * @author mcgredo, brutzman`` */ -public class BrutzmanRefactorCopyTcpExample1Telnet +public class BrutzmanRefactorTcpExample1Telnet { public static void main(String[] args) { @@ -65,9 +65,15 @@ public class BrutzmanRefactorCopyTcpExample1Telnet catch(IOException e) { System.out.println("problem with networking: " + e); -// if (e.getClass() == java.net.BindException) // TODO whazzup? - if (e.getMessage().equals("Address already in use: NET_Bind")) - System.out.println("Be sure to stop any other running instances of this program."); + + // Program modification: provide more helpful information to user if + // exception occurs when running twice at one time + + // brute force exception checking, can be brittle if exection message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + + if (e instanceof java.net.BindException) + System.out.println("Be sure to stop any other running instances of this program!"); } } } diff --git a/examples/src/TcpExamples/TcpExample1Telnet.java b/examples/src/TcpExamples/TcpExample1Telnet.java index 373c0f58ebb4345af3c4dcdeb79f1206ffb11706..65afb1959f0a8ee33c81220916baa5921e3fe94c 100644 --- a/examples/src/TcpExamples/TcpExample1Telnet.java +++ b/examples/src/TcpExamples/TcpExample1Telnet.java @@ -64,7 +64,10 @@ public class TcpExample1Telnet } catch(IOException e) { - System.out.println("problem with networking: " + e); + System.out.println("*** Problem with networking: " + 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index 897fe538f7ec8c0c03a63e75776d659decb76074..9ab0e481be1c5f78075e8fe99701b75bde161c91 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java +++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java @@ -27,6 +27,7 @@ public class TcpExample2ConnectionCounting try { System.out.println("TcpExample2ConnectionCounting has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317)..." ); // ServerSocket waits for a connection from a client. @@ -84,6 +85,9 @@ public class TcpExample2ConnectionCounting catch(IOException e) { System.out.println("problem with networking: " + 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java index 87e44dae4ce5de6b80532253e6d2c109ad20e121..422c16234c4c328c0c9688efbd8d0a647bb1af05 100644 --- a/examples/src/TcpExamples/TcpExample3Client.java +++ b/examples/src/TcpExamples/TcpExample3Client.java @@ -41,13 +41,16 @@ public class TcpExample3Client { String serverMessage = br.readLine(); System.out.println("=================================================="); System.out.println("Now we're talking!"); - System.out.println("The message the server sent was " + serverMessage); + System.out.println("The message the server sent was '" + serverMessage + "'"); // socket gets closed, either automatically/silently this code (or possibly by server) } // end while(true) } catch (IOException e) { System.out.println("Problem with client: "); // describe what is happening System.out.println(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!"); } // program exit: tell somebody about that System.out.println("client exit"); diff --git a/examples/src/TcpExamples/TcpExample3Server.java b/examples/src/TcpExamples/TcpExample3Server.java index eeccda6f4bb813ebb8c1296b6424a9837832bd9d..f6c15238289768e38cd48f737accf21f80286a44 100644 --- a/examples/src/TcpExamples/TcpExample3Server.java +++ b/examples/src/TcpExamples/TcpExample3Server.java @@ -4,8 +4,8 @@ import java.io.*; import java.net.*; /** - * Very slightly more complex than example1. A complete copy of example 2. The - * only thing this does differently is introduce a loop into the response, so + * Very slightly more complex than example1, further modifying example 2. + * The only thing this does differently is introduce a loop into the response, so * you don't have to restart the program after one response. Also, it prints out * the socket pair the server sees. Run the program via telnet several times and * compare the socket pairs. @@ -17,7 +17,7 @@ import java.net.*; * * telnet [ipNumberOfServerLaptop] 2317 * - * And have him display the socket pairs he got. + * And have the instructor display the socket pairs received. * * @author mcgredo */ @@ -31,7 +31,7 @@ public class TcpExample3Server { // Notice that it is outside the loop; ServerSocket // needs to be made only once. - System.out.println("TcpExample3Server has really started..."); // it helps debugging to put this on console first + System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first ServerSocket serverSocket = new ServerSocket(2317); // Server is up and waiting (i.e. "blocked" or paused) @@ -45,11 +45,10 @@ public class TcpExample3Server { OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); - ps.println("This was written by the server"); // this goes back to client! + ps.println("This message was produced by the server."); // this gets sent back to client! - // Print some information locally about the Socket - // connection. This includes the port and IP numbers - // on both sides (the socket pair.) + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress remoteAddress = clientConnection.getInetAddress(); @@ -60,8 +59,7 @@ public class TcpExample3Server { // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) // 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: (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))"); @@ -73,8 +71,12 @@ public class TcpExample3Server { clientConnection.close(); // like it or not, you're outta here! } } - catch (IOException e) { + catch (IOException e) + { System.out.println("problem with networking"); + // 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample4HandlerThread.java b/examples/src/TcpExamples/TcpExample4HandlerThread.java index 3a14d484f4064c2ec5a8b23bf61f4eb4958bcaca..8b51079dbf7d80ba3b894a9159b69ae6de78a626 100644 --- a/examples/src/TcpExamples/TcpExample4HandlerThread.java +++ b/examples/src/TcpExamples/TcpExample4HandlerThread.java @@ -8,7 +8,7 @@ import java.net.*; /** * Handles all the logic associated with one connection * by running in a thread of its own. This is the server - * portion as well, so we artifically invent what happens + * portion as well, so we artificially invent what happens * if the server can't respond to a connection for 10 sec. * * @author Don McGregor @@ -45,7 +45,7 @@ public class TcpExample4HandlerThread extends Thread OutputStream os = socket.getOutputStream(); PrintStream ps = new PrintStream(os); - final long TIMEOUT = 10000; // 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 Thread.sleep(TIMEOUT); // 10 seconds