
/**
 *  NetServer is a program that listens for a network connection.
 *  A computer must listen on a specific "port".  A port is just an
 *  integer between 2049 and 65535.  (Port numbers smaller than 2049 are
 *  reserved for standard network services.)  The user of this program
 *  is asked to specify the port number.  The computer then listens
 *  for a connection on that port.  (The connection will presumably
 *  come from the NetClient program running on another computer.)
 *  Once the connection is made, this server program waits to receive
 *  a one-line message from the client.  After receiving this message,
 *  it gets one line of input from the user and sends that line to
 *  the client.
 */


public class NetServer {

   public static void main(String[] args) {
      
      int port;       // The port on which the computer listen for a connection.
      String message; // A line of text transmitted over the network.
      
      // Get the port number from the user.
      
      System.out.println();
      System.out.print("What port number do you want to use? ");
      port = TextIO.getlnInt();
      
      // Wait for a connection request.
      
      Network.listen(port);
      
      // Wait for a message from the other side of the connection
      // and display it to the user.
      
      System.out.println("Waiting for a message...");
      message = Network.receive();
      System.out.println("RECEIVED MESSAGE:  " + message);
      
      // Get a line of text from the user and send it to the
      // other side of the connection.

      System.out.print("Enter your reply:  ");
      message = TextIO.getln();
      Network.send(message);
      System.out.println("Message sent.");
      System.out.println();
      
      // Close the connection.

      Network.close();
      
   } // end main()

} // end clsss NetServer
