Java Reference
In-Depth Information
Reading from Servers with Sockets
Let's begin with a simple example. You're going to connect to the daytime server at the
National Institute for Standards and Technology (NIST) and ask it for the current time.
This protocol is defined in RFC 867 . Reading that, you see that the daytime server listens
on port 13, and that the server sends the time in a human-readable format and closes
the connection. You can test the daytime server with Telnet like this:
$ telnet time.nist.gov 13
Trying 129.6.15.28...
Connected to time.nist.gov.
Escape character is '^]'.
56375 13-03-24 13:37:50 50 0 0 888.8 UTC(NIST) *
Connection closed by foreign host.
The line “56375 13-03-24 13:37:50 50 0 0 888.8 UTC(NIST)” is sent by the daytime
server. When you read the Socket 's InputStream , this is what you will get. The other
lines are produced either by the Unix shell or by the Telnet program.
RFC 867 does not specify any particular format for the output other than that it be
human readable. In this case, you can see this connection was made on March 24, 2013,
at 1:37: 50 P.M., Greenwich Meantime. More specifically, the format is defined as JJJJJ
YY-MM-DD HH:MM:SS TT L H msADV UTC(NIST) OTM where:
JJJJJ is the “Modified Julian Date” (i.e., it is the number of whole days since midnight
on November 17, 1858).
YY-MM-DD is the last two digits of the year, the month, and the current day of
month.
HH:MM:SS is the time in hours, minutes, and seconds in Coordinated Universal
Time (UTC, essentially Greenwich Mean Time).
TT indicates whether the United States is currently observing on Standard Time or
Daylight Savings Time: 00 means standard time; 50 means daylight savings time.
Other values count down the number of days until the switchover.
L is a one-digit code that indicates whether a leap second will be added or subtracted
at midnight on the last day of the current month: 0 for no leap second, 1 to add a
leap second, and 2 to subtract a leap second.
H represents the health of the server: 0 means healthy, 1 means up to 5 seconds off,
2 means more than 5 seconds off, 3 means an unknown amount of inaccuracy, and
4 is maintenance mode.
msADV is a number of milliseconds that NIST adds to the time it sends to roughly
compensate for network delays. In the preceding code, you can see that it added
888.8 milliseconds to this result, because that's how long it estimates it's going to
take for the response to return.
Search WWH ::




Custom Search