From 34bc86944bf76766433cf9c1e083b5dd05490bf6 Mon Sep 17 00:00:00 2001
From: rojas <rojas@N619>
Date: Fri, 9 Aug 2024 13:18:58 -0700
Subject: [PATCH] Update

---
 .../homework2/Romero/RomeroClientHW2.java     | 171 ++++++++++++++++++
 .../homework2/Romero/RomeroServerHW2.java     |  23 +++
 .../homework2/Romero/TcpExample4Client.java   |  84 ---------
 3 files changed, 194 insertions(+), 84 deletions(-)
 create mode 100644 assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java
 delete mode 100644 assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/TcpExample4Client.java

diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java
new file mode 100644
index 0000000000..b99d39bc79
--- /dev/null
+++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java
@@ -0,0 +1,171 @@
+package MV3500Cohort2024JulySeptember.homework2.Romero;
+
+import java.io.*;
+import java.net.*;
+import java.time.LocalDate;
+import java.io.PrintWriter;
+import java.time.DayOfWeek;
+//import java.time.LocalTime; // conversion?
+
+/**
+ * This client program establishes a socket connection to the {@link TcpExample4DispatchServer},
+ * then checks how long it takes to read the single line it expects as a server response.
+ * No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}.
+ * 
+ * @see TcpExample4DispatchServer
+ * @see TcpExample4HandlerThread
+ *
+ * @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a>
+ * @see <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a>
+ * @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a>
+ * 
+ * @author Don McGregor
+ * @author Don Brutzman
+ * @author MV3500 class
+ */
+public class RomeroClientHW2
+{
+    /** Default constructor */
+    public RomeroClientHW2() 
+    {
+        // default constructor
+    }
+//    static String DESTINATION_HOST = "localhost";
+//    static int    MAX_LOOP_COUNT   = 4;
+
+    /**
+     * Program invocation, execution starts here
+     * @param args command-line arguments
+     */
+    public static void main(String[] args) {
+        
+        DataInputStream in;
+        DataOutputStream out;
+        
+        LocalDate currentDate = LocalDate.now();
+        DayOfWeek day = currentDate.getDayOfWeek();
+        
+        System.out.println("Current date: " + currentDate);
+        System.out.println("Day: " + day);
+        
+        System.out.println(RomeroClientHW2.class.getName() + " creating new socket ...");
+        
+try {   
+        Socket clientConnectionSocket = new Socket("localhost", 2317);
+
+        in = new DataInputStream(clientConnectionSocket.getInputStream());
+        out = new DataOutputStream(clientConnectionSocket.getOutputStream());
+
+        //PrintWriter out = new PrintWriter(clientConnectionSocket.getOutputStream(), true);
+        out.writeUTF("New client has arrived!, conected on " + day);
+
+        String mensaje = in.readUTF();
+
+        System.out.println(mensaje);
+
+        clientConnectionSocket.close();
+
+    } catch (IOException e) {
+        System.out.println("Problem with " + RomeroClientHW2.class.getName() + " networking:networking:"); // describe what is happening
+        System.out.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.out.println("*** Be sure to stop any other running instances of programs using this port!");
+        }
+    }
+        
+//        try {
+//            System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
+//            System.out.println("=======================================================");
+//            
+//            for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
+//            {
+//                System.out.println(TcpExample4Client.class.getName() + " creating new socket #" + loopCount + "...");
+//
+//                // 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.This particualar example is interacting
+//                // with what it expects is a server that writes a single text
+//                // line after 10 sec.
+//                long startTime = System.currentTimeMillis();
+//
+//                // open a socket for each loop
+//		Socket socket = new Socket(DESTINATION_HOST, 2317);
+//
+//                // Setup.  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();
+//                Reader isr = new InputStreamReader(is);
+//                BufferedReader br = new BufferedReader(isr);
+//
+//                String serverMessage = br.readLine(); // blocks
+//                long readTime = System.currentTimeMillis();
+//                long timeLength = readTime - startTime;
+//
+//                System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
+//                System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
+//                System.out.println("=======================================================");
+//                // To push this further, launch multiple copies of TcpExample4Client simultaneously
+//            }
+//            System.out.println(TcpExample4Client.class.getName() + " complete");
+//            // main method now exits
+//        } catch (IOException e) {
+//            System.out.println("Problem with " + TcpExample4Client.class.getName() + " networking:networking:"); // describe what is happening
+//            System.out.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.out.println("*** Be sure to stop any other running instances of programs using this port!");
+//            }
+//        }
+        
+//        try {
+//            System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
+//            System.out.println("=======================================================");
+//            
+//            for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
+//            {
+//                System.out.println(TcpExample4Client.class.getName() + " creating new socket #" + loopCount + "...");
+//
+//                // 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.This particualar example is interacting
+//                // with what it expects is a server that writes a single text
+//                // line after 10 sec.
+//                long startTime = System.currentTimeMillis();
+//
+//                // open a socket for each loop
+//		Socket socket = new Socket(DESTINATION_HOST, 2317);
+//
+//                // Setup.  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();
+//                Reader isr = new InputStreamReader(is);
+//                BufferedReader br = new BufferedReader(isr);
+//
+//                String serverMessage = br.readLine(); // blocks
+//                long readTime = System.currentTimeMillis();
+//                long timeLength = readTime - startTime;
+//
+//                System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
+//                System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
+//                System.out.println("=======================================================");
+//                // To push this further, launch multiple copies of TcpExample4Client simultaneously
+//            }
+//            System.out.println(TcpExample4Client.class.getName() + " complete");
+//            // main method now exits
+//        } catch (IOException e) {
+//            System.out.println("Problem with " + TcpExample4Client.class.getName() + " networking:networking:"); // describe what is happening
+//            System.out.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.out.println("*** Be sure to stop any other running instances of programs using this port!");
+//            }
+//        }
+    }
+}
diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java
index 43c3c77459..fdc5bb347a 100644
--- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java
+++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java
@@ -1,5 +1,8 @@
 package MV3500Cohort2024JulySeptember.homework2.Romero;
 
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.*;
 
