From 5c1323c7a66adabb814dee3db36064bd11830c29 Mon Sep 17 00:00:00 2001
From: garrettloeffelman <garrettloeffelman@172.20.155.92>
Date: Wed, 1 Aug 2018 09:44:59 -0700
Subject: [PATCH] LoeffelmanAssignment2Client/Server Skeleton

---
 .../LoeffelmanAssignment2Client.java          | 57 +++++++++++++++
 .../LoeffelmanAssignment2Server.java          | 72 +++++++++++++++++++
 2 files changed, 129 insertions(+)
 create mode 100644 deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Client.java
 create mode 100644 deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Server.java

diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Client.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Client.java
new file mode 100644
index 0000000000..590d660b60
--- /dev/null
+++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Client.java
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+package MV3500Cohort2018JulySeptember.homework2.Loeffelman;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.Socket;
+
+/**
+ *
+ * @author garrettloeffelman
+ */
+public class LoeffelmanAssignment2Client {
+
+   public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1
+
+	public static void main(String[] args) {
+            
+		try {
+			while (true) {
+				System.out.println("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 the Socket
+				// object; the server uses a ServerSocket to wait for
+				// connections.
+				Socket socket = new Socket(LOCALHOST, 2317); // locohost?
+
+				// Read the single line written by the server. We'd
+				// do things a bit differently if many lines to be read
+				// from the server, instead of one only.
+				InputStream is = socket.getInputStream();
+				InputStreamReader isr = new InputStreamReader(is);
+				BufferedReader br = new BufferedReader(isr);
+
+				String serverMessage = br.readLine();
+				System.out.println("==================================================");
+				System.out.println("Now we're talking!");
+				System.out.println("The message the server sent was " + serverMessage);
+				// socket gets closed, either automatically/silently this code (or possibly by server)
+			} // end while(true)
+		} 
+		catch (IOException e) {
+			System.out.println("Problem with client: "); // describe what is happening
+			System.out.println(e);
+		}
+		// program exit: tell somebody about that
+		System.out.println("client exit");
+	}
+    
+}
diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Server.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Server.java
new file mode 100644
index 0000000000..1d0db5018c
--- /dev/null
+++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Loeffelman/LoeffelmanAssignment2Server.java
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+package MV3500Cohort2018JulySeptember.homework2.Loeffelman;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+/**
+ *
+ * @author garrettloeffelman
+ */
+public class LoeffelmanAssignment2Server {
+
+    @SuppressWarnings("ConvertToTryWithResources")
+	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.
+
+			ServerSocket serverSocket = new ServerSocket(2317);
+
+			// Loop, infinitely, waiting for client connections.
+			// Stop the program somewhere else.
+			while (true)
+			{
+				Socket clientConnection = serverSocket.accept(); // block until connected
+				OutputStream os = clientConnection.getOutputStream();
+				PrintStream ps = new PrintStream(os);
+
+				ps.println("This was written by the server");
+
+				// 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();
+
+				// 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("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+						+ remoteAddress.toString() + ", " + remotePort + " ))");
+
+				// 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(); // like it or not, you're outta here!
+			}
+		} 
+		catch (IOException e) {
+			System.out.println("problem with networking");
+		}
+	}
+    
+}
-- 
GitLab