diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/README.md b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8dac0233135447e37e325fa45c31a1bbccf39741 --- /dev/null +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/README.md @@ -0,0 +1 @@ +My Assignment #1 diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/TcpExample1TelnetYu.java b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/TcpExample1TelnetYu.java new file mode 100644 index 0000000000000000000000000000000000000000..22d370792b4c79161cde63d528e902b2d63aa753 --- /dev/null +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/TcpExample1TelnetYu.java @@ -0,0 +1,114 @@ +package MV3500Cohort2024JulySeptember.homework1.Yu; + +import src.TcpExamples.*; +import java.io.*; +import java.net.*; + +/** + * <p> + * The simplest possible TCP network program, opening a socket and waiting for a reply. + * It listens for any socket connection response, either from telnet (telnet localhost 2317) + * or another program that you write (which we will do later). + * Once a socket connection is established, TcpExample1Telnet simply + * writes a string in response to that connection and finishes, exiting. + * + * <p> + * As an alternative to running the Windows (or other operating system) console, + * you can instead run the NetBeans terminal window. If you are on Windows, + * NetBeans is looking for Cgwin installation (for Unix-like compatibility) + * with details at <a href="https://savage.nps.edu/Savage/developers.html#Cygwin" target="blank">Savage Developers Guide: Cygwin</a>. + * Modifying this program is the basis for Assignment 1. + * </p> + * + * <p> + * Testing the running server program from telnet looks like this: + * </p> + * <pre> + * it154916:projects mcgredo$ <b>telnet localhost 2317</b> + * Trying ::1... + * Connected to localhost. + * Escape character is '^]'. + * This was written by the server + * Connection closed by foreign host. + * </pre> + * <p> + * Notice that "This was written by the server" matches + * what is written by the code below, over the output stream. + * </p> + * + * <p> + * After this first connection the program below drops to + * the bottom of the method, and does not repeat itself. + * In other words, the program exits. + * </p> + * + * @see <a href="../../../src/TcpExamples/TcpExample1TerminalLog.txt" target="_blank">TcpExample1TerminalLog.txt</a> + * @see <a href="../../../src/TcpExamples/TcpExample1NetBeansConsoleTelnet.png" target="_blank">TcpExample1NetBeansConsoleTelnet.png</a> + * @see <a href="../../../src/TcpExamples/TcpExample1NetBeansConsoleTelnet.pdf" target="_blank">TcpExample1NetBeansConsoleTelnet.pdf</a> + * @see <a href="../../../src/TcpExamples/TcpExample1ScreenshotNetcat.png" target="_blank">TcpExample1ScreenshotNetcat.png</a> + * @see <a href="../../../src/TcpExamples/TcpExample1ScreenshotTelnet.png" target="_blank">TcpExample1ScreenshotTelnet.png</a> + * @see <a href="https://savage.nps.edu/Savage/developers.html#Cygwin" target="blank">Savage Developers Guide: Cygwin</a> + * @see <a href="https://savage.nps.edu/Savage/developers.html#telnet" target="blank">Savage Developers Guide: telnet</a> + * + * @author mcgredo + * @author brutzman@nps.edu + */ +public class TcpExample1TelnetYu +{ + /** Default constructor */ + public TcpExample1TelnetYu() + { + // default constructor + } + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String[] args) + { + try + { + System.out.println(TcpExample1TelnetYu.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(" terminal: enter (telnet localhost 2317) or, for macOS (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()) // listen, 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); + + String remoteHostname = clientConnection.getInetAddress().getHostAddress(); + ps.println("The " + remoteHostname + " client response was written by server " + TcpExample1TelnetYu.class.getName()); // to remote client + System.out.println("The " + remoteHostname + " server response was written by server " + TcpExample1TelnetYu.class.getName()); // to server console + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println(TcpExample1TelnetYu.class.getName() + " completed successfully."); + } + catch(IOException ioe) + { + System.err.println("Exception with " + TcpExample1TelnetYu.class.getName() + " networking:"); // describe what is happening + System.err.println(ioe); + // 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 (ioe 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/MV3500Cohort2024JulySeptember/homework1/Yu/package-info.java b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..36c25b042fa4b01623744512c7edbbd60c30fd94 --- /dev/null +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework1/Yu/package-info.java @@ -0,0 +1,10 @@ +/** + * TCP Unicast homework assignments supporting the NPS MOVES MV3500 Networked Graphics course. + * + * @see <a href="https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/tree/master/assignments" target="_blank">networkedGraphicsMV3500 assignments</a> + * @see java.lang.Package + * @see <a href="https://stackoverflow.com/questions/22095487/why-is-package-info-java-useful" target="_blank">StackOverflow: why-is-package-info-java-useful</a> + * @see <a href="https://stackoverflow.com/questions/624422/how-do-i-document-packages-in-java" target="_blank">StackOverflow: how-do-i-document-packages-in-java</a> + */ + +package MV3500Cohort2024JulySeptember.homework1.Yu;