Skip to content
Snippets Groups Projects
Commit dd4dded9 authored by advan's avatar advan
Browse files

Assignment 2

parent 40669a4b
No related branches found
No related tags found
No related merge requests found
package TcpExamples; package MV3500Cohort2024JulySeptember.homework2.Yu;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
//import java.time.LocalTime; // conversion? //import java.time.LocalTime; // conversion?
/** /**
* This client program establishes a socket connection to the * @author Jin Hong Yu
* {@link TcpExample4DispatchServer}, then checks how long it takes to read the
* single line it expects as a server response. No fancy footwork here, it is
* pretty simple and similar to {@link TcpExample3Client}.
*
* @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread
*
* @see
* <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a>
* @see
* <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a>
* @see
* <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a>
*
* @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/ */
public class YuHW2Client { public class YuHW2Client {
......
package TcpExamples; package MV3500Cohort2024JulySeptember.homework2.Yu;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
/** /**
* <p> * @author Jin Hong Yu
* This utility class supports the {@link TcpExample4DispatchServer} program,
* handling all programming logic needed for a new socket connection
* to run 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.
* </p>
* <p>
* Warning: do not run this class! It is created and used automatically by {@link TcpExample4DispatchServer} at run time.
* </p>
*
* @see TcpExample4Client
* @see TcpExample4DispatchServer
*
* @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a>
* @see <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a>
* @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a>
*
* @author Don McGregor
* @author Don Brutzman
* @author MV3500 class
*/ */
public class YuHW2HandlerThread extends Thread public class YuHW2HandlerThread extends Thread
{ {
......
package TcpExamples; package MV3500Cohort2024JulySeptember.homework2.Yu;
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.*;
...@@ -22,11 +22,11 @@ public class YuHW2Server ...@@ -22,11 +22,11 @@ public class YuHW2Server
try { try {
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
Socket clientConnectionSocket; Socket clientConnectionSocket;
TcpExample4HandlerThread handlerThread; YuHW2HandlerThread handlerThread;
int connectionCount = 0; // state variable int connectionCount = 0; // state variable
System.out.println(TcpExample4DispatchServer.class.getName() + " ready to accept socket connections..."); //System.out.println(TcpExample4DispatchServer.class.getName() + " ready to accept socket connections...");
while (true) // infinite loop while (true) // infinite loop
{ {
clientConnectionSocket = serverSocket.accept(); // block! until connected clientConnectionSocket = serverSocket.accept(); // block! until connected
...@@ -35,18 +35,18 @@ public class YuHW2Server ...@@ -35,18 +35,18 @@ public class YuHW2Server
System.out.println("============================================================="); System.out.println("=============================================================");
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread created for connection #" + connectionCount + "..."); //System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread created for connection #" + connectionCount + "...");
// hand off this aready-created and connected socket to constructor // hand off this aready-created and connected socket to constructor
handlerThread = new TcpExample4HandlerThread(clientConnectionSocket); handlerThread = new YuHW2HandlerThread(clientConnectionSocket);
handlerThread.start();// invokes the run() method in that object handlerThread.start();// invokes the run() method in that object
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread is now dispatched and running, using most recent connection..."); //System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread is now dispatched and running, using most recent connection...");
// while(true) continue looping, serverSocket is still waiting for another customer client // while(true) continue looping, serverSocket is still waiting for another customer client
} }
} }
catch (IOException e) { catch (IOException e) {
System.out.println("Problem with " + TcpExample4DispatchServer.class.getName() + " networking:"); // describe what is happening //System.out.println("Problem with " + TcpExample4DispatchServer.class.getName() + " networking:"); // describe what is happening
System.out.println("Error: " + e); System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time // 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) {
......
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