Skip to content
Snippets Groups Projects
Commit eb2bf879 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

classroom tuneups and diagnostics/comments

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