Java Reference
In-Depth Information
Figure 13.5 Schematic of a socket based client/server system. The ServerSocket in
the server program monitors a particular port for requests for connections from
clients on the network , which could range from a local area network to the global
Internet. The client programs connect via an instance of Socket using the server's
IP address and port. When the ServerSocket makes a connection with the client, it
returns an instance of Socket that is given to a thread process, which we call a
Worker ,toattend to the requests of the client. The ServerSocket returns to
monitoring the port for new requests for connections and spins off a worker process
for each client that connects.
connections. The client sends the request for a connection by creating a socket
with the host name and port for that server as discussed in the previous section.
Figure 13.5 shows a diagram illustrating the basics of a socket-based client/
server system. The ServerSocket instance listens for a client to connect to the
particular port. When a client request arrives, the ServerSocket object sets up
a Socket instance for the connection and then spins off a new thread to interact
with the client via that socket. Many clients can therefore be served since each
client has an independent thread dedicated to it.
AJavaweb server would be built with this kind of socket-oriented client/server
approach. In Chapters 14-15 we discuss this approach further and show how
to build custom servers for applications such as providing access to data at a
remote device. We also use such a server in Chapter 24 on an embedded Java
processor.
In Chapters 16-20 we look at a more direct approach to communications
between clients and servers. Using tools like RMI and CORBA, a Java program
on one machine can invoke a method in an object on another machine just as if
the code was running on the local platform. A client, for example, could call a
method on a server program to obtain some parameter of interest. The server, in
turn, can invoke methods in the client.
Search WWH ::




Custom Search