Java Reference
In-Depth Information
Chapter 14, multiple clients can connect to the server because it can create a new
thread for each new client. For the log-in, for the exchange of commands, and for
the transmission of data, the two programs need a simple protocol so that they
can understand one another.
Both our server and client programs provide graphical user interfaces. The
server's GUI displays the status of the communications with the clients in a text
area and offers some basic controls. The client's GUI includes its own text area for
communications monitoring and a set of controls, plus it includes two histograms
to display the data.
15.3 The client/server interaction
In this section we present the main steps involved in our data client/server sys-
tem. The information flow is similar to that described in Section 13.8 and in
Chapter 14. We first start the DataServer application program, which uses a
ServerSocket to wait for clients to request connections at the appropriate port.
The client, an instance of DataClient , starts on another machine (or on the
same machine for convenience during testing) and connects to the server using a
socket.
The server initiates a simple log-in procedure by sending the string
Username: to the client, to which the client responds with a name string.
Here we do nothing with this string but you could easily add code to com-
pare the name to a list of authorized users; you could require a password as
well. On the client-side, you could use JPassworldField ,which is a sub-
class of JTextField designed for password input, to protect what is typed on
the screen. However, you would need to take additional measures to encrypt the
data flowing over the Internet if you want to ensure that the password remains
secret.
For each client, the server creates an instance of the Thread subclass called
DataWorker whose job is to communicate with the client over the socket and
perform the requested tasks. The DataServer passes the socket for the client
to DataWorker ,which sets up the I/O streams with the client and carries out all
of the communications with the client.
Similarly, DataClient creates an instance of DataClientWorker ,a
thread that is in charge of communications with the server. When the server
sends data to a DataClient ,itisthe DataClientWorker that receives and
processes the data. The DataClient then displays the results on its graphical
interface.
Figure 15.1 shows a diagram of possible data-taking scenarios such as the
DataWorker reading data from an instrument via a serial port connection. It
could also run an external program and read its output. In Chapter 23 we discuss
how Java programs can operate in the world outside of the JVM.
Search WWH ::




Custom Search