What Is examples.ntp.NTPClient.java?

Q

What is examples.ntp.NTPClient.java? Can I use it to get the current time?

✍: FYIcenter.com

A

examples.ntp.NTPClient.java is an example program demonstrating how to use the NTPUDPClient class.

This program sends a Datagram client request packet to a Network time Protocol (NTP) service port on a specified server, retrieves the time, and prints it to standard output along with the fields from the NTP message header (e.g. stratum level, reference id, poll interval, root delay, mode, ...) See RFC 868 for details.

You can follow these steps to download, view and run examples.telnet.WeatherTelnet.java program.

1. Download commons-net-3.5-bin.zip file from Apache Commons Net.

2. Unzip all files from commons-net-3.5-bin.zip, and install them to C:\local. You should see C:\local\commons-net-3.5 folder with two JAR files and "examples" sub folder:

05/01/2016  11:37 PM           303,658 commons-net-3.5.jar
05/01/2016  11:37 PM            82,453 commons-net-examples-3.5.jar
05/01/2016  11:37 PM    <DIR>          examples

3. View C:\local\commons-net-3.5\examples\ntp\NTPClient.java with a text editor.

4. Run NTPClient.java in command window with both JAR files included in the classpath:

C:\fyicenter>\local\jdk-1.8.0\bin\java 
   -cp .;C:\local\commons-net-3.5\commons-net-3.5.jar
        ;C:\local\commons-net-3.5\commons-net-examples-3.5.jar 
   examples.ntp.NTPClient

Usage: NTPClient <hostname-or-address-list>   

5. Get the current time from clock.psu.edu NTP server:

C:\fyicenter>\local\jdk-1.8.0\bin\java 
   -cp .;C:\local\commons-net-3.5\commons-net-3.5.jar
        ;C:\local\commons-net-3.5\commons-net-examples-3.5.jar 
   examples.ntp.TimeClient clock.psu.edu

> clock.psu.edu/128.118.25.3
 Stratum: 2 (Secondary Reference; e.g. via NTP or SNTP)
 leap=0, version=3, precision=-23
 mode: Server (4)
 poll: 8 seconds (2 ** 3)
 rootdelay=37.26, rootdispersion(ms): 39.35
 Reference Identifier:  128.4.1.1 (rackety.udel.edu)
 Reference Timestamp:   dbdb3a67.688f5664  Sat, Nov 19 2016 15:53:59.408
 Originate Timestamp:   dbdb3b10.828f5c28  Sat, Nov 19 2016 15:56:48.510
 Receive Timestamp:     dbdb3b12.dcb96209  Sat, Nov 19 2016 15:56:50.862
 Transmit Timestamp:    dbdb3b12.dcbb478d  Sat, Nov 19 2016 15:56:50.862
 Destination Timestamp: dbdb3b10.96872b02  Sat, Nov 19 2016 15:56:48.588
 Roundtrip delay(ms)=78, clock offset(ms)=2313

 

Using commons-net.jar in Java Programs

⇒⇒FAQ for Apache commons-net.jar

2017-04-08, 443👍, 0💬