diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java index 0403e222bfe4e365c702f9c4a698fcea2a8a5d60..de94d5fb361ce77f084802c845fed37e3c512050 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java @@ -33,7 +33,6 @@ public class LennonHW2Client // default constructor } static String DESTINATION_HOST = "localhost"; - static int MAX_LOOP_COUNT = 4; /** * Program invocation, execution starts here @@ -43,9 +42,7 @@ public class LennonHW2Client try { boolean play; Scanner scanner = new Scanner(System.in); - System.out.println(LennonHW2Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times"); - - //for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit + do{ System.out.println("======================================================="); System.out.println(LennonHW2Client.class.getName() + " creating new socket ...\n"); diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/README.md b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/README.md index c51055becc5cf015cae6c69d40a4e119e150078a..238484217398adae27c1fef7112dbf676faff40a 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/README.md +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/README.md @@ -4,4 +4,16 @@ ## Description +Modification to TCPExample4 to show the Message of the day (MOTD), depending on the day of the week. + +The server side sets up a server listening for incoming connectios. + +When a client attempts a connection to the server it sends the day of the week for the current connection. + +Once received the day of the week, the server reply with a message according to the day of the week. + +Phrases taken from + +https://www.divein.com/everyday/monday-motivation-quotes/ + diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java index b99d39bc797831094fb07e31f2dae3e20c5ca7c9..e81d0687c433467c9d6b0163969aa345ac16b286 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroClientHW2.java @@ -3,7 +3,6 @@ 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? @@ -30,8 +29,6 @@ public class RomeroClientHW2 { // default constructor } -// static String DESTINATION_HOST = "localhost"; -// static int MAX_LOOP_COUNT = 4; /** * Program invocation, execution starts here @@ -44,128 +41,33 @@ public class RomeroClientHW2 LocalDate currentDate = LocalDate.now(); DayOfWeek day = currentDate.getDayOfWeek(); - + System.out.println("Current date: " + currentDate); - System.out.println("Day: " + day); + System.out.println("Today is: " + day + "\n"); System.out.println(RomeroClientHW2.class.getName() + " creating new socket ..."); -try { - Socket clientConnectionSocket = new Socket("localhost", 2317); + try { + Socket clientConnectionSocket = new Socket("localhost", 2317); - in = new DataInputStream(clientConnectionSocket.getInputStream()); - out = new DataOutputStream(clientConnectionSocket.getOutputStream()); + 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); + out.writeUTF(day.name()); - String mensaje = in.readUTF(); + String mensaje = in.readUTF(); - System.out.println(mensaje); + System.out.println(mensaje); - clientConnectionSocket.close(); + 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!"); + } 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 fdc5bb347a793e593aa56d7fb61d044ded932f67..7242113e1d5196fe05ed575b335f9bb0a22c540d 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Romero/RomeroServerHW2.java @@ -1,7 +1,6 @@ package MV3500Cohort2024JulySeptember.homework2.Romero; import java.io.DataInputStream; -import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.net.*; @@ -34,8 +33,7 @@ public class RomeroServerHW2 * @param args command-line arguments */ public static void main(String[] args) - { - + { DataInputStream in; DataOutputStream out; @@ -57,23 +55,31 @@ public class RomeroServerHW2 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); + System.out.println("Client connected on: " + messageFromClient); - out.writeUTF("**//*///*/*/*/*/*a/a//a*/*a/*a/*a/*: "); + String messageOTD = ""; - // 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 - // TcpExample4HandlerThread introduce itself to the client. + switch(messageFromClient) { + case "MONDAY" -> messageOTD = "\n\"Mondays are the start of the work week which offer new beginnings 52 times a year!\"\n \t- David Dweck"; + case "TUESDAY" -> messageOTD = "\n\"May your Tuesday be like your coffee: strong, smooth, and full of warmth.\"\n \t- Unknown"; + case "WEDNESDAY" -> messageOTD = "\n\"Wednesdays will always bring smiles for the second half of the week.\"\n \t- Anthony T. Hincks"; + case "THURSDAY" -> messageOTD = "\n\"Thursday is a day to admit your mistakes and try to improve.\"\n \t- Byron Pulsifer"; + case "FRIDAY" -> messageOTD = "\n\"Every Friday, I like to high five myself for getting through another week\non little more than caffeine, willpower, and inappropriate humor.\"\n \t- Anonymous"; + case "SATURDAY" -> messageOTD = "\n\"Saturday is a time to enjoy the small things in life, and to look back on the week with gratitude.\"\n \t- Unknown"; + case "SUNDAY" -> messageOTD = "\n\"Sunday clears away the rust of the whole week.\"\n \t- Joseph Addison"; + default -> messageOTD = "\nError while getting the day of the week"; + } + out.writeUTF("\n**********************************************************************\n" + + messageOTD + + "\n\n**********************************************************************\n\n" + + "You are the client numer: " + connectionCount); + System.out.println("============================================================="); System.out.println(RomeroServerHW2.class.getName() + ".handlerThread created for connection #" + connectionCount + "...");