Java Reference
In-Depth Information
Q Was it necessary to use the SocketCommand object to indicate which command to per-
form on the database?
A No. We chose this implementation for clarity: it was a nice way to enumerate over the
possible commands and demonstrate the command pattern. The drawback is that if
new commands were added to the database, the client would have to receive an
updated SocketCommand class in order to submit future requests since we would no
longer be able to deserialize older command objects on the server. (Of course, we
could check the serialversionID so that we could be backward compatible, but you
get the idea.) A different approach would be to embed the command as a string in the
DvdCommand . This way, as new commands are added, there would be no need to add
them to the SocketCommand class, but the server would be able to recognize the new
command.
Q Should I allow the user to change the port to be used in this application?
A While not strictly necessary, doing so is generally considered a good idea. Otherwise, if
another server application is using your desired port, your server application will be
unable to start.
Q When choosing a port number to use in my application (whether hard-coded or a
default value), are there any numbers I should avoid?
A The Internet Assigned Numbers Authority (IANA) specifies a list of well-known ports
(from port number 0 through 1023), registered ports (from 1024 through 49151), and
dynamic and/or private ports (from 49152 through 65535). It is recommended that you
choose a port number from the private port range to avoid conflict with any other
service. You should avoid using a well-known port since, depending on your operating
system, you may also find that you cannot run your server using a well-known port
without administrator privileges.
Q How can I perform system cleanup when a client disconnects?
A If the thread that is dedicated to that client is listening for a new command from the
client, then it will receive an exception when the client disconnects. Alternatively, if the
client disconnects before your server has an opportunity to respond to a previous
request, then the thread dedicated to that client will receive an exception when you
attempt to send the response to the client. In either of these cases, you can add
cleanup code to your catch block.
Alternatively, if you are only interested in cleaning up any outstanding locks, you can
use the thread dedicated to the client as a key within a WeakHashMap containing the
locks. When the client disconnects (and the thread dies), then eventually the lock
will be automatically removed from the WeakHashMap . Refer to Chapter 5 for more
information.
Q How can I automatically update all clients whenever a booking is made by any other
client on the server?
A This is not required for the Sun assignment; however, you would have to open an addi-
tional socket between the server and the client so that the server can send messages
to the client. You could combine this with the Observer design pattern (described in
Chapter 8) so that clients can register their desire to be notified of bookings.
Search WWH ::




Custom Search