Java Reference
In-Depth Information
Chapter 15
Client/server with sockets
15.1 Introduction
In Chapter 14 we showed how to build a basic web server that sends files to
browsers or to browser-like clients. In this chapter we present a more interesting
socket-based client/server demonstration system that goes beyond just transmis-
sion of web pages. This new server sends data to a client, which then displays
the data in histograms. This type of client/server system could be quite useful
in various applications such as transmitting data from a remote experiment, run-
ning diagnostics under the direction of a client, installing calibration settings,
and controlling an instrument remotely. For demonstration purposes, our server
generates simulated data.
As in Chapter 14 we use sockets for our client/server communications [1-3].
Later chapters present RMI and CORBA based approaches. In a step-by-step
manner we describe the concepts and the code techniques used in the client and
server demonstration programs.
15.2 The client/server design
For the web server discussed in Chapter 14 we used socket communications.
The server monitors a port with a ServerSocket ,which returns a socket
for a client whenever one requests a connection. The socket is passed to a
thread that receives a request from the client for a file and then transmits that
file if it is available. The server then breaks the connection and the session
ends.
We can, however, create a client/server system in which the server maintains
a connection for as long as the client desires. To demonstrate the benefits of
such a system, we create a server program that sends simulated data to clients. A
client program displays the data that it obtains from the server, and its graphical
interface lets the user send requests to update the data.
A client session begins with a log-in procedure for the client with the server.
Then the server and client set up I/O streams for their communications. As in
448
Search WWH ::




Custom Search