Skip to content
Snippets Groups Projects
Commit db490ad4 authored by tbavl's avatar tbavl
Browse files

Merge origin/master

parents 12c18e71 80e3c23e
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
......@@ -68,23 +68,49 @@ public class LennonHW2Client
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine(); // blocks
long readTime = System.currentTimeMillis();
long timeLength = readTime - startTime;
System.out.println("Message from server:" + serverMessage);
String guess = null;
boolean validGuess = false;
while(!validGuess){
guess = scanner.nextLine();
try{
int numGuess = Integer.parseInt(guess);
if (numGuess < 1 || numGuess > 10){
System.out.println("I said between 1 and 10. Guess Again");
continue;
}
validGuess = true;
}
catch(NumberFormatException e){
System.out.println("That's not a number. Guess Again");
}
}
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println(guess);
ps.flush();
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
serverMessage = br.readLine(); // blocks
long endTime = System.currentTimeMillis();
long timeLength = endTime - startTime;
System.out.println(LennonHW2Client.class.getName() + ": message received from server='" + serverMessage + "'");
System.out.println(LennonHW2Client.class.getName() + ": time msec required for read=" + timeLength);
System.out.println("=======================================================");
// To push this further, launch multiple copies of TcpExample4Client simultaneously
System.out.println(": message received from server:" + serverMessage);
System.out.println(": time to play=" + timeLength);
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());
//line below is for debug
System.out.println(response.toUpperCase()); //for debug only. delete later
}while(play);
System.out.println("GOODBYE");
System.out.println("=======================================================");
System.out.println(LennonHW2Client.class.getName() + " complete");
// main method now exits
} catch (IOException e) {
......
package MV3500Cohort2024JulySeptember.homework2.Lennon;
import TcpExamples.TcpExample4Client;
import TcpExamples.TcpExample4DispatchServer;
import java.io.*;
import java.net.*;
import java.util.Random;
/**
* <p>
......@@ -64,16 +67,39 @@ public class LennonHW2HandlerThread extends Thread
System.out.println(LennonHW2HandlerThread.class.getName() + " starting to handle a thread...");
// get the connection output stream, then wait a period of time.
Random random = new Random();
int randNum = random.nextInt(10)+1;
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
String serverResponse = null;
try{
int userNum = Integer.parseInt(userGuess);
if (userNum == randNum){
serverResponse = "Good job. You got it.";
}else{
serverResponse = "You're wrong. It was " + randNum;
}
}catch(NumberFormatException 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
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(serverResponse + 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.");
......
## Homework 2:
For this assignment, I utilized the framework classes from the example 4 we disected in class.
The three files—HW2Client, HW2Server, HW2Thread— work together to create a simple client-server application.
The server class is the main server program, listening for incoming client connections on a specified
port 2317. When the client connects, the server runs HW2Thread to handle the communication with that client,
allowing the server to manage multiple clients concurrently. The thread class is responsible for processing the client's
input and sending an appropriate response back, after which it closes the connection. On the client side,
the client class connects to the server, sends a message "Ethan", and receives a response. The client measures
the time taken for the interaction and handles multiple iterations if specified. Overall, the server/client interact
through a series of messages, with the server capable of handling multiple clients simultaneously through threading.
\ No newline at end of file
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