The Client/Server paradigm has become
a dominant one for the Internet. In this model, the clients are
programs running on remote machines that communicate with a program
called the server that runs at a single site and responds to requests
from many clients. The server provides the clients with, say,
Web pages or database information.
Much of the World Wide Web is built on the
client/server paradigm.The clients are Web browsers run by many
millions of individual users, and the servers are the many Web
hosting systems running at the many host sites on the Web.
A single server at a single host can support
many hundreds or thousands or more of clients from around the
world. Large systems that serve hundreds of thousands of clients
balance the server load over multiple machines in an arrangement
called "server farms."
With Java you can build client/server systems
with sockets or with RMI (Remote Method
Invocation). In the following chapters of Part II we come back
to the client/server model repeatedly, though we will keep things
simple by considering a server to be a program running on a single
server computer.
In a socket based client/server system, a server
listens to a particular port for client applications sending requests
for connections. A ServerSocket
class is provided in Java that allows for a server to monitor
and answer such requests for 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.
The figure below 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.
The diagram above shows the basic aspects of a socket
based client/server system. The ServerSocket
instance watches for a client to connect with the particular port.
When a client request arrives, the ServerSocket object
sets up a Socket
instance for the connection and then spins off a thread process
to interact with the client via that socket.
A Java Web 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.
Remote method invocation (RMI)
provides for powerful distributed computing capabilities. Much
of the complicated machinery to make this happen is hidden from
the application programmer who can instead concentrate on the
task at hand. The portability of Java is a further advantage since
a distributed computing application can be developed that will
run with many different platforms with the same code.
References & Web
Resources