diff --git a/assignments/ReportingForDuty.md b/assignments/ReportingForDuty.md index 7d3b4faff9f7c28496cf83c42040e13d7aad0419..b9511a38f34445e00beeb1779fb1d05be59d54ab 100644 --- a/assignments/ReportingForDuty.md +++ b/assignments/ReportingForDuty.md @@ -8,13 +8,14 @@ More information on your use of Git is in the parent directory [README.md](../.. - Don Brutzman -- Brian Pugh - --- + ### 2021 - Terry is here! +- Brian Pugh + - Kurt Reynolds - Rick Lentz @@ -35,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/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 b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting new file mode 100644 index 0000000000000000000000000000000000000000..492e78f857cc84ca5d90e986e9ba2cb65c5b6538 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting @@ -0,0 +1,103 @@ +package MV3500Cohort2021JulySeptember.homework1.Pugh; + +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/PughTcp2ConnectionCounting.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting.java new file mode 100644 index 0000000000000000000000000000000000000000..492e78f857cc84ca5d90e986e9ba2cb65c5b6538 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/PughTcp2ConnectionCounting.java @@ -0,0 +1,103 @@ +package MV3500Cohort2021JulySeptember.homework1.Pugh; + +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/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!"); + } + } +}