Java Reference
In-Depth Information
public long getPayNum()
{
return payrollNum;
}
public String getSurname()
{
return surname;
}
public String getFirstNames()
{
return fi rstNames;
}
public void setSurname(String sName)
{
surname = sName;
}
}
Using methods covered in Chap.
2
, the above code may be adapted very easily to
produce a simple client-server application in which the server supplies personnel
details in response to client requests. The only difference is that, instead of sending
a series of strings from the server to the client(s), we shall now be passing an
ArrayList
. Consequently, we shall not be making use of a
PrintWriter
object in our
server. Instead, we shall need to create an
ObjectOutputStream
object. We do this by
passing the
OutputStream
object returned by our server's
Socket
object to the
ObjectOutputStream
constructor, instead of to the
PrintWriter
constructor (as was
done previously).
Example
Suppose that the
Socket
object is called
socket
and the output object is called
out
.
Then, instead of
PrintWriter out =
new PrintWriter(socket.getOutputStream(),true);
we shall have:
ObjectOutputStream out =
new ObjectOutputStream(socket.getOutputStream());
Though class
Personnel
was shown in previous examples as being in the same
fi le as the main code, this was merely for convenience of reference. Normally, of
course, we would hold this class in a separate fi le, in order to avoid code duplication
and to allow the class's reusability by other applications. The code for the server
(
PersonnelServer.java
) and the client (
PersonnelClient.java
) is shown below. You
will fi nd that the code for the server is an amalgamation of the fi rst half of
MessageServer.java
from Chap.
2
and the early part of
ArrayListSerialise.java
from
Search WWH ::
Custom Search