From 45284f12c07ca1d6b7f496e4c93066301a44f08b Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@nps.edu>
Date: Thu, 23 Aug 2018 08:31:40 -0700
Subject: [PATCH] TODO work in progress: moving all course examples into a
 single Netbeans project.

---
 CourseExamples/src/TcpExamples/README.md      |  1 +
 .../src/TcpExamples/TcpExample1Telnet.java    | 65 ++++++++++++++
 .../TcpExamples/TcpExample1TerminalLog.txt    |  7 ++
 .../TcpExample2ConnectionCounting.java        | 90 +++++++++++++++++++
 .../TcpExamples/TcpExample2TerminalLog.txt    | 26 ++++++
 5 files changed, 189 insertions(+)
 create mode 100644 CourseExamples/src/TcpExamples/README.md
 create mode 100644 CourseExamples/src/TcpExamples/TcpExample1Telnet.java
 create mode 100644 CourseExamples/src/TcpExamples/TcpExample1TerminalLog.txt
 create mode 100644 CourseExamples/src/TcpExamples/TcpExample2ConnectionCounting.java
 create mode 100644 CourseExamples/src/TcpExamples/TcpExample2TerminalLog.txt

diff --git a/CourseExamples/src/TcpExamples/README.md b/CourseExamples/src/TcpExamples/README.md
new file mode 100644
index 0000000000..0c705d3c17
--- /dev/null
+++ b/CourseExamples/src/TcpExamples/README.md
@@ -0,0 +1 @@
+TODO work in progress: moving all course examples into a single Netbeans project.
diff --git a/CourseExamples/src/TcpExamples/TcpExample1Telnet.java b/CourseExamples/src/TcpExamples/TcpExample1Telnet.java
new file mode 100644
index 0000000000..0f4ea292d7
--- /dev/null
+++ b/CourseExamples/src/TcpExamples/TcpExample1Telnet.java
@@ -0,0 +1,65 @@
+package TcpExamples;
+
+import java.io.*;
+import java.net.*;
+
+/**
+ * The simplest possible TCP network program. It listens for
+ * a connection, from telnet (telnet localhost 2317) or a program
+ * you write, which we will do later. Right now the TcpExample simply
+ * writes a string in response to a connection. 
+ * 
+ * Testing the running server program from telnet looks like this:
+ * 
+ * it154916:projects mcgredo$ telnet localhost 2317
+ * Trying ::1...
+ * Connected to localhost.
+ * Escape character is '^]'.
+ * This was written by the server
+ * Connection closed by foreign host.
+ * 
+ * Notice that "This was written by the server" matches 
+ * what is written by the code below, over the output stream.
+ * 
+ * After this first connection the program below drops out
+ * the bottom of the program, and does not repeat itself.
+ * The program exits.
+ * 
+ * @author mcgredo
+ */
+public class TcpExample1Telnet 
+{
+
+    public static void main(String[] args) 
+    {
+        try
+        {
+            // The ServerSocket waits for a connection from a client.
+            // It returns a Socket object when the connection occurs.
+            ServerSocket serverSocket = new ServerSocket(2317);
+            
+            // The Socket object represents the connection between
+            // the server and client, including a full duplex
+            // connection
+            Socket clientConnection = serverSocket.accept();
+            
+            // Use Java io classes to write text (as opposed to
+            // unknown bytes of some sort) to the client
+            OutputStream os = clientConnection.getOutputStream();
+            PrintStream ps = new PrintStream(os);
+            
+                    ps.println("This client response was written by server TcpExample1"); // to remote client
+            System.out.println("This server response was written by server TcpExample1"); // to server console
+            
+            // "flush()" in important in that it forces a write 
+            // across what is in fact a slow connection
+            ps.flush();
+            
+            clientConnection.close();
+        }
+        catch(Exception e)
+        {
+            System.out.println("problem with networking: " + e);
+        }
+    }
+}
diff --git a/CourseExamples/src/TcpExamples/TcpExample1TerminalLog.txt b/CourseExamples/src/TcpExamples/TcpExample1TerminalLog.txt
new file mode 100644
index 0000000000..c08fc0262a
--- /dev/null
+++ b/CourseExamples/src/TcpExamples/TcpExample1TerminalLog.txt
@@ -0,0 +1,7 @@
+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 TcpExample1
+Connection closed by foreign host.
diff --git a/CourseExamples/src/TcpExamples/TcpExample2ConnectionCounting.java b/CourseExamples/src/TcpExamples/TcpExample2ConnectionCounting.java
new file mode 100644
index 0000000000..3620d4036f
--- /dev/null
+++ b/CourseExamples/src/TcpExamples/TcpExample2ConnectionCounting.java
@@ -0,0 +1,90 @@
+package TcpExamples;
+
+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.
+ * 
+ * telnet localhost 2317
+ * 
+ * If you're sophisticated you can contact the instructor's computer
+ * while running this program.
+ * 
+ * <code>telnet ipOfServersLaptop 2317</code>
+ * 
+ * And have him display the socket pairs he got.
+ * @author mcgredo
+ */
+public class TcpExample2ConnectionCounting 
+{
+
+    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.
+			
+			int connectionCount = 0; // state
+            
+            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)
+            {
+                Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed
+				
+				connectionCount++; // got another one!
+				
+                OutputStream os = clientConnection.getOutputStream();
+                PrintStream ps = new PrintStream(os);
+
+				        ps.println("This client response was written by server TcpExample2"); // to remote client
+				System.out.println("This server response was written by server TcpExample2"); // to server console
+				
+				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 the 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();
+                clientConnection.close();
+            }
+       }
+        catch(Exception e)
+        {
+            System.out.println("problem with networking: " + e);
+        }
+    }
+}
diff --git a/CourseExamples/src/TcpExamples/TcpExample2TerminalLog.txt b/CourseExamples/src/TcpExamples/TcpExample2TerminalLog.txt
new file mode 100644
index 0000000000..bab60ab55a
--- /dev/null
+++ b/CourseExamples/src/TcpExamples/TcpExample2TerminalLog.txt
@@ -0,0 +1,26 @@
+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.
-- 
GitLab