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;
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) {
......
......@@ -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.");
......
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