diff --git a/assignments/ReportingForDuty.md b/assignments/ReportingForDuty.md index 39e8aef3f508fde6d6175350a63cad0199ce7b21..b9511a38f34445e00beeb1779fb1d05be59d54ab 100644 --- a/assignments/ReportingForDuty.md +++ b/assignments/ReportingForDuty.md @@ -7,16 +7,20 @@ Be sure to *git update* before, and be sure to *git commit* and *git push* after More information on your use of Git is in the parent directory [README.md](../../README.md) - Don Brutzman + --- + ### 2021 - Terry is here! +- Brian Pugh + - Kurt Reynolds - Rick Lentz -- Dom Hittner +- Max Schlessel - Nick Hittner @@ -32,6 +36,8 @@ More information on your use of Git is in the parent directory [README.md](../.. - John Allen +--- + ### 2020 - Bernd Weissenberger diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java index d925c7fdc0b4cb83e4cd3eec4a662be1c4b49a09..91f18ffa79d3cd8ba9126bd8e399d3b57b10d304 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java @@ -46,7 +46,7 @@ public class FisherTCPExample1Telnet { // across what is in fact a slow connection ps.flush(); } - System.out.println("TcpExample1 completed successfully."); + System.out.println("FisherTCPExample1 completed successfully."); } catch(IOException e) { diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java new file mode 100644 index 0000000000000000000000000000000000000000..fc40c12a19f53e231416efc2d81e898277a8e3c0 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java @@ -0,0 +1,103 @@ +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Very slightly more complex than example1. 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. + * + * <code>telnet localhost 2317</code> + * + * If you're sophisticated you can contact the instructor's computer + * while running this program. + * + * <code>telnet ipOfServersLaptop 2317</code> + * + * And have that machine display the socket pairs received. + * @author mcgredo + * @author brutzman + * @author McNeely + */ +public class McNeelyTCPExample2 +{ + public static void main(String[] args) + { + try + { + System.out.println("Justin McNeely's TcpExample2 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) to win..." ); + + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket needs to be made only once. + + int connectionCount = 0; // state variable + int totalEntrantCount = 31; // spoofed entrants + + ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. + // of interest: often client doesn't care what port it uses locally when connecting to that server port. + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + // blocks! then proceeds once a connection is "accept"ed + try (Socket clientConnection = serverSocket.accept()) { + connectionCount++; // got another one! + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server " + McNeelyTCPExample2.class.getName()); // to remote client + System.out.println("This server response was written by server " + McNeelyTCPExample2.class.getName()); // to server console + + ps.println("You have attempted " + connectionCount + "times, you are now aplicant number " + totalEntrantCount + " to win. Keep trying!"); + + totalEntrantCount = (totalEntrantCount + 24); + // 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(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different? + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6 + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + // + // Why is first IP/port the same, while the second set has different ports? + + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + System.out.println("got another connection! WINNING!, #" + connectionCount); // report progress + + // Notice the use of flush() and close(). Without + // the close() to Socket object may stay open for + // a while after the client has stopped needing this + // connection. Close() explicitly ends the connection. + ps.flush(); + } + } + } + catch(IOException e) + { + System.err.println("Problem with " + McNeelyTCPExample2.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) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting.java new file mode 100644 index 0000000000000000000000000000000000000000..47ccc375d2671a5cf86c545b8c2b77ff1c053698 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting.java @@ -0,0 +1,103 @@ +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.*; +import java.net.*; + +/** + * Very slightly more complex than example1. 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. + * + * <code>telnet localhost 2317</code> + * + * If you're sophisticated you can contact the instructor's computer while + * running this program. + * + * <code>telnet ipOfServersLaptop 2317</code> + * + * And have that machine display the socket pairs received. + * + * @author pugh + * @author brutzman + */ +public class PughTcp2ConnectionCounting { + + /** + * Program invocation, execution starts here + * + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + System.out.println("TcpExample2ConnectionCounting has started and is waiting for a connection."); + System.out.println("Get Ready To Network!!!"); + 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. + // Notice that it is outside the loop; ServerSocket needs to be made only once. + int connectionCount = 0; // state variable + + ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. + // of interest: often client doesn't care what port it uses locally when connecting to that server port. + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + // blocks! then proceeds once a connection is "accept"ed + try ( Socket clientConnection = serverSocket.accept()) { + connectionCount++; // got another one! + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server " + PughTcp2ConnectionCounting.class.getName()); // to remote client + System.out.println("This server response was written by server " + PughTcp2ConnectionCounting.class.getName() + "\n"); // to server console + if (connectionCount % 2 == 1) { + System.out.println("Go\n"); + } + if (connectionCount % 2 == 0) { + System.out.println("Gators!\n"); + } + if (connectionCount % 3 == 1 && connectionCount != 0) { + System.out.println("O'Doyle Rules!!! \n"); + } + 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.) + InetAddress localAddress = clientConnection.getLocalAddress(); + InetAddress remoteAddress = clientConnection.getInetAddress(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different? + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6 + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + // + // Why is first IP/port the same, while the second set has different ports? + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + System.out.println("got another connection, #" + connectionCount); // report progress + + // Notice the use of flush() and close(). Without + // the close() to Socket object may stay open for + // a while after the client has stopped needing this + // connection. Close() explicitly ends the connection. + ps.flush(); + } + } + } catch (IOException e) { + System.err.println("Problem with " + PughTcp2ConnectionCounting.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) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java new file mode 100644 index 0000000000000000000000000000000000000000..2758b2dcafecba78d0f32534a03d3aaa2f1934ba --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java @@ -0,0 +1,63 @@ +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author kReynolds + */ + + +public class ReynoldsTcpExample1Telnet1 { + 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); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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); + + ps.println("Client response was brought to you by the Domo"); // to remote clientnc + ps.println("DomsaMom"); + System.out.println("Client response was brought to you by the Domo"); // to server console + System.out.println("DomsaMom"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + if (e instanceof java.net.BindException) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java new file mode 100644 index 0000000000000000000000000000000000000000..608af41e7dee3bfa8a370716eb6e513b28aba79f --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java @@ -0,0 +1,60 @@ +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author mrobi + */ +public class RobinsonTcpExample1Telnet { + 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); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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); + + ps.println("Client response was written by Matt's server TcpExample1."); // to remote clientnc + ps.println("Can you hear me?"); + System.out.println("Client response was written by Matt's server TcpExample1."); // to server console + System.out.println("Yes"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with FisherTCPExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + if (e instanceof java.net.BindException) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/SchlesselTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/SchlesselTcpExample1Telnet.java new file mode 100644 index 0000000000000000000000000000000000000000..fa2565785de76a5753d4368ab87c75adce506373 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/SchlesselTcpExample1Telnet.java @@ -0,0 +1,65 @@ +package TcpExamples; + +import java.io.*; +import java.net.*; + +/** + * Homework 1 + * + * @author max schlessel + */ +public class SchlesselTcpExample1Telnet +{ + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String[] args) + { + try + { + System.out.println(SchlesselTcpExample1Telnet.class.getName() + " 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); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (Socket clientConnection = serverSocket.accept()) // wait here for a client to connect + { + // OK we got something, time to respond! + // 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); + + ps.println("This client response was written by Max's client " + SchlesselTcpExample1Telnet.class.getName()); // to remote client + ps.println("aka the shot heard round the world"); + System.out.println("This server response was written by Max's server " + SchlesselTcpExample1Telnet.class.getName()); // to server console + System.out.println("boom"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println(SchlesselTcpExample1Telnet.class.getName() + " completed successfully."); + } + catch(IOException e) + { + System.err.println("Exception with " + SchlesselTcpExample1Telnet.class.getName() + " networking:"); // describe what is happening + System.err.println(e); + // Provide more helpful information to user if exception occurs due to running twice at one time + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + if (e instanceof java.net.BindException) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherClient.java b/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherClient.java new file mode 100644 index 0000000000000000000000000000000000000000..c3d46c8d3cc389f0688a61b070d226fbb66394a5 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherClient.java @@ -0,0 +1,89 @@ +package MV3500Cohort2021JulySeptember.homework2.Fisher; + +//import static MV3500Cohort2020JulySeptember.homework2.White.WhiteClient.LOCALHOST; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.Socket; + +/** + * Before, we always used telnet (netcat) to connect to the server. Here we are + * now writing our own program to do the connection. + * + * As you will see, when we run this after we start the server we will see the + * same string telnet printed, sent by the server. The output at the server will + * show different socket pairs for each time the loop iterates. + * + * @author adfis + */ +public class FisherClient { + + // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String[] args) { + + // Local variables/fields + Socket socket; + InputStream is; + InputStreamReader isr; + BufferedReader br; + OutputStream os; + PrintStream ps; + String serverMessage; + int clientLoopCount = 0; + + try { + System.out.println("FisherClient creating socket..."); + while (true) { + + // We request an IP to connect to ("localhost") and + // port number at that IP (2317). This establishes + // a connection to that IP in the form of a Socket + // object; the server uses a ServerSocket to wait for + // connections. + socket = new Socket(LOCALHOST, 2317); // locohost? + + os = socket.getOutputStream(); + ps = new PrintStream(os); + + // Now hook everything up (i.e. set up the streams), Java style: + is = socket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + serverMessage = br.readLine(); + System.out.println("=================================================="); + + System.out.println("The message the server sent was: '" + serverMessage + "'"); + System.out.println("FisherClient responds with: To get to the other side"); + // socket gets closed, either automatically/silently by this code (or possibly by the server) + + } // end while(true) + } catch (IOException e) { + System.err.println("Problem with FisherClient 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) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } finally // occurs after any other activity when shutting down + { + // program exit: tell somebody about that happening. Likely cause: server drops connection. + System.out.println(); + System.out.println("FisherClient exit"); + } + } + +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherServer.java b/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherServer.java new file mode 100644 index 0000000000000000000000000000000000000000..82c781df98da0bcf2779f861ca4f12fa07d22ad8 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework2/Fisher/FisherServer.java @@ -0,0 +1,113 @@ +package MV3500Cohort2021JulySeptember.homework2.Fisher; + +import static com.google.common.base.CharMatcher.is; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Very slightly more complex than example1, further modifying example2. 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. + * + * telnet (nc) localhost 2317 + * + * If you're sophisticated you can contact the instructor's computer while + * running this program. + * + * telnet (nc) [ipNumberOfServerLaptop] 2317 + * + * and have the instructor display the socket pairs received. + * + * @author adfis + */ +public class FisherServer { + + /** + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + + // 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("FisherServer has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2317); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + int localPort, remotePort; + + InputStream is; + InputStreamReader isr; + BufferedReader br; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + + // block until connected to a client + try ( Socket clientConnectionSocket = serverSocket.accept()) { + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnectionSocket.getOutputStream(); + ps = new PrintStream(os); + ps.println("Why did the chicken cross the road?"); // 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). + localAddress = clientConnectionSocket.getLocalAddress(); + remoteAddress = clientConnectionSocket.getInetAddress(); + localPort = clientConnectionSocket.getLocalPort(); + remotePort = clientConnectionSocket.getPort(); + + + + // My socket pair connection looks like this, to localhost: + // 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? + System.out.println("FisherServer socket pair showing host name, address, port:"); + System.out.println(" (( " + + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + + is = clientConnectionSocket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + if (localAddress.getHostName().equals(localAddress.getHostAddress()) + || remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) { + System.out.println(" note HostName matches address if host has no DNS name"); + } + + // Notice the use of flush() and try w/ resources. Without + // the try w/ resources the Socket object may stay open for + // a while after the client has stopped needing this + // connection. try w/ resources explicitly ends the connection. + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with FisherServer 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.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } + +} diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java index aa798ba67e5895baed22ff3f051b015b82189b38..3127dc0d83404980fdcaf9a7d50120cd50b510fe 100644 --- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java +++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java @@ -16,9 +16,104 @@ import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; -/** The purpose of this program is to provide an easily modifiable example simulation program. */ +/** The purpose of this program is to provide an easily modifiable example simulation program + * that includes DIS-capable entities doing tasks and reporting them. */ public class ExampleSimulationProgram { + /** + * runSimulation is for you! This block is programmer-modifiable method for defining and running a new simulation of interest. + * Support include DIS EntityStatePdu, FirePdu and CommentPdu all available for + * modification and sending in a simulation loop. + */ + @SuppressWarnings("SleepWhileInLoop") + public void runSimulation () + { + try + { + + + final double LOOP_DURATION_SECONDS = 1.0; // seconds + final int MAX_LOOP_COUNT = 10; + int loopCount = 0; + VariableRecordType narrativeType = VariableRecordType.OTHER; // of potential use + boolean simulationComplete = false; // sentinel variable as termination condition + + // TODO reset clock to zero each time for consistent outputs. + + // your model setup: who's who in this zoo? + // create PDU objects and set their values + + EntityID entityID_1 = new EntityID(); + entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID + + EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu(); + entityStatePdu.setEntityID(entityID_1); + + FirePdu firePdu = pduFactory.makeFirePdu(); + // should we customize this munition? what is it for your simulation? + + // loop the simulation while allowed, programmer can set additional conditions to break out and finish + while (loopCount < MAX_LOOP_COUNT) + { + String narrativeMessage1, narrativeMessage2, narrativeMessage3; + // initialize loop variables + loopCount++; + + // ============================================================================================= + // your own simulation code starts here! + + // compute a track, update an ESPDU, whatever it is that your model is doing... + + // Where is my entity? + entityStatePdu.getEntityLocation().setX(entityStatePdu.getEntityLocation().getX() + 1.0); // 1m per timestep + + // decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoort at! + + // etc. etc. your code goes here + + // something happens between my simulation entities, la de da de da... + System.out.println ("... My simulation just did something, no really..."); + + + // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) + narrativeMessage1 = "MV3500 ExampleSimulationProgram"; + narrativeMessage2 = "runSimulation() loop " + loopCount; + narrativeMessage3 = ""; // intentionally blank for testing + + // your loop termination condition goes here + if (loopCount > 4) // for example + { + simulationComplete = true; + } + // your own simulation code is finished here! + // ============================================================================================= + + // keep track of timestep: wait duration for elapsed time in this loop + // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes + Thread.sleep((long)(LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds + System.out.println ("... Pausing for " + LOOP_DURATION_SECONDS + " seconds"); + + // send the status PDUs for this loop and continue + System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); + sendAllPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); + System.out.println ("... PDUs successfully sent"); + + // =============================== + // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue + if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good + { + System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); + break; + } + } // end of while loop + } + catch (InterruptedException iex) // handle any exception that your code might choose to provoke! + { + Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, iex); + } + } + /* **************************** infrastructure code, modification is seldom needed ************************* */ + /** * Output prefix to identify this class, helps with logging */ @@ -39,7 +134,7 @@ public class ExampleSimulationProgram */ public ExampleSimulationProgram() { - // Under consideration. Constructor is not currently needed. + // Constructor is under consideration. Constructor is not currently needed. } /** @@ -205,96 +300,4 @@ public class ExampleSimulationProgram System.out.println(TRACE_PREFIX + "complete."); // report successful completion } - - /** - * Programmer-modifiable method for defining and running a new simulation of interest. - * Support include DIS EntityStatePdu, FirePdu and CommentPdu all available for - * modification and sending in a simulation loop. - */ - @SuppressWarnings("SleepWhileInLoop") - public void runSimulation () - { - try - { - - - final double LOOP_DURATION_SECONDS = 1.0; // seconds - final int MAX_LOOP_COUNT = 10; - int loopCount = 0; - VariableRecordType narrativeType = VariableRecordType.OTHER; // of potential use - boolean simulationComplete = false; // sentinel variable as termination condition - - // TODO reset clock to zero each time for consistent outputs. - - // your model setup: who's who in this zoo? - // create PDU objects and set their values - - EntityID entityID_1 = new EntityID(); - entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID - - EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu(); - entityStatePdu.setEntityID(entityID_1); - - FirePdu firePdu = pduFactory.makeFirePdu(); - // should we customize this munition? what is it for your simulation? - - while (loopCount < MAX_LOOP_COUNT) // loop the simulation while allowed, can set additional conditions to break - { - String narrativeMessage1, narrativeMessage2, narrativeMessage3; - // initialize loop variables - loopCount++; - - // ============================================================================================= - // your own simulation code starts here! - - // compute a track, update an ESPDU, whatever it is that your model is doing... - - // Where is my entity? - entityStatePdu.getEntityLocation().setX(entityStatePdu.getEntityLocation().getX() + 1.0); // 1m per timestep - - // decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoort at! - - // etc. etc. your code goes here - - - - - - // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) - narrativeMessage1 = "MV3500 ExampleSimulationProgram"; - narrativeMessage2 = "runSimulation() loop " + loopCount; - narrativeMessage3 = ""; // intentionally blank for testing - - // your loop termination condition goes here - if (loopCount > 4) // for example - { - simulationComplete = true; - } - // your own simulation code is finished here! - // ============================================================================================= - - // keep track of timestep: wait duration for elapsed time in this loop - // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes - Thread.sleep((long)(LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds - System.out.println ("... Pausing for " + LOOP_DURATION_SECONDS + " seconds"); - - // send the status PDUs for this loop and continue - System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); - sendAllPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); - System.out.println ("... PDUs successfully sent"); - - // =============================== - // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue - if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good - { - System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); - break; - } - } // end of while loop - } - catch (InterruptedException iex) // handle any exception that your code might choose to provoke! - { - Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, iex); - } - } } diff --git a/examples/src/Simkit/README.md b/examples/src/Simkit/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b68e2b837a30c48d2c446855de4b715103c78e76 --- /dev/null +++ b/examples/src/Simkit/README.md @@ -0,0 +1,8 @@ +# Simkit DIS examples + +This directory will hold a simple pair of basic Simkit programs. + +We will then make a pair of DIS-capable versions corresponding these same programs. + +Design goal: keep any DIS code to be entity oriented, +with focus of the servers to be centered on the production/handling/consumption of entities. diff --git a/examples/src/Simkit/SimkitSimpleDiscreteEventSimulationModelForDIS.docx b/examples/src/Simkit/SimkitSimpleDiscreteEventSimulationModelForDIS.docx new file mode 100644 index 0000000000000000000000000000000000000000..9b53790d0458594f1553db9b99c9cc03bf9b9096 Binary files /dev/null and b/examples/src/Simkit/SimkitSimpleDiscreteEventSimulationModelForDIS.docx differ diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index 988aac7e8f7a288b1ea201bf208a239c5748d999..c1b62609c7d5c46014b06f0ce7db5b8f24e36be9 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java +++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java @@ -33,7 +33,10 @@ public class TcpExample2ConnectionCounting { 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)..." ); + System.out.println(" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)"); + System.out.println(" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or" ); + System.out.println(" Windows enter (telnet IPv4_Address 2317) or Mac enter (nc IPv4_Address 2317) for LAN operation" ); + System.out.println("TcpExample2ConnectionCounting server standing by..." ); // ServerSocket waits for a connection from a client. // Notice that it is outside the loop; ServerSocket needs to be made only once. @@ -42,18 +45,33 @@ public class TcpExample2ConnectionCounting ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. // of interest: often client doesn't care what port it uses locally when connecting to that server port. - + // Loop, infinitely, waiting for client connections. // Stop the program somewhere else. while(true) { - // blocks! then proceeds once a connection is "accept"ed + // serverSocket.accept() blocks! then proceeds once a connection is "accept"ed try (Socket clientConnection = serverSocket.accept()) { connectionCount++; // got another one! OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); +// if (connectionCount == 1) // first time through, report connection +// { +// // Where are we? In other words, what is our host number? Advertise it. +// // Note that we use the serverSocket to get address, since host may have multiple network connections. +// // https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java +// String localHostAddress = clientConnection.getInetAddress().getHostAddress(); +// System.out.println("Local host address is " + localHostAddress); +// if (localHostAddress.contains("/")) +// localHostAddress = localHostAddress.substring(localHostAddress.indexOf("/")+1); +// // show localhost IP number to facilitate connections over local area network (LAN, WAN) +// System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317) for local operation" ); +// System.out.println(" enter (nc " + localHostAddress + " 2317) or " + +// "(telnet " + localHostAddress + " 2317)..." ); +// } + 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 @@ -74,7 +92,7 @@ public class TcpExample2ConnectionCounting // // Why is first IP/port the same, while the second set has different ports? - System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + System.out.println("Socket pair (server, client): (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))"); System.out.println("got another connection, #" + connectionCount); // report progress diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCountingTerminalLog.txt b/examples/src/TcpExamples/TcpExample2ConnectionCountingTerminalLog.txt index 5daf91804f3c0dd2517476281c77ac9b6420e7de..e7324dfea05687f0e3e4130afb6a058a3a994d2d 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCountingTerminalLog.txt +++ b/examples/src/TcpExamples/TcpExample2ConnectionCountingTerminalLog.txt @@ -1,47 +1,46 @@ Invocation instructions: -1. run/debug TcpExample2.java -2. console: nc localhost 2317 - alternate: telnet localhost 2217 +1. run/debug TcpExample2ConnectionCounting.java + +2. Windows: ipconfig + MacOS: ifconfig + +3. Windows: telnet localhost 2317 + MacOS: nc localhost 2317 Program responses: =================================================== -run: -TcpExample2ConnectionCounting has started and is waiting for a connection: telnet localhost 2317 -This server response was written by server TcpExample2ConnectionCounting -Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57630 )) +run-single: +TcpExample2ConnectionCounting has started and is waiting for a connection. + help: https://savage.nps.edu/Savage/developers.html#telnet + Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address) + Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or + Windows enter (telnet IPv4_Address 2317) or Mac enter (nc IPv4_Address 2317) for LAN operation +TcpExample2ConnectionCounting server standing by... +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55424 )) got another connection, #1 -This server response was written by server TcpExample2ConnectionCounting -Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57631 )) +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55425 )) got another connection, #2 -This server response was written by server TcpExample2ConnectionCounting -Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57633 )) +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55426 )) got another connection, #3 +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55427 )) +got another connection, #4 +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55428 )) +got another connection, #5 +This server response was written by server TcpExamples.TcpExample2ConnectionCounting +Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.154.38, 49308 )) +got another connection, #6 =================================================== -don@it154928 /cygdrive/c/Program Files/NetBeans 8.2 $ telnet localhost 2317 -Trying ::1... -Connected to localhost. -Escape character is '^]'. -This client response was written by server TcpExample2 -You were connection #1, by my count -Connection closed by foreign host. -don@it154928 /cygdrive/c/Program Files/NetBeans 8.2 -$ telnet localhost 2317 -Trying ::1... -Connected to localhost. -Escape character is '^]'. -This client response was written by server TcpExample2 -You were connection #2, by my count -Connection closed by foreign host. - -don@it154928 /cygdrive/c/Program Files/NetBeans 8.2 -$ telnet localhost 2317 -Trying ::1... -Connected to localhost. -Escape character is '^]'. -This client response was written by server TcpExample2 -You were connection #3, by my count -Connection closed by foreign host. +This client response was written by server TcpExamples.TcpExample2ConnectionCounting +You were connection #6, by my count + +Connection to host lost. +=================================================== \ No newline at end of file diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java index 94ca31012128a28f2253a854ac7cdeb6fa812e36..e230a0a88a73f186027806f55b5c294fca0adbca 100644 --- a/examples/src/TcpExamples/TcpExample3Client.java +++ b/examples/src/TcpExamples/TcpExample3Client.java @@ -65,7 +65,7 @@ public class TcpExample3Client { System.out.println("The message the server sent was: '" + serverMessage + "'"); // socket gets closed, either automatically/silently by this code (or possibly by the server) - Thread.sleep(500l); // slow things down, for example 500l (long) = 500 msec + Thread.sleep(500l); // slow things down, for example 500l (long) = 500 msec (1/2 second) } // end while(true) // infinite loops are dangerous, be sure to kill this process! } diff --git a/presentations/00_ConfiguringJavaNetbeansGit.pdf b/presentations/00_ConfiguringJavaNetbeansGit.pdf index 04646de71d84674ffcb950b43388915ea0e94d44..f8a54eadac67d32b24bd4def771bb596df5dfc47 100644 Binary files a/presentations/00_ConfiguringJavaNetbeansGit.pdf and b/presentations/00_ConfiguringJavaNetbeansGit.pdf differ diff --git a/presentations/00_ConfiguringJavaNetbeansGit.pptx b/presentations/00_ConfiguringJavaNetbeansGit.pptx index 465c37a8abb188328af3e03100d5dc6dd430fcb9..a71e57da845af97ae6c69ba9d8333996ff883db2 100644 Binary files a/presentations/00_ConfiguringJavaNetbeansGit.pptx and b/presentations/00_ConfiguringJavaNetbeansGit.pptx differ diff --git a/presentations/03_TCPIP.pptx b/presentations/03_TCPIP.pptx index 65fa0684b829ef545e9626f986cc29aaa0eb0c4d..cbe387103bf98c12d2381cf7cb43fe7a22ba5e81 100644 Binary files a/presentations/03_TCPIP.pptx and b/presentations/03_TCPIP.pptx differ diff --git a/presentations/04_TCPSocketsJava.pptx b/presentations/04_TCPSocketsJava.pptx index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..23ca879f0de227bc27de42d6138ed38860fa7b5c 100644 Binary files a/presentations/04_TCPSocketsJava.pptx and b/presentations/04_TCPSocketsJava.pptx differ