3. Comparison of Client/Server Model Implementation
3.3.2 Socket Implementation
When choosing a design for the socket system I felt it best to
opt for one that highlighted both the simplicity of its implementation
structure and the problems faced because of this simplicity.
The sockets implementation of the primality testing service is
formed of two parts - the server and the client. Embedded within
the server is the implementation of the services.
Server (NFServer.java)
The server only accepts one connection and it is this connection,
which it will use to communicate with the client rather than having
to create a child socket for this purpose. Because the socket
mechanism is more primitive than RMI, the creation and management
of the connection must be explicitly coded into the server's implementation.
The socket mechanism receives data in streams thus the client
arguments will be received as a stream of values (i.e. 1,2,3).
This stream needs to broken into three separate arguments before
they can be applied to the services. This task is performed by
using a BufferedReader to read the data on the socket, which is
then stored as a string variable. The string variable is divided
into three substrings by splitting the string at the divisions
between the values, the divisions are signified by a comma. Figure
10 illustrates the server method for this process:
clientLine = reader.readLine();
String a = clientLine.substring(0,clientLine.indexOf(','));
String b = clientLine.substring(clientLine.indexOf(',')+1, clientLine.lastIndexOf(','));
String c = clientLine.substring(clientLine.lastIndexOf(',')+1,
clientLine.length());
val1 = Long.valueOf(a).longValue();
val2 = Long.valueOf(b).longValue();
val3 = Long.valueOf(c).longValue();
Once the three variables have been separated they
can be processed by their respective service simply by calling
the service with the variables passed as an argument.
The processed variables are packaged together in a string container
and returned to the client.
Client (NFClient.java)
As with the server, the connection to the server must be created
and managed explicitly within the clients' implementation.
The primary role of the client is to accept three command line
arguments. If no arguments are specified then defaults are used.
The arguments are packaged together in a string container and
passed to the server. The processed results are returned in a
single string. Using a similar process to the server, the string
is split into sub strings containing the returned values. The
values are then displayed to the user.
Next Page