Java Reference
In-Depth Information
will use the server's hostname (or IP address) and port to request a connection. On the server
side of the network, java.net.ServerSocket is the class that listens for connection requests
from socket clients. The class java.net.Socket is one of the endpoints of a socket connection.
Both the server and the client will have an instance of java.net.Socket per socket connection.
Why Use Sockets
Choosing between sockets and RMI is one of the biggest decisions you will have to make for
the certification project. But each choice comes with its own advantages and trade-offs. If the
JavaRanch web forum on the certification exam is any indication, it seems that most develop-
ers opt for the RMI solution. In fact, in the first edition of this topic we recommended using
RMI instead of sockets just because we believed at the time that the approach seemed more
intuitive from a Java programmer's perspective. However, since then we have backed off from
this assertion. Either RMI or sockets will work fine and both are reasonably straightforward to
implement. But for the adventurous at heart, let's discuss some of the reasons you might opt
for a socket solution.
Socket servers are more scalable and faster than RMI servers. Acquiring a remote object
handle requires a network call in the form of a Registry lookup. In addition, each remote
object has a client-side proxy, or stubs, which effectively adds an object layer between the
client and the actual remote object implementation class. This communication overhead is
paid for in terms of performance.
Typically one of the reasons mentioned for not using sockets is the implicit contract, or
application protocol, that needs to be in place between the socket clients and the socket
server. We discuss this in more detail later in this chapter in the section “The Application Pro-
tocol.” However, what if the protocol is very simple? In cases where the socket interface is
simple, sockets are an excellent choice.
One final reason worth considering a socket implementation involves threading. Each
socket client request in our socket solution spawns a new thread, which we can use to main-
tain a lock on the DvdDatabase . This means we do not have to worry about implementing a
factory as we did with RMI. The very nature of our socket solution avoids this potentially
complicating issue. Of course, you now have to deal with the problem of developing a multi-
threaded socket server, but help is on the way! Denny's DVDs is a multithreaded socket server,
and we describe our server implementation later in this chapter.
Socket Basics
In this section, we will explore the types of sockets available to the Java programmer as well as
some of the fundamental concepts related to socket development.
Addresses
An address is a unique number used to identify a device connected to a network, much like a
postal address identifies the location of a building in a city. An IP address is a unique number,
usually consisting of 12 digits or more, that is technically a 32-bit or 128-bit unsigned number
used by the Internet protocol (i.e., IP) and for sending messages between socket addresses on
TCP- or UDP-based networks. The java.net.InetAddress class is the Java class that encapsu-
lates an IP address for use with Java sockets.
Search WWH ::




Custom Search