diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java index 0b1f881a1032bf3d2a44c6f8cda1e23328e7b164..80cf6a753c63557f25d9c2316d3b25f6a5ae7590 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2Client.java @@ -1,8 +1,8 @@ package MV3500Cohort2024JulySeptember.homework2.Lennon; -import TcpExamples.TcpExample3Client; -import TcpExamples.TcpExample4DispatchServer; -import TcpExamples.TcpExample4HandlerThread; +//import TcpExamples.TcpExample3Client; +//import TcpExamples.TcpExample4DispatchServer; +//import TcpExamples.TcpExample4HandlerThread; import java.io.*; import java.net.*; import java.util.Scanner; @@ -42,6 +42,7 @@ public class LennonHW2Client public static void main(String[] args) { try { 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 @@ -52,13 +53,12 @@ public class LennonHW2Client // 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(); + // connections. + // open a socket for each loop Socket socket = new Socket(DESTINATION_HOST, 2317); + long startTime = System.currentTimeMillis(); // Setup. Read the single line written by the server. // We'd do things a bit differently if many lines to be read @@ -76,15 +76,12 @@ public class LennonHW2Client System.out.println("======================================================="); // To push this further, launch multiple copies of TcpExample4Client simultaneously System.out.println("Want to go again? Y/N"); - String response = ""; - //response = scanner.nextLine(); - if ("Y".equals(response.toUpperCase().charAt(0))){ - play = true; - } - else{ - play = false; - } + + String response = scanner.nextLine(); + play = "Y".equals(response.toUpperCase()); + System.out.println(response.toUpperCase()); }while(play); + System.out.println("GOODBYE"); System.out.println(LennonHW2Client.class.getName() + " complete"); // main method now exits } catch (IOException e) { diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java index 6105573440f9c83e11ce535c1e9934112306466f..6fa0180d2f85fead73cf0d47775e62b08ddafbe7 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework2/Lennon/LennonHW2HandlerThread.java @@ -66,14 +66,23 @@ public class LennonHW2HandlerThread extends Thread // get the connection output stream, then wait a period of time. 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.flush(); // make sure that it indeed escapes current process and reaches the client + InputStream is = socket.getInputStream(); + Reader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String userGuess = br.readLine(); // blocks + final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 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("This message was written by the server " + LennonHW2HandlerThread.class.getName()); // TODO insert socket count here! + ps.println("That's a good guess, but you're wrong" + LennonHW2HandlerThread.class.getName()); // TODO insert socket count here! 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.");