From 082b3d37412790c11316a5a7529cf72ed9768b76 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Mon, 2 Aug 2021 19:29:55 -0700 Subject: [PATCH] show localhost IP number to facilitate connections over local area network (LAN, WAN) --- .../TcpExamples/TcpExample2ConnectionCounting.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java index 988aac7e8f..478508c9f7 100644 --- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java +++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java @@ -33,7 +33,16 @@ public class TcpExample2ConnectionCounting { 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(" enter (nc localhost 2317) or (telnet localhost 2317)..." ); + // Where are we? In other words, what is our host number? Advertise it + // https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java + String localHostAddress = Inet4Address.getLocalHost().toString(); + 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)..." ); // ServerSocket waits for a connection from a client. // Notice that it is outside the loop; ServerSocket needs to be made only once. @@ -42,7 +51,7 @@ public class TcpExample2ConnectionCounting 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. - + // Loop, infinitely, waiting for client connections. // Stop the program somewhere else. while(true) -- GitLab