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

Merge origin/master

parents 5b9efbce df10c5aa
No related branches found
No related tags found
No related merge requests found
......@@ -54,10 +54,10 @@ public class TcpExample2ConnectionCounting
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server TcpExample2ConnectionCounting"); // to remote client
System.out.println("This server response was written by server TcpExample2ConnectionCounting"); // to server console
ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client
System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
ps.println("You were connection #" + connectionCount + ", by my count");
ps.println("You were connection #" + connectionCount + ", by my count");
// Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.)
......@@ -89,7 +89,7 @@ public class TcpExample2ConnectionCounting
}
catch(IOException e)
{
System.err.println("Problem with TcpExample2ConnectionCounting networking:"); // describe what is happening
System.err.println("Problem with " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening
System.err.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)
......
......@@ -18,7 +18,8 @@ public class TcpExample3Client {
/** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1
* @see <a href="https://en.wikipedia.org/wiki/localhost">https://en.wikipedia.org/wiki/localhost</a>
* @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a> */
* @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a>
*/
public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
/**
......@@ -30,7 +31,7 @@ public class TcpExample3Client {
// Local variables/fields
Socket socket = null;
InputStream is;
InputStreamReader isr;
Reader isr;
BufferedReader br;
String serverMessage;
int clientLoopCount = 0;
......@@ -39,7 +40,7 @@ public class TcpExample3Client {
while (true)
{
clientLoopCount++; // increment at beginning of loop for reliability
System.out.println("TcpExample3Client creating socket...");
System.out.println(TcpExample3Client.class.getName() + " creating socket...");
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
......@@ -70,7 +71,7 @@ public class TcpExample3Client {
}
catch (IOException e)
{
System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening
System.err.println("Problem with " + TcpExample3Client.class.getName() + " networking:"); // describe what is happening
System.err.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
......@@ -87,7 +88,7 @@ public class TcpExample3Client {
// program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println();
System.out.println("TcpExample3Client exit");
System.out.println(TcpExample3Client.class.getName() + " exit");
}
}
}
......@@ -36,7 +36,7 @@ public class TcpExample3Server {
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket
// needs to be made only once.
System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first
System.out.println(TcpExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os;
......@@ -74,7 +74,7 @@ public class TcpExample3Server {
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
// Why is the first IP/port the same, while the second set has different ports?
System.out.println("TcpExample3Server socket pair showing host name, address, port:");
System.out.println(TcpExample3Server.class.getName() + " socket pair showing host name, address, port:");
System.out.println(" (( " +
localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
......@@ -92,7 +92,7 @@ public class TcpExample3Server {
}
}
} catch (IOException e) {
System.err.println("Problem with TcpExample3Server networking: " + e);
System.err.println("Problem with " + TcpExample3Server.class.getName() + " networking: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException) {
......
......@@ -22,11 +22,11 @@ public class TcpExample4Client {
*/
public static void main(String[] args) {
try {
System.out.println("TcpExample4Client start, loop " + MAX_LOOP_COUNT + " times");
System.out.println(TcpExample4Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times");
System.out.println("==================================================");
for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
{
System.out.println("TcpExample4Client creating socket #" + loopCount + "...");
System.out.println(TcpExample4Client.class.getName() + " creating socket #" + loopCount + "...");
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
......@@ -44,22 +44,22 @@ public class TcpExample4Client {
// We'd do things a bit differently if many lines to be read
// from the server, instead of one only.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
Reader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine(); // blocks
long readTime = System.currentTimeMillis();
long timeLength = readTime - startTime;
System.out.println("TcpExample4Client: message received from server='" + serverMessage + "'");
System.out.println("TcpExample4Client: time msec required for read=" + timeLength);
System.out.println(TcpExample4Client.class.getName() + ": message received from server='" + serverMessage + "'");
System.out.println(TcpExample4Client.class.getName() + ": time msec required for read=" + timeLength);
System.out.println("==================================================");
// To push this further, launch multiple copies of TcpExample4Client simultaneously
}
System.out.println("TcpExample4Client complete");
System.out.println(TcpExample4Client.class.getName() + " complete");
// main method now exits
} catch (IOException e) {
System.out.println("Problem with TcpExample4Client networking:networking:"); // describe what is happening
System.out.println("Problem with " + TcpExample4Client.class.getName() + " networking: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) {
......
......@@ -21,23 +21,25 @@ public class TcpExample4DispatchServer {
{
try {
ServerSocket serverSocket = new ServerSocket(2317);
TcpExample4HandlerThread handlerThread;
Socket clientConnection;
int connectionCount = 0; // state variable
System.out.println("TcpExample4DispatchServer ready to accept socket connections...");
System.out.println(TcpExample4DispatchServer.class.getName() + " ready to accept socket connections...");
while (true) // infinite loop
{
Socket clientConnection = serverSocket.accept(); // block until connected
clientConnection = serverSocket.accept(); // block until connected
connectionCount++; // unblocked, got another connection
System.out.println("=============================================================");
System.out.println("TcpExample4DispatchServer.handlerThread invocation for connection #" + connectionCount + "...");
TcpExample4HandlerThread handlerThread = new TcpExample4HandlerThread(clientConnection);
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread invocation for connection #" + connectionCount + "...");
handlerThread = new TcpExample4HandlerThread(clientConnection);
handlerThread.start(); // invokes the run() method in that object
System.out.println("TcpExample4DispatchServer.handlerThread is launched, awaiting another connection...");
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread is launched, awaiting another connection...");
}
} catch (IOException e) {
System.out.println("Problem with TcpExample4DispatchServer networking:"); // describe what is happening
System.out.println("Problem with " + TcpExample4DispatchServer.class.getName() + " 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) {
......
......@@ -14,8 +14,7 @@ import java.net.*;
* @author Don Brutzman
* @author MV3500 class
*/
public class TcpExample4HandlerThread extends Thread
class TcpExample4HandlerThread extends Thread
{
/** The socket connection to a client */
Socket socket;
......@@ -26,7 +25,7 @@ public class TcpExample4HandlerThread extends Thread
*
* @param socket The socket connection handled by this thread
*/
public TcpExample4HandlerThread(Socket socket)
TcpExample4HandlerThread(Socket socket)
{
this.socket = socket;
}
......@@ -35,31 +34,33 @@ public class TcpExample4HandlerThread extends Thread
* 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
// @overriding run() method in Java Thread class is deliberate
@Override
public void run()
{
try
{
System.out.println("TcpExample4HandlerThread starting to handle a thread...");
System.out.println(TcpExample4HandlerThread.class.getName() + " 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);
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);
final long TIMEOUT = 2000; // 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
System.out.println(TcpExample4HandlerThread.class.getName() + " pausing for TIMEOUT=" + TIMEOUT + "ms"); // debug
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 TcpExample4HandlerThread"); // TODO insert socket count here!
ps.println("This message was written by the server " + TcpExample4HandlerThread.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("TcpExample4HandlerThread finished handling a thread, now exit.");
System.out.println(TcpExample4HandlerThread.class.getName() + " finished handling a thread, now exit.");
}
catch(IOException | InterruptedException e) // either a networking or a threading problem
{
System.out.println("Problem with TcpExample4HandlerThread networking:"); // describe what is happening
System.out.println("Problem with " + TcpExample4HandlerThread.class.getName() + " 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)
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