package TcpExamples;

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

/**
 * <p>
 * This program is only slightly more complex than {@link TcpExamples.TcpExample1Telnet}
 * and {@link TcpExamples.TcpExample2ConnectionCounting}, awaiting a socket response. 
 * The only thing it 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. Run the program via telnet (or nc)
 * several times and compare the socket pairs.
 * </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>
 * Then contact the other computer by using telnet (or nc)
 * </p>
 * <p>
 *  <code><b>telnet</b> [ipNumberOfServerLaptop] <b>2317</b></code>
 * </p>
 * <p>
 * and have the other system display the socket pairs received.
 * </p>
 * 
 * @see TcpExample3Client
 * @see <a href="../../../src/TcpExamples/TcpExample3TerminalLog.txt" target="blank">TcpExample3TerminalLog.txt</a>
 * @see <a href="../../../src/TcpExamples/TcpExample3ServerClientScreenshot.png" target="blank">TcpExample3ServerClientScreenshot.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 TcpExample3Server
{
    /** Default constructor */
    public TcpExample3Server()
    {
        // default constructor
    }

    /**
     * Program invocation, execution starts here
     * If already compiled, can run using console in directory ../../build/classes/ by invoking \
     *      java -classpath . TcpExamples.TcpExample3Server
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        try {
            
            // ServerSocket waits for a connection from a client. 
            // Notice that it is outside the loop; ServerSocket
            // needs to be made only once.
            System.out.println(TcpExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first
            
            ServerSocket serverSocket = new ServerSocket(2317);
            OutputStream os;
            PrintStream ps;
            InetAddress localAddress, remoteAddress;
            int localPort, remotePort;
            int serverLoopCount = 0;

            // Server is up and waiting (i.e. "blocked" or paused)
            // Loop, infinitely, waiting for client connections.
            // Stop the program somewhere else.
            while (true) { 
                
                // block until connected to a client
                try (Socket clientConnectionSocket = serverSocket.accept())
                {
                    serverLoopCount++; // increment at beginning of loop for reliability
                    
                    // Now hook everything up (i.e. set up the streams), Java style:
                    os = clientConnectionSocket.getOutputStream();
                    ps = new PrintStream(os);
                    ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client!
                    
                    // Print some information locally about the Socket connection.
                    // This includes the port and IP numbers on both sides (the socket pair).
                     localAddress = clientConnectionSocket.getLocalAddress();
                    remoteAddress = clientConnectionSocket.getInetAddress();
                        localPort = clientConnectionSocket.getLocalPort();
                       remotePort = clientConnectionSocket.getPort();
                       
                    System.out.print ("Server loop " + serverLoopCount + ": ");
                    
                    // 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.class.getName() + " 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
                    // a while after the client has stopped needing this
                    // connection. try w/ resources explicitly ends the connection.
                    ps.flush();
                    // like it or not, you're outta here!
                }
            }
        } catch (IOException e) {
            System.err.println("Problem with " + TcpExample3Server.class.getName() + " networking: " + 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!");
            }
        }
    }
}