From bfa6e988d3ea277dc2a5289ae968fa3b1613063b Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Tue, 23 Jul 2024 10:42:53 -0700 Subject: [PATCH] improved comments and variable naming --- .../TcpExample2ConnectionCounting.java | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index de9a434599..03bb12bddf 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java +++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java @@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting try { // 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(" 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" ); @@ -76,18 +76,22 @@ public class TcpExample2ConnectionCounting int connectionCount = 0; // state variable 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. - // Stop the program somewhere else. + // If you want to stop or pause the program, go somewhere else. while(true) { - // serverSocket.accept() blocks! then proceeds once a connection is "accept"ed - try (Socket clientConnection = serverSocket.accept()) { // the accept() method blocks here until a client connects + // note that serverSocket.accept() first blocks! then after waiting, execution proceeds once a connection is "accept"ed + // 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 - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); + outputStream = clientConnectionSocket.getOutputStream(); // clientConnectionSocket is new, every time through, so reset the streams + printStream = new PrintStream(outputStream); // if (connectionCount == 1) // first time through, report connection // { @@ -104,19 +108,19 @@ public class TcpExample2ConnectionCounting // "(telnet " + localHostAddress + " 2317)..." ); // } - 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 + 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 - 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. // This includes the port and IP numbers on both sides (the socket pair.) - InetAddress localAddress = clientConnection.getLocalAddress(); - InetAddress remoteAddress = clientConnection.getInetAddress(); + InetAddress localAddress = clientConnectionSocket.getLocalAddress(); + InetAddress remoteAddress = clientConnectionSocket.getInetAddress(); - int localPort = clientConnection.getLocalPort(); - int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different? + int localPort = clientConnectionSocket.getLocalPort(); + int remotePort = clientConnectionSocket.getPort(); // remember the prior question, why are 2 ports different? // 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 @@ -125,25 +129,34 @@ public class TcpExample2ConnectionCounting // Why is first IP/port the same, while the second set has different ports? 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 - // Notice the use of flush() and close(). Without - // the close() to Socket object may stay open for - // a while after the client has stopped needing this - // connection. Close() explicitly ends the connection. - ps.flush(); + // Notice the use of flush() to ensure that messages are sent immediately, + // rather than possibly getting buffered for the program's self-perceived efficiency. + outputStream.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("Error: " + e); + System.err.println("Error: " + ioe); // 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!"); } + 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!"); + } } } -- GitLab