diff --git a/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java b/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java index 63386b553660738092dfd002b752b85e1d957ecc..6bf47f29e3064ca841e291797560313ce340506f 100644 --- a/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java +++ b/projects/TcpExample4/TcpClient/TcpClient/src/tcpclient/TcpClient.java @@ -2,7 +2,7 @@ package tcpclient; import java.io.*; import java.net.*; -import java.time.LocalTime; +//import java.time.LocalTime; // conversion? /** * This is a slightly different client. It establishes a connection @@ -13,12 +13,11 @@ import java.time.LocalTime; */ public class TcpClient { - public static void main(String[] args) { try { - System.out.println("creating socket"); + System.out.println("creating socket"); // "here i am" on console // We request an IP to connect to ("localhost") and // port number at that IP (2317). This establishes @@ -39,20 +38,16 @@ public class TcpClient BufferedReader br = new BufferedReader(isr); String serverMessage = br.readLine(); - long readTime = System.currentTimeMillis(); + long readTime = System.currentTimeMillis(); long timeLength = readTime - startTime; System.out.println("The message the server sent was " + serverMessage); System.out.println("The time to read in ms was " + timeLength); } - catch(Exception e) + catch(IOException e) { System.out.println("Problem with client"); System.out.println(e); } - } - - - } \ No newline at end of file diff --git a/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java b/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java index 5dd36ab12db915ca137f1a509fb1ccf0263105db..c36bcc66620d1af92a8b756f344b6b7a4eb2c16e 100644 --- a/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java +++ b/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/HandlerThread.java @@ -33,8 +33,9 @@ public class HandlerThread extends Thread /** 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 @@ -43,24 +44,20 @@ public class HandlerThread extends Thread // get the connection output stream, then wait a period // of time. OutputStream os = socket.getOutputStream(); - PrintStream ps = new PrintStream(os); + PrintStream ps = new PrintStream(os); - final long TIMEOUT = 10000; // 10000 + final long TIMEOUT = 10000; // 10000 milliseconds System.out.println("Server: pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug Thread.sleep(TIMEOUT); // 10 seconds ps.println("This was written by the server"); ps.flush(); socket.close(); - System.out.println("Finished handling a thread"); + System.out.println("Finished handling a thread, HandlerThread exit"); } - catch(Exception e) + catch(IOException | InterruptedException e) // either a networking or a threading problem { System.out.println(e); } - } - - - } diff --git a/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java b/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java index c7b0b2a99ab5408f6c5a5eafc80cef794ff39f08..6a88bb36b1fe1a475e6a730ac15012ccea83ff09 100644 --- a/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java +++ b/projects/TcpExample4/TcpThreadServer/TcpThreadServer/src/tcpthreadserver/TcpThreadServer.java @@ -1,7 +1,6 @@ - package tcpthreadserver; -import java.io.*; +import java.io.IOException; import java.net.*; /** @@ -11,29 +10,27 @@ import java.net.*; * @author mcgredo */ public class TcpThreadServer { - - public static void main(String[] args) + public static void main(String[] args) // execution starts here { try { ServerSocket serverSocket = new ServerSocket(2317); - while(true) + while(true) // infinite loop { + System.out.println("TcpThreadServer ready to accept socket connection..."); Socket clientConnection = serverSocket.accept(); // block until connected HandlerThread handlerThread = new HandlerThread(clientConnection); handlerThread.start(); // invokesthe run() method in that object - } - + System.out.println("TcpThreadServer handlerThread is complete"); + } } - catch(Exception e) + catch(IOException e) { System.out.println("problem with networking"); System.out.println("Error: " + e); } - } - }