Skip to content
Snippets Groups Projects
TcpExample2ConnectionCounting.java 8.06 KiB
package TcpExamples;

import java.io.*;
import java.net.*;

/**
 * <p>
 * This program is only slightly more complex than {@link TcpExample1Telnet}, 
 * counting each connection instead of immediately exiting after the first connection. 
 * The only thing this program does differently is introduce a loop into the response,
 * so you don't have to restart the program after one response.
 * Also, it prints out the socket pair the server sees.
 * </p>
 * <p>
 * Suggestion: connect to this program via telnet several times, 
 * comparing the socket-pair responses each time.
 * </p>
 * <p>
 * <b><code>telnet localhost 2317</code></b>
 * </p>
 * <p>
 * If you're paying attention (and sophisticated!) you can contact the
 * another person's computer in class while running this program.  This is
 * a good way to check local-area network (LAN) connectivity.  
 * To learn that local computer's IPv4 Address, type
 * </p>
 * <p>
 * <b><code>ipconfig</code></b>
 * </p>
 * <p>
 * (On the NPS campus you might get a ipOfServerLaptop number like 172.20.148.215).
 * The someone else (or even that person) can run
 * </p>
 * <p>
 * <b><code>telnet ipOfServerLaptop 2317</code></b>
 * </p>
 * <p>
 * If this program is running, then that machine displays the current counter value of
 * socket pairs received.
 * </p>
 * @see <a href="../../../src/TcpExamples/TcpExample2ConnectionCountingTerminalLog.txt" target="blank">TcpExample2ConnectionCountingTerminalLog.txt</a>
 * @see <a href="../../../src/TcpExamples/TcpExample2ConnectionCountingScreenshot.png" target="blank">TcpExample2ConnectionCountingScreenshot.png</a>
 * @see <a href="https://savage.nps.edu/Savage/developers.html#Cygwin" target="blank">Savage Developers Guide: Cygwin</a>
 * @see <a href="https://savage.nps.edu/Savage/developers.html#telnet" target="blank">Savage Developers Guide: telnet</a>
 * @see <a href="https://en.wikipedia.org/wiki/Ipconfig" target="blank">Wikipedia: ipconfig</a>
 * 
 * @author mcgredo
 * @author brutzman@nps.edu
 */
public class TcpExample2ConnectionCounting 
{
    /** Default constructor */
    public TcpExample2ConnectionCounting()
    {
        // default constructor
    }
    /**
     * Program invocation, execution starts here
     * @param args command-line arguments
     */
    public static void main(String[] args)
    {
        try
        {
            // welcome aboard messages
            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("  Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)");
            System.out.println("  Windows enter (telnet localhost 2317)    or Mac enter (nc localhost 2317)    for loopback operation, or" );
            System.out.println("  Windows enter (telnet IPv4_Address 2317) or Mac enter (nc IPv4_Address 2317) for LAN operation" );
            System.out.println("TcpExample2ConnectionCounting server standing by..." );
			
            // ServerSocket waits for a connection from a client. 
            // Notice that it is outside the loop; ServerSocket needs to be made only once.
			
            int connectionCount = 0; // state variable
            
            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)
            {
                // serverSocket.accept() blocks! then proceeds once a connection is "accept"ed
                try (Socket clientConnection = serverSocket.accept()) { // the accept() method blocks here until a client connects
                    connectionCount++; // got another one! a client has connected
                    
                    OutputStream os = clientConnection.getOutputStream();
                    PrintStream ps = new PrintStream(os);
                    
//                    if (connectionCount == 1) // first time through, report connection
//                    {
//                        // Where are we?  In other words, what is our host number? Advertise it.
//                        // Note that we use the serverSocket to get address, since host may have multiple network connections.
//                        // https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
//                        String localHostAddress = clientConnection.getInetAddress().getHostAddress();
//                        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)..." );
//                    }
                    
                    ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client
                    System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
                    
                            ps.println("You were connection #" + connectionCount + ", by my count");
                    
                    // Print some information locally about the Socket connection.
                    // This includes the port and IP numbers on both sides (the socket pair.)
                    
                    InetAddress  localAddress = clientConnection.getLocalAddress();
                    InetAddress remoteAddress = clientConnection.getInetAddress();
                    
                    int  localPort = clientConnection.getLocalPort();
                    int remotePort = clientConnection.getPort();       // remember the prior question, why are 2 ports different?
                    
                    // 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 )) note IPv6
                    // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
                    //
                    // Why is first IP/port the same, while the second set has different ports?
                    
                    System.out.println("Socket pair (server, client): (( " + localAddress.toString() + ", " + localPort + " ), ( " +
                            remoteAddress.toString() + ", " + remotePort + " ))");
                    
                    System.out.println("got another connection, #" + connectionCount); // report progress
                    
                    // Notice the use of flush() and close(). Without
                    // the close() to Socket object may stay open for
                    // a while after the client has stopped needing this
                    // connection. Close() explicitly ends the connection.
                    ps.flush();
                }
            }
        }
        catch(IOException e)
        {
            System.err.println("Problem with  " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening
            System.err.println("Error: " + e);
            // Provide more helpful information to user if exception occurs due to running twice at one time
            if (e instanceof java.net.BindException)
                System.err.println("*** Be sure to stop any other running instances of programs using this port!");
        }
    }
}