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

debug testing with Terry and Don

parent 082b3d37
No related branches found
No related tags found
No related merge requests found
...@@ -33,16 +33,10 @@ public class TcpExample2ConnectionCounting ...@@ -33,16 +33,10 @@ public class TcpExample2ConnectionCounting
{ {
System.out.println("TcpExample2ConnectionCounting has started and is waiting for a connection."); System.out.println("TcpExample2ConnectionCounting 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");
// Where are we? In other words, what is our host number? Advertise it System.out.println(" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)");
// https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java System.out.println(" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or" );
String localHostAddress = Inet4Address.getLocalHost().toString(); System.out.println(" Windows enter (telnet IPv4_Address 2317) or Mac enter (nc IPv4_Address 2317) for LAN operation" );
System.out.println("Local host address is " + localHostAddress); System.out.println("TcpExample2ConnectionCounting server standing by..." );
if (localHostAddress.contains("/"))
localHostAddress = localHostAddress.substring(localHostAddress.indexOf("/")+1);
// show localhost IP number to facilitate connections over local area network (LAN, WAN)
System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317) for local operation" );
System.out.println(" enter (nc " + localHostAddress + " 2317) or " +
"(telnet " + localHostAddress + " 2317)..." );
// ServerSocket waits for a connection from a client. // ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket needs to be made only once. // Notice that it is outside the loop; ServerSocket needs to be made only once.
...@@ -51,18 +45,33 @@ public class TcpExample2ConnectionCounting ...@@ -51,18 +45,33 @@ public class TcpExample2ConnectionCounting
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.
// Loop, infinitely, waiting for client connections. // Loop, infinitely, waiting for client connections.
// Stop the program somewhere else. // Stop the program somewhere else.
while(true) while(true)
{ {
// blocks! then proceeds once a connection is "accept"ed // serverSocket.accept() blocks! then proceeds once a connection is "accept"ed
try (Socket clientConnection = serverSocket.accept()) { try (Socket clientConnection = serverSocket.accept()) {
connectionCount++; // got another one! connectionCount++; // got another one!
OutputStream os = clientConnection.getOutputStream(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
// if (connectionCount == 1) // first time through, report connection
// {
// // Where are we? In other words, what is our host number? Advertise it.
// // Note that we use the serverSocket to get address, since host may have multiple network connections.
// // https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
// String localHostAddress = clientConnection.getInetAddress().getHostAddress();
// System.out.println("Local host address is " + localHostAddress);
// if (localHostAddress.contains("/"))
// localHostAddress = localHostAddress.substring(localHostAddress.indexOf("/")+1);
// // show localhost IP number to facilitate connections over local area network (LAN, WAN)
// System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317) for local operation" );
// System.out.println(" enter (nc " + localHostAddress + " 2317) or " +
// "(telnet " + localHostAddress + " 2317)..." );
// }
ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client 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 System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
...@@ -83,7 +92,7 @@ public class TcpExample2ConnectionCounting ...@@ -83,7 +92,7 @@ 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: (( " + 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
......
Invocation instructions: Invocation instructions:
1. run/debug TcpExample2.java 1. run/debug TcpExample2ConnectionCounting.java
2. console: nc localhost 2317
alternate: telnet localhost 2217 2. Windows: ipconfig
MacOS: ifconfig
3. Windows: telnet localhost 2317
MacOS: nc localhost 2317
Program responses: Program responses:
=================================================== ===================================================
run: run-single:
TcpExample2ConnectionCounting has started and is waiting for a connection: telnet localhost 2317 TcpExample2ConnectionCounting has started and is waiting for a connection.
This server response was written by server TcpExample2ConnectionCounting help: https://savage.nps.edu/Savage/developers.html#telnet
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57630 )) Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)
Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or
Windows enter (telnet IPv4_Address 2317) or Mac enter (nc IPv4_Address 2317) for LAN operation
TcpExample2ConnectionCounting server standing by...
This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55424 ))
got another connection, #1 got another connection, #1
This server response was written by server TcpExample2ConnectionCounting This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57631 )) Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55425 ))
got another connection, #2 got another connection, #2
This server response was written by server TcpExample2ConnectionCounting This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 57633 )) Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55426 ))
got another connection, #3 got another connection, #3
This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55427 ))
got another connection, #4
This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.153.125, 55428 ))
got another connection, #5
This server response was written by server TcpExamples.TcpExample2ConnectionCounting
Socket pair (server, client): (( /172.20.154.38, 2317 ), ( /172.20.154.38, 49308 ))
got another connection, #6
=================================================== ===================================================
don@it154928 /cygdrive/c/Program Files/NetBeans 8.2
$ telnet localhost 2317 $ telnet localhost 2317
Trying ::1...
Connected to localhost.
Escape character is '^]'.
This client response was written by server TcpExample2
You were connection #1, by my count
Connection closed by foreign host.
don@it154928 /cygdrive/c/Program Files/NetBeans 8.2 This client response was written by server TcpExamples.TcpExample2ConnectionCounting
$ telnet localhost 2317 You were connection #6, by my count
Trying ::1...
Connected to localhost. Connection to host lost.
Escape character is '^]'. ===================================================
This client response was written by server TcpExample2 \ No newline at end of file
You were connection #2, by my count
Connection closed by foreign host.
don@it154928 /cygdrive/c/Program Files/NetBeans 8.2
$ telnet localhost 2317
Trying ::1...
Connected to localhost.
Escape character is '^]'.
This client response was written by server TcpExample2
You were connection #3, by my count
Connection closed by foreign host.
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