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

Homework 2

parent 80e3c23e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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.");
......
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