From 08a37ccac1547af76eaf493601ebb037616ceeb0 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@DESKTOP-2S09UKA> Date: Sun, 11 Aug 2019 17:29:58 -0700 Subject: [PATCH] show both HostName and HostAddress --- examples/src/TcpExamples/TcpExample3Server.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/src/TcpExamples/TcpExample3Server.java b/examples/src/TcpExamples/TcpExample3Server.java index 7bd494ba38..8afc2f5be3 100644 --- a/examples/src/TcpExamples/TcpExample3Server.java +++ b/examples/src/TcpExamples/TcpExample3Server.java @@ -20,6 +20,7 @@ import java.net.*; * and have the instructor display the socket pairs received. * * @author mcgredo + * @author brutzman */ public class TcpExample3Server { @@ -52,18 +53,24 @@ public class TcpExample3Server { // Print some information locally about the Socket connection. // This includes the port and IP numbers on both sides (the socket pair). - localAddress = clientConnection.getLocalAddress(); + localAddress = clientConnection.getLocalAddress(); remoteAddress = clientConnection.getInetAddress(); - localPort = clientConnection.getLocalPort(); - remotePort = clientConnection.getPort(); + localPort = clientConnection.getLocalPort(); + remotePort = clientConnection.getPort(); // 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 )) // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) // Why is the first IP/port the same, while the second set has different ports? - System.out.println("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " - + remoteAddress.toString() + ", " + remotePort + " ))"); + System.out.println("TcpExample3Server socket pair showing host name, address, port:"); + System.out.println(" (( " + + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + + if ( localAddress.getHostName().equals( localAddress.getHostAddress()) || + remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) + System.out.println(" note HostName matches address if host has no DNS name"); // Notice the use of flush() and try w/ resources. Without // the try w/ resources the Socket object may stay open for -- GitLab