Java Reference
In-Depth Information
}
/*NOTREACHED*/
}
}
To send a string across an arbitrary network connection, some authorities recommend send-
ing both the carriage return and the newline character; many protocol specifications require
that you do so. This explains the \r\n in the code. If the other end is a DOS program or a
Telnet-like program, it may be expecting both characters. On the other hand, if you are writ-
ing both ends, you can simply use println() —followed always by an explicit flush() be-
fore you read—-to prevent the deadlock of having both ends trying to read with one end's
data still in the PrintWriter 's buffer!
If you need to process binary data, use the data streams from java.io instead of the readers/
writers. I need a server for the DaytimeBinary program of Reading and Writing Binary
Data . In operation, it should look like the following:
C:\javasrc\network> java network.DaytimeBinary
Remote time is 3161316799
BASE_DIFF is 2208988800
Time diff == 952284799
Time on localhost is Sun Mar 08 19:33:19 GMT 2014
C:\javasrc\network> time/t
Current time is 7:33:23.84p
C:\javasrc\network> date/t
Current date is Sun 03-08-2014
C:\javasrc\network>
Well, it happens that I have such a program in my arsenal, so I present it in Example 16-4 .
Note that it directly uses certain public constants defined in the client class. Normally these
are defined in the server class and used by the client, but I wanted to present the client code
first.
Example 16-4. DaytimeServer.java
public
public class
DaytimeServer {
/** Our server-side rendezvous socket */
ServerSocket sock ;
/** The port number to use by default */
class DaytimeServer
Search WWH ::




Custom Search