From 9f19f3c2508da99efb59d6dfc59cbd20a72b687e Mon Sep 17 00:00:00 2001 From: owner <owner@DESKTOP-150HAJJ.ern.nps.edu> Date: Fri, 9 Aug 2024 08:41:49 -0700 Subject: [PATCH] Homework 2 --- .../homework2/Lennon/LennonHW2Client.java | 15 +++++++-------- .../homework2/Lennon/LennonHW2HandlerThread.java | 16 ++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java index b86a6573b8..0403e222bf 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java @@ -44,10 +44,11 @@ public class LennonHW2Client boolean play; Scanner scanner = new Scanner(System.in); System.out.println(LennonHW2Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times"); - System.out.println("======================================================="); + //for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit do{ - System.out.println(LennonHW2Client.class.getName() + " creating new socket ..."); + System.out.println("======================================================="); + System.out.println(LennonHW2Client.class.getName() + " creating new socket ...\n"); // We request an IP to connect to ("localhost") and // port number at that IP (2317). This establishes @@ -99,17 +100,15 @@ public class LennonHW2Client long endTime = System.currentTimeMillis(); long timeLength = endTime - startTime; - System.out.println(": message received from server:" + serverMessage); - System.out.println(": time to play=" + timeLength); + System.out.println("\nMessage from server:" + serverMessage); + System.out.println("Time to play = " + timeLength); - System.out.println("Want to go again? Y/N"); + System.out.println("\nWant to go again? Y/N"); String response = scanner.nextLine(); play = "Y".equals(response.toUpperCase()); - //line below is for debug - System.out.println(response.toUpperCase()); //for debug only. delete later }while(play); - System.out.println("GOODBYE"); + System.out.println("\nGOODBYE"); System.out.println("======================================================="); System.out.println(LennonHW2Client.class.getName() + " complete"); // main method now exits diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java index 2bdad4bfc1..6ca17564c4 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java @@ -1,7 +1,7 @@ package MV3500Cohort2024JulySeptember.homework2.Lennon; -import TcpExamples.TcpExample4Client; -import TcpExamples.TcpExample4DispatchServer; +//import TcpExamples.TcpExample4Client; +//import TcpExamples.TcpExample4DispatchServer; import java.io.*; import java.net.*; import java.util.Random; @@ -50,8 +50,8 @@ public class LennonHW2HandlerThread extends Thread */ public static void main(String[] args) { - System.out.println ("*** TcpExample4HandlerThread is not a standalone executable progam."); - System.out.println ("*** Please run TcpExample4DispatchServer instead... now exiting."); + System.out.println ("*** LennonHW2HandlerThread is not a standalone executable progam."); + System.out.println ("*** Please run LennonHW2DispatchServer instead... now exiting."); } /** Handles one connection. We add an artificial slowness @@ -73,7 +73,7 @@ public class LennonHW2HandlerThread extends Thread OutputStream os = socket.getOutputStream(); PrintStream ps = new PrintStream(os); - ps.println("I'm thinking of a number between 1 and 10. Can you guess it?" + LennonHW2HandlerThread.class.getName()); // TODO insert socket count here! + ps.println("I'm thinking of a number between 1 and 10. Can you guess it?" ); ps.flush(); // make sure that it indeed escapes current process and reaches the client InputStream is = socket.getInputStream(); @@ -90,16 +90,16 @@ public class LennonHW2HandlerThread extends Thread serverResponse = "You're wrong. It was " + randNum; } }catch(NumberFormatException e){ - System.out.println("Something went wrong. guess was not a number" + e); + System.out.println("Something went wrong. Guess was not a number" + e); serverResponse = "Something went wrong. Anyway, my number was " + randNum; } - final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds + final long TIMEOUT = 500; // 2000 milliseconds = 2 seconds, 500 milliseconds = 0.5 seconds System.out.println(LennonHW2HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms" + " to emulate computation and avoid server-side overload"); Thread.sleep(TIMEOUT); // ps is the PrintStream is the Java way to use System.print() to pass data along the socket. - ps.println(serverResponse + LennonHW2HandlerThread.class.getName()); // TODO insert socket count here! + ps.println(serverResponse ); ps.flush(); // make sure that it indeed escapes current process and reaches the client socket.close(); // all clear, no longer need socket System.out.println(LennonHW2HandlerThread.class.getName() + " finished handling a thread, now exit."); -- GitLab