A zero-dependency Java CLI that queries NTP time servers using a hand-rolled
implementation of the RFC 5905 wire protocol — no NTP library, just java.net
UDP sockets and java.time. It reports how far your local clock is from each
server, with the offset and round-trip delay computed from the four timestamps
of the exchange.
$ java -jar ntp-client.jar pool.ntp.org time.google.com
Server: pool.ntp.org (162.159.200.1)
Stratum: 2
Leap: 0 (no warning)
Reference: 192.168.1.1
Root delay: 1.000 ms
Root disp.: 2.000 ms
Server time: 2026-11-30T15:00:00Z
Offset: +12.000 ms (local clock behind)
Delay: 45.000 ms
Verdict: good (<50 ms)
Every machine that syncs its clock runs NTP, but the protocol itself is small enough to implement from scratch: a 48-byte packet, four timestamps, and two formulas. This project is that implementation, written to be read — the packet layout, the timestamp math, and the validation rules are all explicit in the code rather than hidden behind a library.
- Hand-rolled wire protocol. The 48-byte packet is encoded and decoded
field by field (
NtpPacket), including the 16.16 fixed-point root delay/dispersion fields and the 64-bit fixed-point timestamps. No third-party NTP code is involved. - Era-aware timestamps. NTP seconds count from 1900 and wrap every 136
years (next wrap: 2036).
NtpTimestampresolves the era so dates after 2036 decode correctly instead of landing in 1900. - Responses are validated before they are trusted. A reply must be 48 bytes, in server mode, at the requested NTP version, and its originate timestamp must match the one we sent — otherwise a stray or replayed packet could be mistaken for our answer.
- Kiss-o'-death is a hard failure. A stratum-0 reply (e.g.
RATE,DENY) raises an error with the server's code instead of producing a bogus reading. - A
Clockis injected, not read directly, so tests can freeze or step time and the originate/receive timestamps always come from the same clock — otherwise the offset math would be meaningless.
From the four timestamps of one exchange — t0 when the client sent the request, t1 when the server received it, t2 when the server sent the reply, t3 when the client received it (RFC 5905, section 8):
offset θ = ((t1 − t0) + (t2 − t3)) / 2
delay δ = (t3 − t0) − (t2 − t1)
A positive offset means the local clock is behind the server. With
--samples N, the reported offset is the median of N queries and the delay
is the minimum — the standard way to filter out network jitter.
./build.sh # compile
java -jar out/ntp-client.jar pool.ntp.org # query one server
java -jar out/ntp-client.jar pool.ntp.org time.google.com --samples 5 --json
java -jar out/ntp-client.jar pool.ntp.org --timeout 1000 --retries 2 --version 3| Option | Default | Meaning |
|---|---|---|
--timeout <ms> |
3000 | UDP timeout per attempt |
--retries <n> |
1 | Extra attempts per sample |
--samples <n> |
1 | Queries per server (median offset, min delay) |
--version <3|4> |
4 | NTP version to request |
--port <n> |
123 | UDP port (use a high port to test against a local mock server) |
--json |
— | Print results as a JSON array |
--help |
— | Show usage |
Exit codes: 0 every server answered, 1 at least one server failed,
2 usage error.
./build.sh test37 JUnit 5 tests. The client logic is tested against an in-process fake
transport, because this sandbox blocks outbound UDP entirely (EPERM even to
127.0.0.1) — the real DatagramSocketTransport is exercised by one test
gated behind NTP_INTEGRATION=1 (run it on a machine where UDP works).
The suite covers timestamp conversion (including the 2036 era wrap),
packet round-trips, offset/delay math, kiss-of-death and unsynchronized
handling, timeouts, and CLI parsing.
src/main/java/com/subhanmalik/ntpclient/
├── NtpTimestamp.java # NTP 64-bit timestamps <-> Instant, era handling
├── NtpPacket.java # 48-byte RFC 5905 packet encode/decode
├── NtpClient.java # UDP query, validation, offset/delay, sampling
├── PacketTransport.java # transport seam (real UDP vs in-process fake)
├── DatagramSocketTransport.java # the real UDP transport
├── HostResolver.java # DNS seam (lets tests fake resolution failures)
├── NtpResult.java # one query's outcome + verdict bands
├── NtpException.java # timeout / malformed / KoD failures
├── Report.java # text and JSON rendering
└── Main.java # CLI parsing and exit codes
- JDK 17+
- No runtime dependencies. JUnit 5 (console standalone) is fetched from Maven Central only when running tests.
MIT — see LICENSE.