A server derived class application
The previous example set-up the necessary server infrastructure. All that
remains to create a real server application is to inherit from
ServerBaseClass and override the
run(Socket) method.
The
Socket parameter is the "pipeline" to the client that
has just established a connection. Input (read) and output (write) streams are
retrieved from the
Socket object. The "protocol" for exchanging data between
the server and the client is:
- The client writes four integers to the socket
- The server reads four integers from the socket
- The server "processes" the input to produce an answer
- The server returns the answer by writing two integers to the socket
- The client reads two integers from the socket
- The server waits for the next batch of input from the client
main() gets everything started by creating an instance of
the server derived class, and then calling the
startServer() method defined in the server base class.
The client application requests a connection on a pre-arranged port
number. It then proceeds to play the role reserved for it in the protocol
described above.