@@ -32,20 +35,40 @@ public class RomeroServerHW2
      */
     public static void main(String[] args)
     {
+        
+        DataInputStream in;
+        DataOutputStream out;
+        
         try {
             ServerSocket             serverSocket = new ServerSocket(2317);
+            
+            System.out.println("Server has been initializated ... ");
+            System.out.println("Witing for conections ...");
+            
             Socket                   clientConnectionSocket;
             TcpExample4HandlerThread handlerThread;
 
             int connectionCount = 0; // state variable
 
             System.out.println(RomeroServerHW2.class.getName() + " ready to accept socket connections...");
+
             while (true) // infinite loop
             {
                 clientConnectionSocket = serverSocket.accept(); // block! until connected
 
                 connectionCount++; // unblocked, got another connection
                 
+                
+                in = new DataInputStream(clientConnectionSocket.getInputStream());
+                out = new DataOutputStream(clientConnectionSocket.getOutputStream());
+                
+                String messageFromClient = in.readUTF();                
+                System.out.println(messageFromClient);
+                
+                out.writeUTF("Hello world from SERVER, you are the client numer: " + connectionCount);
+                
+                out.writeUTF("**//*///*/*/*/*/*a/a//a*/*a/*a/*a/*: ");
+                
                 // TODO option for the student, provide initial message *to the client*
                 // that we are handing off to a dispatch thread... because that is polite behavior.
                 // Plenty of code in Example3, we instead let our proxy thread
diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/TcpExample4Client.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/TcpExample4Client.java
deleted file mode 100644
index 7b64e47eab..0000000000
--- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/TcpExample4Client.java
+++ /dev/null
@@ -1,84 +0,0 @@
-package MV3500Cohort2024JulySeptember.homework2.Romero;
-
-import java.io.*;
-import java.net.*;
-//import java.time.LocalTime; // conversion?
-
-/**
- * This client program establishes a socket connection to the {@link TcpExample4DispatchServer},
- * then checks how long it takes to read the single line it expects as a server response.
- * No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}.
- * 
- * @see TcpExample4DispatchServer
- * @see TcpExample4HandlerThread
- *
- * @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a>
- * @see <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a>
- * @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a>
- * 
- * @author Don McGregor
- * @author Don Brutzman
- * @author MV3500 class
- */
-public class TcpExample4Client
-{
-    /** Default constructor */
-    public TcpExample4Client()
-    {
-        // default constructor
-    }
-    static String DESTINATION_HOST = "localhost";
-    static int    MAX_LOOP_COUNT   = 4;
-
-    /**
-     * Program invocation, execution starts here
-     * @param args command-line arguments
-     */
-    public static void main(String[] args) {
-        try {
-            System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
-            System.out.println("=======================================================");
-            for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
-            {
-                System.out.println(TcpExample4Client.class.getName() + " creating new socket #" + loopCount + "...");
-
-                // 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.This particualar example is interacting
-                // with what it expects is a server that writes a single text
-                // line after 10 sec.
-                long startTime = System.currentTimeMillis();
-
-                // open a socket for each loop
-		Socket socket = new Socket(DESTINATION_HOST, 2317);
-
-                // Setup.  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();
-                Reader isr = new InputStreamReader(is);
-                BufferedReader br = new BufferedReader(isr);
-
-                String serverMessage = br.readLine(); // blocks
-                long readTime = System.currentTimeMillis();
-                long timeLength = readTime - startTime;
-
-                System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
-                System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
-                System.out.println("=======================================================");
-                // To push this further, launch multiple copies of TcpExample4Client simultaneously
-            }
-            System.out.println(TcpExample4Client.class.getName() + " complete");
-            // main method now exits
-        } catch (IOException e) {
-            System.out.println("Problem with " + TcpExample4Client.class.getName() + " networking:networking:"); // describe what is happening
-            System.out.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.out.println("*** Be sure to stop any other running instances of programs using this port!");
-            }
-        }
-    }
-}
-- 
GitLab