Java Reference
In-Depth Information
Figure 7-2. Iterative socket server lifecycle
Socket servers that can multitask can process multiple client requests simultaneously.
This is accomplished with a multithreaded socket server. If you are anticipating a lot of
requests, or requests take a long time to process, then multitasking is a very useful feature for
a socket server to have. The Denny's DVDs socket version is a multithreaded socket server.
Each client gets serviced with its own thread, which makes the socket solution operate
much like our RMI factory solution. We can thus be assured that each client has its own
DvdDatabase object. (See our discussion on the RMI factory in Chapter 6.) The next section
describes the details of our multitasking socket solution.
The Server Socket Class
The class java.net.ServerSocket allows you to create servers that listen to incoming connec-
tions on a specified port. A ServerSocket can send and receive data as well as act upon that
data. Figure 7-3 illustrates the lifecycle of a multithreaded server. The public constructors
for ServerSocket let you specify the port, queue length, and binding address. The server is
responsible for implementing the protocol, or the rules that both the client and server must
Search WWH ::




Custom Search