Java Reference
In-Depth Information
If the client successfully connects to the server, it spins off the socket to an
instance of the
Thread
subclass
DataClientWorker
,which handles commu-
nications with the server. After the connection is made, the text on the
“
Start
„
button becomes
“
Stop
„
.Ifthe user clicks on the
“
Stop
„
button, the
stop()
method is invoked, via the
actionPerformed()
method, and this tells the
DataClientWorker
to break the connection and die.
When the
DataClientWorker
receives data from the server, it calls back
to the
DataClient
via the
setData (int[] data)
method to pass the
data on. This method uses the
pack()
method in the
Histogram
class (see
Section 6.11) to display the values in the array for the top histogram in the user
interface. For the data channel (i.e. the data array index) given in the interface
text field, the distribution of values are displayed in the second histogram.
15.7 The
DataClientWorker
As we indicated in Figure 15.1, communication with the server is mostly han-
dled by the
DataClientWorker
object. This
Thread
subclass first opens the
streams to the server and then carries out a simple log-in procedure with the
server by passing the user name to it. See the
doConnection()
and
login()
methods in the code snippet shown below. Note that, as with
DataServer
, the
class holds some utility methods for the I/O operations.
...
In the class
DataClientWorker ....
/** Remain in a loop to monitor the I/O from the server.
* Display the data.
**/
public void run () {
// The socket connection was made by the caller, now
// set up the streams and do a login
try {
if (!doConnection ()) {
fDataClient.println (" Connection/login failed");
return;
}
}
catch (IOException ioe) {
fDataClient.println (" I/O exception with serve:" +
ioe);
}
int num
-
channels = -1;
Search WWH ::
Custom Search