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

improved comments, also report to client what connection number they were

parent 2250b05d
No related branches found
No related tags found
No related merge requests found
package tcpexample2; package tcpexample2;
import java.io.*; import java.io.*;
...@@ -16,7 +15,7 @@ import java.net.*; ...@@ -16,7 +15,7 @@ import java.net.*;
* If you're sophisticated you can contact the instructor's computer * If you're sophisticated you can contact the instructor's computer
* while running this program. * while running this program.
* *
* telnet <ipOfServersLaptop> 2317 * <code>telnet ipOfServersLaptop 2317</code>
* *
* And have him display the socket pairs he got. * And have him display the socket pairs he got.
* @author mcgredo * @author mcgredo
...@@ -34,7 +33,8 @@ public class TcpExample2 ...@@ -34,7 +33,8 @@ public class TcpExample2
int connectionCount = 0; // state int connectionCount = 0; // state
ServerSocket serverSocket = new ServerSocket(2317); 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. // Loop, infinitely, waiting for client connections.
// Stop the program somewhere else. // Stop the program somewhere else.
...@@ -49,27 +49,28 @@ public class TcpExample2 ...@@ -49,27 +49,28 @@ public class TcpExample2
ps.println("This client response was written by server TcpExample2"); // to remote client ps.println("This client response was written by server TcpExample2"); // to remote client
System.out.println("This server response was written by server TcpExample2"); // to server console System.out.println("This server response was written by server TcpExample2"); // to server console
ps.println("You were connection #" + connectionCount + ", by my count");
// Print some information locally about the Socket // Print some information locally about the Socket
// connection. This includes the port and IP numbers // connection. This includes the port and IP numbers
// on both sides (the socket pair.) // on both sides (the socket pair.)
InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress(); InetAddress remoteAddress = clientConnection.getInetAddress();
int localPort = clientConnection.getLocalPort(); int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort(); int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different?
// 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 )) note IPv6
// 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 // Why is the first IP/port the same, while the second set has
// different ports? // different ports?
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + System.out.println("Socket pair: (( " + 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
...@@ -85,7 +86,5 @@ public class TcpExample2 ...@@ -85,7 +86,5 @@ public class TcpExample2
{ {
System.out.println("problem with networking: " + e); System.out.println("problem with networking: " + e);
} }
} }
} }
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