Java Reference
In-Depth Information
Figure 15.1
The
DataClient/DataServer
example demonstrates how remote
clients communicate through a network (e.g. a local network or the Internet) with a
server offering specialized services. When a client connects to
DataServer,
it
assigns an instance of
DataWorker
to attend to the client. Similarly, the
DataClient
application uses a
DataClientWorker
to communicate with server while the
DataClient
provides the user interface. In the example here, the server provides
simulated data but it could be modified for other tasks such as sending data files,
running external programs, or communicating with a sensor via a serial port (see
Chapter 23).
15.4 The
DataServer
Figure 15.2 shows the interface for the
DataServer
application. A text field
allows the user to set the port number that the clients must use to connect with
the server. A text area displays the status of the server and messages indicating
the various actions taken as connections to the clients occur and services are
provided. The utility method
println()
sends messages to the text area:
public void println (String str) {
fTextArea.append (str + "
\
n");
repaint ();
}
The
"
Start
"
button puts the server program into a server state by starting
a thread where a loop in the
run()
method connects to clients and assigns
instances of
DataWorker
to handle them. As with the web server in Chapter
14, the program uses a
ServerSocket
object to wait for clients to attempt to
make a socket connection with it on a given port.
Search WWH ::
Custom Search