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

show localhost IP number to facilitate connections over local area network (LAN, WAN)

parent bb78d6db
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,16 @@ public class TcpExample2ConnectionCounting ...@@ -33,7 +33,16 @@ 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");
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. // 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.
...@@ -42,7 +51,7 @@ public class TcpExample2ConnectionCounting ...@@ -42,7 +51,7 @@ 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)
......
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