1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.examples.ntp;
19
20 import java.io.IOException;
21 import java.net.InetAddress;
22 import java.net.SocketException;
23 import java.net.UnknownHostException;
24 import java.text.NumberFormat;
25 import java.time.Duration;
26
27 import org.apache.commons.net.ntp.NTPUDPClient;
28 import org.apache.commons.net.ntp.NtpUtils;
29 import org.apache.commons.net.ntp.NtpV3Packet;
30 import org.apache.commons.net.ntp.TimeInfo;
31 import org.apache.commons.net.ntp.TimeStamp;
32
33
34
35
36
37
38
39
40
41
42
43
44 public final class NTPClient {
45
46 private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
47
48 public static void main(final String[] args) {
49 if (args.length == 0) {
50 System.err.println("Usage: NTPClient <hostname-or-address-list>");
51 System.exit(1);
52 }
53
54 final NTPUDPClient client = new NTPUDPClient();
55
56 client.setDefaultTimeout(Duration.ofSeconds(10));
57 try {
58 client.open();
59 for (final String arg : args) {
60 System.out.println();
61 try {
62 final InetAddress hostAddr = InetAddress.getByName(arg);
63 System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
64 final TimeInfo info = client.getTime(hostAddr);
65 processResponse(info);
66 } catch (final IOException ioe) {
67 ioe.printStackTrace();
68 }
69 }
70 } catch (final SocketException e) {
71 e.printStackTrace();
72 }
73
74 client.close();
75 }
76
77
78
79
80
81
82 public static void processResponse(final TimeInfo info) {
83 final NtpV3Packet message = info.getMessage();
84 final int stratum = message.getStratum();
85 final String refType;
86 if (stratum <= 0) {
87 refType = "(Unspecified or Unavailable)";
88 } else if (stratum == 1) {
89 refType = "(Primary Reference; e.g., GPS)";
90 } else {
91 refType = "(Secondary Reference; e.g. via NTP or SNTP)";
92 }
93
94 System.out.println(" Stratum: " + stratum + " " + refType);
95 final int version = message.getVersion();
96 final int li = message.getLeapIndicator();
97 System.out.println(" leap=" + li + ", version=" + version + ", precision=" + message.getPrecision());
98
99 System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
100 final int poll = message.getPoll();
101
102 System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " seconds" + " (2 ** " + poll + ")");
103 final double disp = message.getRootDispersionInMillisDouble();
104 System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble()) + ", rootdispersion(ms): " + numberFormat.format(disp));
105
106 final int refId = message.getReferenceId();
107 String refAddr = NtpUtils.getHostAddress(refId);
108 String refName = null;
109 if (refId != 0) {
110 if (refAddr.equals("127.127.1.0")) {
111 refName = "LOCAL";
112 } else if (stratum >= 2) {
113
114
115
116 if (!refAddr.startsWith("127.127")) {
117 try {
118 final InetAddress addr = InetAddress.getByName(refAddr);
119 final String name = addr.getHostName();
120 if (name != null && !name.equals(refAddr)) {
121 refName = name;
122 }
123 } catch (final UnknownHostException e) {
124
125
126
127 refName = NtpUtils.getReferenceClock(message);
128 }
129 }
130 } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
131 refName = NtpUtils.getReferenceClock(message);
132
133 }
134
135 }
136 if (refName != null && refName.length() > 1) {
137 refAddr += " (" + refName + ")";
138 }
139 System.out.println(" Reference Identifier:\t" + refAddr);
140
141 final TimeStamp refNtpTime = message.getReferenceTimeStamp();
142 System.out.println(" Reference Timestamp:\t" + refNtpTime + " " + refNtpTime.toDateString());
143
144
145 final TimeStamp origNtpTime = message.getOriginateTimeStamp();
146 System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString());
147
148 final long destTimeMillis = info.getReturnTime();
149
150 final TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
151 System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString());
152
153
154 final TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
155 System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString());
156
157
158 final TimeStamp destNtpTime = TimeStamp.getNtpTime(destTimeMillis);
159 System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString());
160
161 info.computeDetails();
162 final Long offsetMillis = info.getOffset();
163 final Long delayMillis = info.getDelay();
164 final String delay = delayMillis == null ? "N/A" : delayMillis.toString();
165 final String offset = offsetMillis == null ? "N/A" : offsetMillis.toString();
166
167 System.out.println(" Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset);
168 }
169
170 }