diff --git a/assignments/ReportingForDuty.md b/assignments/ReportingForDuty.md index 58d92923cbeb8a6b387c7be5b835e809399c7688..39e8aef3f508fde6d6175350a63cad0199ce7b21 100644 --- a/assignments/ReportingForDuty.md +++ b/assignments/ReportingForDuty.md @@ -7,11 +7,11 @@ 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! + - Kurt Reynolds - Rick Lentz @@ -24,10 +24,12 @@ More information on your use of Git is in the parent directory [README.md](../.. - Alex Fisher ---John Morris +- John Morris - Max Schlessel +- Justin McNeely + - John Allen ### 2020 diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java index 483b40bf6e0256e6fd6486ebb3d5917e21040dd0..d925c7fdc0b4cb83e4cd3eec4a662be1c4b49a09 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java @@ -1,64 +1,64 @@ -package MV3500Cohort2021JulySeptember.homework1; - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author adfis - */ - - -public class FisherTCPExample1Telnet { - 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 Alex' server TcpExample1."); // to remote clientnc - ps.println("We are crushing this assignment, good work."); - System.out.println("Server response was written by Alex' server TcpExample1."); // to server console - System.out.println("SUCCESS!"); - - // "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!"); - } - } -} - +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author adfis + */ + + +public class FisherTCPExample1Telnet { + 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 Alex' server TcpExample1."); // to remote clientnc + ps.println("We are crushing this assignment, good work."); + System.out.println("Server response was written by Alex' server TcpExample1."); // to server console + System.out.println("SUCCESS!"); + + // "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/FrankTCPExample3Client.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java new file mode 100644 index 0000000000000000000000000000000000000000..67275fa64d71b19b353d4a84947fa57fab80d2df --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java @@ -0,0 +1,96 @@ +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.Socket; + +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +/** + * + * @author justi + */ +public class FrankTCPExample3Client { + + /** + */ + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + /** + * Program invocation, execution starts here + * + * @param args command-line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + + // Local variables/fields + Socket socket = null; + InputStream is; + Reader isr; + BufferedReader br; + String serverMessage; + int clientLoopCount = 0; + + try { + while (true) { + clientLoopCount++; // increment at beginning of loop for reliability + System.out.println(FrankTCPExample3Client.class.getName() + " creating socket..."); + + // 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, 2318); // locohost? + + // 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.print("Client loop " + clientLoopCount + ": "); + System.out.println("now we're talking!"); + System.out.println("The message the server sent was: '" + serverMessage + "'"); + // socket gets closed, either automatically/silently by this code (or possibly by the server) + if (serverMessage.equals("this is good bye message from the server")) { //if client recieved termanation message stop client + break; + } + Thread.sleep(1000); // turned it down to 1 second + + } // end while(true) // infinite loops are dangerous, be sure to kill this process! + } catch (IOException e) { + System.err.println("Problem with " + FrankTCPExample3Client.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!"); + } + } finally // occurs after any other activity when shutting down + { + try { + if (socket != null) { + socket.close(); + } + } catch (IOException e) { + } + + // program exit: tell somebody about that happening. Likely cause: server drops connection. + System.out.println(); + System.out.println("FrankTCPExample3Client.class.getName() + exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.JPG b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.JPG new file mode 100644 index 0000000000000000000000000000000000000000..a2652f97a08b14422f64213a8f3d0050257e8563 Binary files /dev/null and b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.JPG differ diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java new file mode 100644 index 0000000000000000000000000000000000000000..45c8aa10fe6e56ddac91ec864216aac7eb93cafd --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java @@ -0,0 +1,107 @@ +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.*; +import java.net.*; + +/** + * 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 2318 + * + * 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 mcgredo + * @author brutzman + */ +public class FrankTCPExample3Server { + + /** + * Program invocation, execution starts here If already compiled, can run + * using console in directory ../../build/classes/ by invoking \ java + * -classpath . TcpExamples.TcpExample3Server + * + * @param args command-line arguments + */ + public static void main(String[] args) throws InterruptedException { + 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(FrankTCPExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2318); // changed from 2317 to 2318 + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + int localPort, remotePort; + int serverLoopCount = 0; + + // 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()) { + serverLoopCount++; // increment at beginning of loop for reliability + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnectionSocket.getOutputStream(); + ps = new PrintStream(os); + if (serverLoopCount <= 20) { // checking if the loop count <= 20 + ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client! + } else { + ps.println("this is good bye message from the server"); // termination after 20 messages + break; // Stop server + } + // 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(); + + System.out.print("Server loop " + serverLoopCount + ": "); + + // 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(FrankTCPExample3Server.class.getName() + " socket pair showing host name, address, port:"); + System.out.println(" (( " + + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + + if (localAddress.getHostName().equals(localAddress.getHostAddress()) + || remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) { + System.out.println(" note HostName matches address if host has no DNS name"); + } + + // Not/*i*/ce 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 " + FrankTCPExample3Server.class.getName() + " networking: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3ServerClient.JPG b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3ServerClient.JPG new file mode 100644 index 0000000000000000000000000000000000000000..3a5853becfde3cdc97b73849da1f168963efe360 Binary files /dev/null and b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3ServerClient.JPG differ diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java new file mode 100644 index 0000000000000000000000000000000000000000..3ba4381f1ad60668101abdd5a16b4a0283e5f2cd --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.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 adfis + */ + + +public class HittnerDomTcpExample1Telnet { + 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/HittnerNickTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.java new file mode 100644 index 0000000000000000000000000000000000000000..495dcb25294407aa06f1e22268a6eb25ebdfdbd7 --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.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 adfis + */ + + +public class HittnerNickTcpExample1Telnet { + 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 Highlander"); // to remote clientnc + ps.println("There Can Be Only One"); + System.out.println("Client response was brought to you by the Highlander"); // to server console + System.out.println("There Can be Only One"); + + // "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/LeckieTcpExample2.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/LeckieTcpExample2.java new file mode 100644 index 0000000000000000000000000000000000000000..037e875c50c1d270f877119b837a3e2cda1ca00f --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/LeckieTcpExample2.java @@ -0,0 +1,103 @@ +package pkg3500.leckie; + +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 leckie + */ +public class LeckieTcpExample2 +{ + public static void main(String[] args) + { + 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. + // 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 " + LeckieTcpExample2.class.getName()); // to remote client + System.out.println("This server response was written by server " + LeckieTcpExample2.class.getName()); // to server console + + ps.println("This is your lottery attempt number #" + connectionCount + ", you are aplicant number " + totalEntrantCount + " to try and win. Keep trying!"); + + totalEntrantCount = (totalEntrantCount + 79); + // 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 " + LeckieTcpExample2.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/TcpExample2_Leckie_Mod_Hw1.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/TcpExample2_Leckie_Mod_Hw1.java new file mode 100644 index 0000000000000000000000000000000000000000..6fed11d3d705393f355c5db404295fd373f5159c --- /dev/null +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/TcpExample2_Leckie_Mod_Hw1.java @@ -0,0 +1,103 @@ +package pkg3500.leckie; + +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 leckie + */ +public class TcpExample2_Leckie_Mod_Hw1 +{ + public static void main(String[] args) + { + 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. + // 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 " + TcpExample2_Leckie_Mod_Hw1.class.getName()); // to remote client + System.out.println("This server response was written by server " + TcpExample2_Leckie_Mod_Hw1.class.getName()); // to server console + + ps.println("This is your lottery attempt number #" + connectionCount + ", you are aplicant number " + totalEntrantCount + " to try and win. Keep trying!"); + + totalEntrantCount = (totalEntrantCount + 79); + // 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 " + TcpExample2_Leckie_Mod_Hw1.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!"); + } + } + +}