Skip to content
Snippets Groups Projects
Commit a50bee47 authored by Schutt, Thomas (Capt)'s avatar Schutt, Thomas (Capt)
Browse files

Schutt HW 2 work

parent 58b5e2f0
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2019JulySeptember.homework2.Schutt;
import java.io.IOException;
import java.net.*;
/**
......@@ -36,9 +37,20 @@ public class SchuttServerDispatcher {
System.out.println("========================================================");
System.out.println("SchuttServerDispatcher.handlerThread invocation for connection #" + connectionCount + "...");
SchuttThreadHandler handlerThread = new SchuttThreadHandler(clientConnection);
handlerThread.start();
System.out.println("SchuttServerDispatcher.handlerThread is launched, awaiting next connection...");
}
} catch (IOException e){
System.out.println("Problem with SchuttServerDispatcher networking:");
System.out.println("Error: " + e);
if(e instanceof java.net.BindException){
System.out.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
System.out.println("=============================================================");
}
}
......@@ -4,63 +4,83 @@ import java.io.*;
import java.net.*;
/**
* A program that handles all logic associated with one socket connection
* by running in a thread of its own. This is the server
* portion as well, so we artificially invent what happens
* if the server can't respond to a connection for several seconds.
* Warning: do not run this class! It is created automatically by TcpExample4DispatchServer.
*
* A program that handles all logic associated with one socket connection by
* running in a thread of its own. This is the server portion as well, so we
* artificially invent what happens if the server can't respond to a connection
* for several seconds. Warning: do not run this class! It is created
* automatically by TcpExample4DispatchServer.
*
* @author Schutt
*/
public class SchuttThreadHandler extends Thread {
public class SchuttThreadHandler extends Thread
{
/** The socket connection to a client */
/**
* The socket connection to a client
*/
Socket socket;
/** The thread constructor creates the socket from
* a ServerSocket, and passes one to the thread
* responsible for handling the connection.
*
int num1;
int num2;
int output;
/**
* The thread constructor creates the socket from a ServerSocket, and passes
* one to the thread responsible for handling the connection.
*
* @param socket The socket connection handled by this thread
*/
public SchuttThreadHandler(Socket socket)
{
public SchuttThreadHandler(Socket socket) {
this.socket = socket;
}
/** Handles one connection. We add an artificial slowness
* to handling the connection with a sleep(). This means
* the client won't see a server connection response for ten seconds (default).
/**
* Handles one connection. We add an artificial slowness to handling the
* connection with a sleep(). This means the client won't see a server
* connection response for ten seconds (default).
*/
// @overriding run() method in Java Thread class is deliberate
@Override
public void run()
{
try
{
System.out.println("TcpExample4HandlerThread starting to handle a thread...");
// @overriding run() method in Java Thread class is deliberate
@Override
public void run() {
try {
System.out.println("SchuttThreadHandler starting to handle a thread...");
// get the connection output stream, then wait a period of time.
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
System.out.println("TcpExample4HandlerThread pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug
Thread.sleep(TIMEOUT);
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
ps.println("Please send your equation!");
String clientInput = br.readLine();
System.out.println("The Client's equation is: " + clientInput);
if(clientInput.contains("+")){
num1 = (int)clientInput.charAt(0);
num2 = (int)clientInput.charAt(4);
System.out.println(num1 + " " + num2);
// 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 TcpExample4HandlerThread");
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("TcpExample4HandlerThread finished handling a thread, now exit.");
}
catch(IOException | InterruptedException e) // either a networking or a threading problem
output = num1 + num2;
}
if(clientInput.contains("*")){
num1 = (int)clientInput.charAt(0);
num2 = (int)clientInput.charAt(4);
output = num1 * num2;
}
System.out.println("The Client's answer is: " + output);
ps.println("The answer to your equation is: " + output);
ps.flush();
socket.close();
} catch (IOException e) // either a networking or a threading problem
{
System.out.println("Problem with TcpExample4HandlerThread networking:"); // describe what is happening
System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException)
if (e instanceof java.net.BindException) {
System.out.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
}
}
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