Skip to content
Snippets Groups Projects
LeckieTcpExample2.java 5.12 KiB
package MV3500Cohort2021JulySeptember.homework1;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Very slightly more complex than example1. The only thing this 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
 * several times and compare the socket pairs.
 * 
 * <code>telnet localhost 2317</code>
 * 
 * If you're sophisticated you can contact the instructor's computer
 * while running this program.
 * 
 * <code>telnet ipOfServersLaptop 2317</code>
 * 
 * And have that machine display the socket pairs received.
 * @author mcgredo
 * @author brutzman
 * @author leckie
 */
public class LeckieTcpExample2 
{
    /**
     * Program invocation, execution starts here
     * @param args command-line arguments
     */
 public static void main(String[] args)
    {
        try
        {
            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("  enter (nc localhost 2317) or (telnet localhost 2317)..." );
			
            // 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
            int totalEntrantCount = 31; // spoofed entrants
            
            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)
            {
                // blocks! then proceeds once a connection is "accept"ed
                try (Socket clientConnection = serverSocket.accept()) {
                    connectionCount++; // got another one!
                    
                    OutputStream os = clientConnection.getOutputStream();
                    PrintStream ps = new PrintStream(os);
                    
                    ps.println("This client response was written by server " + LeckieTcpExample2.class.getName()); // to remote client
                    System.out.println("This server response was written by server " + LeckieTcpExample2.class.getName()); // to server console
                    
                            ps.println("This is your lottery attempt number #" + connectionCount + ", you are aplicant number " + totalEntrantCount + " to try and win. Keep trying!");
                    
                    totalEntrantCount = (totalEntrantCount + 79);
                    // Print some information locally about the Socket connection.
                    // This includes the port and IP numbers on both sides (the socket pair.)