Skip to content
Snippets Groups Projects
Commit fc902310 authored by owner's avatar owner
Browse files

Homework 2

parent f6a735ea
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2024JulySeptember.homework2.Lennon; package MV3500Cohort2024JulySeptember.homework2.Lennon;
import TcpExamples.TcpExample3Client; //import TcpExamples.TcpExample3Client;
import TcpExamples.TcpExample4DispatchServer; //import TcpExamples.TcpExample4DispatchServer;
import TcpExamples.TcpExample4HandlerThread; //import TcpExamples.TcpExample4HandlerThread;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.Scanner; import java.util.Scanner;
...@@ -42,6 +42,7 @@ public class LennonHW2Client ...@@ -42,6 +42,7 @@ public class LennonHW2Client
public static void main(String[] args) { public static void main(String[] args) {
try { try {
boolean play; boolean play;
Scanner scanner = new Scanner(System.in);
System.out.println(LennonHW2Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times"); System.out.println(LennonHW2Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
System.out.println("======================================================="); System.out.println("=======================================================");
//for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit //for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
...@@ -52,13 +53,12 @@ public class LennonHW2Client ...@@ -52,13 +53,12 @@ public class LennonHW2Client
// port number at that IP (2317). This establishes // port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket // a connection to that IP in the form of the Socket
// object; the server uses a ServerSocket to wait for // object; the server uses a ServerSocket to wait for
// connections.This particualar example is interacting // connections.
// 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 // open a socket for each loop
Socket socket = new Socket(DESTINATION_HOST, 2317); Socket socket = new Socket(DESTINATION_HOST, 2317);
long startTime = System.currentTimeMillis();
// Setup. Read the single line written by the server. // Setup. Read the single line written by the server.
// We'd do things a bit differently if many lines to be read // We'd do things a bit differently if many lines to be read
...@@ -76,15 +76,12 @@ public class LennonHW2Client ...@@ -76,15 +76,12 @@ public class LennonHW2Client
System.out.println("======================================================="); System.out.println("=======================================================");
// To push this further, launch multiple copies of TcpExample4Client simultaneously // To push this further, launch multiple copies of TcpExample4Client simultaneously
System.out.println("Want to go again? Y/N"); System.out.println("Want to go again? Y/N");
String response = "";
//response = scanner.nextLine(); String response = scanner.nextLine();
if ("Y".equals(response.toUpperCase().charAt(0))){ play = "Y".equals(response.toUpperCase());
play = true; System.out.println(response.toUpperCase());
}
else{
play = false;
}
}while(play); }while(play);
System.out.println("GOODBYE");
System.out.println(LennonHW2Client.class.getName() + " complete"); System.out.println(LennonHW2Client.class.getName() + " complete");
// main method now exits // main method now exits
} catch (IOException e) { } catch (IOException e) {
......
...@@ -66,14 +66,23 @@ public class LennonHW2HandlerThread extends Thread ...@@ -66,14 +66,23 @@ public class LennonHW2HandlerThread extends Thread
// get the connection output stream, then wait a period of time. // get the connection output stream, then wait a period of time.
OutputStream os = socket.getOutputStream(); OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os); 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 final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
System.out.println(LennonHW2HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms" + System.out.println(LennonHW2HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms" +
" to emulate computation and avoid server-side overload"); " to emulate computation and avoid server-side overload");
Thread.sleep(TIMEOUT); Thread.sleep(TIMEOUT);
// ps is the PrintStream is the Java way to use System.print() to pass data along the socket. // 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 ps.flush(); // make sure that it indeed escapes current process and reaches the client
socket.close(); // all clear, no longer need socket socket.close(); // all clear, no longer need socket
System.out.println(LennonHW2HandlerThread.class.getName() + " finished handling a thread, now exit."); System.out.println(LennonHW2HandlerThread.class.getName() + " finished handling a thread, now exit.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment