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