Share My Creation SNTPClient Class

This is a basic Simple Network Time Protocol (SNTP) client Class in B4J based on UDPSocket that can be used to retrieve the current time from NIST Internet Time Servers or local NTP servers on your LAN.

It includes a 4 second "delay" to prevent overuse (minimum of 4.1 seconds between Query method calls) with an Unblock event to signal when another request can be made. See the NIST page linked above.

The Response event returns the requested current date and time. Response also returns the Source of the time as reported by the time server (if any). Typical values are listed in RFC 5905 as:

B4X:
+------+----------------------------------------------------------+
| ID  | Clock Source  |
+------+----------------------------------------------------------+
| GOES | Geosynchronous Orbit Environment Satellite  |
| GPS  | Global Position System  |
| GAL  | Galileo Positioning System  |
| PPS  | Generic pulse-per-second  |
| IRIG | Inter-Range Instrumentation Group  |
| WWVB | LF Radio WWVB Ft. Collins, CO 60 kHz  |
| DCF  | LF Radio DCF77 Mainflingen, DE 77.5 kHz  |
| HBG  | LF Radio HBG Prangins, HB 75 kHz  |
| MSF  | LF Radio MSF Anthorn, UK 60 kHz  |
| JJY  | LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz  |
| LORC | MF Radio LORAN C station, 100 kHz  |
| TDF  | MF Radio Allouis, FR 162 kHz  |
| CHU  | HF Radio CHU Ottawa, Ontario  |
| WWV  | HF Radio WWV Ft. Collins, CO  |
| WWVH | HF Radio WWVH Kauai, HI  |
| NIST | NIST telephone modem  |
| ACTS | NIST telephone modem  |
| USNO | USNO telephone modem  |
| PTB  | European telephone modem  |
+------+----------------------------------------------------------+

These are not particularly useful in most programs though.

The attached demo shows use of the SNTPClient Class, which is included in the attached archive.


Reposted updated version 30-Dec-2013.
 

Attachments

  • sshot1.png
    sshot1.png
    10.4 KB · Views: 1,852
  • SNTPClient Demo.zip
    8.2 KB · Views: 482
Last edited:

magoandroid

Member
Licensed User
Longtime User
Hi dilettante,
thank you for sharing your work.
I am a "dilettante (ITA)" and your work helps me to learn.

Greetings
MAgo
 

dilettante

Active Member
Licensed User
Longtime User
It seems to work properly for a simple client request for current time to one second precision. I made no effort to deal with NTP's network delay compensation.

Actually I have attempted to use 0.5 second precision, but I am making an assumption about the format of the "fractions of a second" UInt value returned by the server. I may be handling this incorrectly.

More details can be found in RFC 5905 Network Time Protocol Version 4: Protocol and Algorithms Specification even though I am using Version 3 in order to operate against more servers in the wild. Technical information - NTP Data Packet also offers a useful summary of the datagram format.

One quirk regarding the Reference Identifier value I return as "Source" is that some servers seem to return "garbage" values there. Thus there might be no point in even extracting and returning it as I do in this Class.
 
Top