Lab-1

Socket Programming

Perform the following tasks:

  1. Develop a Root Transfer Protocol socket server. The server must wait for a client, serve that client, and then ends. Recall that an RTP client can make any number of requests but once an empty line is sent, the connection will be terminated and the server will shut down.

    Hint: It is always a good idea to separate the concerns. The management aspect of the server (listening on a port, waiting for a client, and server shutdown) should be in one class, the one with the main method. The request handling aspect of the server (streams, computing the square root, etc.) should be in a separate class, one called Handler. The following pattern is thus highly recommanded:
      1. Determine the port to listen on
      2. Handle or acknowledge exceptions
      3. handler = new Handler()
      4. serversocket = new ServerSocket(port)
      5. socket = serversocket.accept()
      6. handler.handle(socket)
      7. clean exit
    
      Handler class has handle(Socket socket) which:
      1. Get the raw I/O streams from socket
      2. Wrap the streams as appropriate
      3. Follow Protocol to generate a response to the request
      4. clean exit


  2. Develop a network server according to the following specs:



    Hint: There is no method in Java that generates such huge prime numbers but there is one that can determine, albeit not with certainty, if a given huge integer is prime.


  3. Enhance the server developed in the previous question as follows:


    Hint: Recall that the firewall and the log fall in the concern of the server manager, not the protocol handler. Hence, your handler class must be able to talk back to your server. You must arrange things so this is possible.


The following additional task is optional:

  1. Enhance the server developed in the previous question by making it serve multiple clients at the same time.