Skip to content
Snippets Groups Projects
Commit 08a37cca authored by brutzman's avatar brutzman
Browse files

show both HostName and HostAddress

parent b64f8bc0
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ import java.net.*; ...@@ -20,6 +20,7 @@ import java.net.*;
* and have the instructor display the socket pairs received. * and have the instructor display the socket pairs received.
* *
* @author mcgredo * @author mcgredo
* @author brutzman
*/ */
public class TcpExample3Server { public class TcpExample3Server {
...@@ -52,18 +53,24 @@ public class TcpExample3Server { ...@@ -52,18 +53,24 @@ public class TcpExample3Server {
// Print some information locally about the Socket connection. // Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair). // This includes the port and IP numbers on both sides (the socket pair).
localAddress = clientConnection.getLocalAddress(); localAddress = clientConnection.getLocalAddress();
remoteAddress = clientConnection.getInetAddress(); remoteAddress = clientConnection.getInetAddress();
localPort = clientConnection.getLocalPort(); localPort = clientConnection.getLocalPort();
remotePort = clientConnection.getPort(); remotePort = clientConnection.getPort();
// My socket pair connection looks like this, to localhost: // 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, 54876 ))
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) // 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? // Why is the first IP/port the same, while the second set has different ports?
System.out.println("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " System.out.println("TcpExample3Server socket pair showing host name, address, port:");
+ remoteAddress.toString() + ", " + remotePort + " ))"); 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 // Notice the use of flush() and try w/ resources. Without
// the try w/ resources the Socket object may stay open for // the try w/ resources the Socket object may stay open for
......
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