diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java new file mode 100644 index 0000000000000000000000000000000000000000..9d3bbdb3dff6be1f4579c5a1563c428cd065203c --- /dev/null +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java @@ -0,0 +1,78 @@ +package MV3500Cohort2019JulySeptember.homework1; + +import java.io.*; +import java.net.*; +import java.sql.Timestamp; + +/** + * The simplest possible TCP network program. It listens for + * a connection, from telnet (telnet localhost 2317) or a program + * you write, which we will do later. Right now the TcpExample simply + * writes a string in response to a connection. + * + * Modifying his program is the basis for Assignment 1. + * + * Testing the running server program from telnet looks like this: + * + * it154916:projects mcgredo$ telnet localhost 2317 + * Trying ::1... + * Connected to localhost. + * Escape character is '^]'. + * This was written by the server + * Connection closed by foreign host. + * + * Notice that "This was written by the server" matches + * what is written by the code below, over the output stream. + * + * After this first connection the program below drops out + * the bottom of the program, and does not repeat itself. + * The program exits. + * + * @author mcgredo, brutzman`` + */ +public class BoronTcpExample1Telnet1 +{ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + Socket clientConnection = serverSocket.accept(); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + + ps.println("This client response was written by Capt. Jonathan Boron."); // to remote clientnc + ps.println("Connection achieved at time: " + timestamp); + + System.out.println("This server response was written by Capt. Jonathan Boron."); // to server console + System.out.println("Connection achieved at time: " + timestamp); + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + + clientConnection.close(); + System.out.println("Handshake completed successfully."); + } + 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."); + } + } +} 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/nbproject/configs/TcpExample1Telnet.properties b/examples/nbproject/configs/TcpExample1Telnet.properties new file mode 100644 index 0000000000000000000000000000000000000000..b64553bb1922ff82a93a92e9ff1d8fd9843ec2eb --- /dev/null +++ b/examples/nbproject/configs/TcpExample1Telnet.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample1Telnet diff --git a/examples/nbproject/configs/TcpExample2ConnectionCounting.properties b/examples/nbproject/configs/TcpExample2ConnectionCounting.properties new file mode 100644 index 0000000000000000000000000000000000000000..abf1c3aa3e640ceec4e2af7e8c7016b03c93cba9 --- /dev/null +++ b/examples/nbproject/configs/TcpExample2ConnectionCounting.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample2ConnectionCounting diff --git a/examples/nbproject/configs/TcpExample3Client.properties b/examples/nbproject/configs/TcpExample3Client.properties new file mode 100644 index 0000000000000000000000000000000000000000..9827f780ad8fab70650fcd16c4014aa11a2bac25 --- /dev/null +++ b/examples/nbproject/configs/TcpExample3Client.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample3Client diff --git a/examples/nbproject/configs/TcpExample3Server.properties b/examples/nbproject/configs/TcpExample3Server.properties new file mode 100644 index 0000000000000000000000000000000000000000..60e0c036131310c84f2c31dab7323c20c67b6bcd --- /dev/null +++ b/examples/nbproject/configs/TcpExample3Server.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample3Server diff --git a/examples/nbproject/configs/TcpExample4Client.properties b/examples/nbproject/configs/TcpExample4Client.properties new file mode 100644 index 0000000000000000000000000000000000000000..dbf38b30f19d508ef51cbfd3fc0e22c1a0b58d12 --- /dev/null +++ b/examples/nbproject/configs/TcpExample4Client.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample4Client diff --git a/examples/nbproject/configs/TcpExample4ThreadServer.properties b/examples/nbproject/configs/TcpExample4ThreadServer.properties new file mode 100644 index 0000000000000000000000000000000000000000..66983caea8e32d1d2c2e5c87ac5486788bd0bd6a --- /dev/null +++ b/examples/nbproject/configs/TcpExample4ThreadServer.properties @@ -0,0 +1 @@ +main.class=TcpExamples.TcpExample4ThreadServer diff --git a/examples/nbproject/project.properties b/examples/nbproject/project.properties index b86333722a5f49b0e70be3e8a21edfbc052f2402..64f4e603617c8d481d99c8c2e7dd602ffe4fc672 100644 --- a/examples/nbproject/project.properties +++ b/examples/nbproject/project.properties @@ -60,6 +60,7 @@ javac.test.processorpath=\ javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} +javadoc.html5=false javadoc.noindex=false javadoc.nonavbar=false javadoc.notree=false diff --git a/examples/src/TcpExamples/TcpExample1Telnet.java b/examples/src/TcpExamples/TcpExample1Telnet.java index 373c0f58ebb4345af3c4dcdeb79f1206ffb11706..074e545611945ef692b52f395c4d0496489b7837 100644 --- a/examples/src/TcpExamples/TcpExample1Telnet.java +++ b/examples/src/TcpExamples/TcpExample1Telnet.java @@ -64,7 +64,11 @@ public class TcpExample1Telnet } catch(IOException e) { - System.out.println("problem with networking: " + e); + System.out.println("Problem with TcpExample1Telnet 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index 897fe538f7ec8c0c03a63e75776d659decb76074..6dfcf88e81c6ba9f73361c6fa9ee83e6d01eb0d1 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. @@ -83,7 +84,11 @@ public class TcpExample2ConnectionCounting } catch(IOException e) { - System.out.println("problem with networking: " + e); + System.out.println("Problem with TcpExample2ConnectionCounting 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java index 87e44dae4ce5de6b80532253e6d2c109ad20e121..2824b80b4b8ac7d5a267e2c753c02a28b6e10a56 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); + System.out.println("Problem with TcpExample3ServerClient 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!"); } // 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..92e5db879863187aaabe9ae0bad24174587112fd 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) { - System.out.println("problem with networking"); + catch (IOException e) + { + System.out.println("Problem with TcpExample3Server 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/TcpExample4Client.java b/examples/src/TcpExamples/TcpExample4Client.java index 29b996c84214dcc0463c88214dfa7bede9c463b5..c5049c45374e5ba9b3a45d775a6ff8d5a0c59d25 100644 --- a/examples/src/TcpExamples/TcpExample4Client.java +++ b/examples/src/TcpExamples/TcpExample4Client.java @@ -45,7 +45,7 @@ public class TcpExample4Client long readTime = System.currentTimeMillis(); long timeLength = readTime - startTime; - System.out.println("TcpExample4Client: message received from server=" + serverMessage); + System.out.println("TcpExample4Client: message received from server='" + serverMessage + "'"); System.out.println("TcpExample4Client: time msec required for read=" + timeLength); System.out.println("=================================================="); // To push this further, launch multiple copies of TcpExample4Client simultaneously @@ -54,8 +54,11 @@ public class TcpExample4Client } catch(IOException e) { - System.out.println("Problem with TcpExample4Client, see exception trace:"); - System.out.println(e); + System.out.println("Problem with TcpExample4Client 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) + System.out.println("*** Be sure to stop any other running instances of programs using this port!"); } } } \ No newline at end of file diff --git a/examples/src/TcpExamples/TcpExample4HandlerThread.java b/examples/src/TcpExamples/TcpExample4HandlerThread.java index 3a14d484f4064c2ec5a8b23bf61f4eb4958bcaca..8518f02a8066111ab52396850d499dd939f0464b 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,19 +45,22 @@ 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 - ps.println("This was written by the server TcpExample4HandlerThread"); + ps.println("This message was written by the server TcpExample4HandlerThread"); ps.flush(); socket.close(); System.out.println("TcpExample4HandlerThread finished handling a thread, now exit."); } catch(IOException | InterruptedException e) // either a networking or a threading problem { - System.out.println("Problem with TcpExample4HandlerThread, see exception trace:"); - System.out.println(e); + System.out.println("Problem with TcpExample4HandlerThread 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!"); } } } diff --git a/examples/src/TcpExamples/TcpExample4ThreadServer.java b/examples/src/TcpExamples/TcpExample4ThreadServer.java index 8d2c90eae22b0e08239f65e503877cd39614103c..131b0e397b5c1dfbc283b212643a4907c98bbc1c 100644 --- a/examples/src/TcpExamples/TcpExample4ThreadServer.java +++ b/examples/src/TcpExamples/TcpExample4ThreadServer.java @@ -34,8 +34,11 @@ public class TcpExample4ThreadServer { } catch(IOException e) { - System.out.println("Problem with TcpExample4ThreadServer, see exception trace:"); + System.out.println("Problem with TcpExample4ThreadServer 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!"); } System.out.println("============================================================="); }