Java Reference
In-Depth Information
fDataServer.clientConnected (this);
// Get a data output stream for writing numerical
// data to the client
fDataOutputStream = new DataOutputStream
(fNetOutputStream);
return true;
} // serviceSetup
If the log-in procedure is successful, then a call back to the server informs it that
the connection for this client is successful and it should be added to the client
list.
The stream methods to send or receive strings and data involve several lines of
code since they can throw exceptions. Also, to insure that data does not get stuck
in a buffer, a flush() method is invoked. So DataWorker uses the following
utility methods for the I/O operations to reduce the lines of code in the rest of the
program:
... Continue in DataWorker ...
/** Utility method to read a whole text line. **/
String readNetInputLine () {
try {
return fNetInputReader.readLine ();
}
catch (IOException e) {
return null;
}
}
/**
* Output is wrapped with a PrintWriter, which doesn't
* throw IOException. So we invoke the checkError() method
* and then throw an exception if it detects an error.
**/
void writeNetOutputLine (String string) throws
IOException {
fPrintWriter.println (string);
if (fPrintWriter.checkError ()) throw new IOException ();
fPrintWriter.flush ();
if (fPrintWriter.checkError ()) throw new IOException ();
}
Search WWH ::




Custom Search