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

improved comments and variable naming

parent 47484356
No related branches found
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting ...@@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting
try try
{ {
// welcome aboard messages // welcome aboard messages
System.out.println("TcpExample2ConnectionCounting has started and is waiting for a connection."); System.out.println(TcpExample2ConnectionCounting.class.getName() + " has started and is waiting for a connection.");
System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet");
System.out.println(" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)"); System.out.println(" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)");
System.out.println(" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or" ); System.out.println(" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or" );
...@@ -76,18 +76,22 @@ public class TcpExample2ConnectionCounting ...@@ -76,18 +76,22 @@ public class TcpExample2ConnectionCounting
int connectionCount = 0; // state variable int connectionCount = 0; // state variable
ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on.
// of interest: often client doesn't care what port it uses locally when connecting to that server port. // of interest: often client doesn't care what port it uses locally when connecting to that server port.
OutputStream outputStream; // can declare these early, but...
PrintStream printStream; // note initial values are null, they still need to be intialized
// Loop, infinitely, waiting for client connections. // Loop, infinitely, waiting for client connections.
// Stop the program somewhere else. // If you want to stop or pause the program, go somewhere else.
while(true) while(true)
{ {
// serverSocket.accept() blocks! then proceeds once a connection is "accept"ed // note that serverSocket.accept() first blocks! then after waiting, execution proceeds once a connection is "accept"ed
try (Socket clientConnection = serverSocket.accept()) { // the accept() method blocks here until a client connects // you can check this behavior by using NetBeans Debugger
try (Socket clientConnectionSocket = serverSocket.accept()) { // the accept() method blocks here until a client connects
connectionCount++; // got another one! a client has connected connectionCount++; // got another one! a client has connected
OutputStream os = clientConnection.getOutputStream(); outputStream = clientConnectionSocket.getOutputStream(); // clientConnectionSocket is new, every time through, so reset the streams
PrintStream ps = new PrintStream(os); printStream = new PrintStream(outputStream);
// if (connectionCount == 1) // first time through, report connection // if (connectionCount == 1) // first time through, report connection
// { // {
...@@ -104,19 +108,19 @@ public class TcpExample2ConnectionCounting ...@@ -104,19 +108,19 @@ public class TcpExample2ConnectionCounting
// "(telnet " + localHostAddress + " 2317)..." ); // "(telnet " + localHostAddress + " 2317)..." );
// } // }
ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client printStream.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 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"); printStream.println("You were connection #" + connectionCount + ", by my count");
// Print some information locally about the Socket connection. // Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.) // This includes the port and IP numbers on both sides (the socket pair.)
InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress localAddress = clientConnectionSocket.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress(); InetAddress remoteAddress = clientConnectionSocket.getInetAddress();
int localPort = clientConnection.getLocalPort(); int localPort = clientConnectionSocket.getLocalPort();
int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different? int remotePort = clientConnectionSocket.getPort(); // remember the prior question, why are 2 ports different?
// My socket pair connection looks like this, to localhost: // My socket pair connection looks like this, to localhost:
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6 // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6
...@@ -125,25 +129,34 @@ public class TcpExample2ConnectionCounting ...@@ -125,25 +129,34 @@ public class TcpExample2ConnectionCounting
// Why is first IP/port the same, while the second set has different ports? // Why is first IP/port the same, while the second set has different ports?
System.out.println("Socket pair (server, client): (( " + localAddress.toString() + ", " + localPort + " ), ( " + System.out.println("Socket pair (server, client): (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))"); remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("got another connection, #" + connectionCount); // report progress System.out.println("got another connection, #" + connectionCount); // report progress
// Notice the use of flush() and close(). Without // Notice the use of flush() to ensure that messages are sent immediately,
// the close() to Socket object may stay open for // rather than possibly getting buffered for the program's self-perceived efficiency.
// a while after the client has stopped needing this outputStream.flush();
// connection. Close() explicitly ends the connection.
ps.flush(); // Notice the use of optional close() when complete. Without a close() invocation,
// the clientConnectionSocket object may stay open for a while after the client
// has stopped needing this connection. Close() explicitly ends the connection.
// Try hiding this and see if anything different occurs.
clientConnectionSocket.close();
} }
} }
} }
catch(IOException e) catch(IOException ioe)
{ {
System.err.println("Problem with " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening System.err.println("Problem with " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening
System.err.println("Error: " + e); System.err.println("Error: " + ioe);
// 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 (ioe instanceof java.net.BindException)
System.err.println("*** Be sure to stop any other running instances of programs using this port!"); System.err.println("*** Be sure to stop any other running instances of programs using this port!");
} }
finally
{
// given the infinite-loop logic above, this step is "hard to reach" but a good practice nevertheless
System.out.println(TcpExample2ConnectionCounting.class.getName() + " has finished, adios!");
}
} }
} }
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