6. |
Write a program that uses a socket to retrieve the correct time from a timeserver using the TIME protocol.
There are many servers on the Internet that provide a time service. The National Institute of Standards and Technology http://www.bldrdoc.gov/ maintains several timeservers that allow users to synchronize a clock.
One protocol used is the Time protocol. To use the Time protocol, a user connects on port 37 and sends a newline character. The server responds by returning a 32-bit unformatted binary number that represents the time in UTC seconds since January 1, 1900.
Write a program that connects to three of the NIST servers listed below, using the Time protocol. Convert the return value and create a Date() object set to the return string's time. Print out the return number and your Date() object. Use a SimpleDateFormat to format your date. Make sure you adjust the time to your correct time zone. Use verbose output while you are connecting to the server.
Hints:
Unlike the C language, Java does not have an unsigned data type to represent an unsigned 32-bit binary number. An int in Java represents a 32-bit signed binary number. Usually a larger type, in this case a long, is used to store an unsigned int. Unfortunately, the DataInputStream you need to use has only has a readUnsignedByte() method. A little clever programming and bit shifting must be used to convert the four bytes to a long that represented the 32-bit unsigned number.
Java stores dates as the number of milliseconds since January 1, 1970. The Time protocol returns the number of seconds since January 1, 1900. To compute the difference, create a Date object set to 01/01/1970 and a Date object set to 01/01/1900. Call getTime() on each date to retrieve the milliseconds. Then subtract to find the difference. Convert the difference to seconds and add it to the return value. Convert the corrected return value to milliseconds to create your Date object.
NIST Timeservers
time-a.nist.gov
| NIST, Gaithersburg, Maryland
|
time-b.nist.gov
| NIST, Gaithersburg, Maryland
|
time-a.timefreq.bldrdoc.gov
| NIST, Boulder, Colorado
|
time-b.timefreq.bldrdoc.gov
| NIST, Boulder, Colorado
|
time-c.timefreq.bldrdoc.gov
| NIST, Boulder, Colorado
|
utcnist.colorado.edu
| University of Colorado, Boulder
|
time.nist.gov
| NCAR, Boulder, Colorado
|
time-nw.nist.gov
| Microsoft, Redmond, Washington
|
nist1.datum.com
| Datum, San Jose, California
|
nist1.dc.glassey.com
| Abovenet, Virginia
|
nist1.ny.glassey.com
| Abovenet, New York City
|
nist1.sj.glassey.com
| Abovenet, San Jose, California
|
nist1.aol-ca.truetime.com
| TrueTime, AOL facility, Sunnyvale, California
|
nist1.aol-va.truetime.com
| TrueTime, AOL facility, Virginia
|
Answer:
|