1 package org.apache.commons.net.ntp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.IOException;
19 import java.net.DatagramPacket;
20 import java.net.InetAddress;
21 import org.apache.commons.net.DatagramSocketClient;
22
23 /***
24 * The NTPUDPClient class is a UDP implementation of a client for the
25 * Network Time Protocol (NTP) described in RFC 1305 as well as the
26 * Simple Network Time Protocol (SNTP) in RFC-2030. To use the class,
27 * merely open a local datagram socket with <a href="#open"> open </a>
28 * and call <a href="#getTime"> getTime </a> to retrieve the time. Then call
29 * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close </a>
30 * to close the connection properly.
31 * Successive calls to <a href="#getTime"> getTime </a> are permitted
32 * without re-establishing a connection. That is because UDP is a
33 * connectionless protocol and the Network Time Protocol is stateless.
34 *
35 * @author Jason Mathews, MITRE Corp
36 * @version $Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $
37 ***/
38
39 public final class NTPUDPClient extends DatagramSocketClient
40 {
41 /*** The default NTP port. It is set to 123 according to RFC 1305. ***/
42 public static final int DEFAULT_PORT = 123;
43
44 private int _version = NtpV3Packet.VERSION_3;
45
46 /***
47 * Retrieves the time information from the specified server and port and
48 * returns it. The time is the number of miliiseconds since
49 * 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
50 * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
51 * object that allows access to all the fields of the NTP message header.
52 * <p>
53 * @param host The address of the server.
54 * @param port The port of the service.
55 * @return The time value retrieved from the server.
56 * @exception IOException If an error occurs while retrieving the time.
57 ***/
58 public TimeInfo getTime(InetAddress host, int port) throws IOException
59 {
60
61 if (!isOpen())
62 {
63 open();
64 }
65
66 NtpV3Packet message = new NtpV3Impl();
67 message.setMode(NtpV3Packet.MODE_CLIENT);
68 message.setVersion(_version);
69 DatagramPacket sendPacket = message.getDatagramPacket();
70 sendPacket.setAddress(host);
71 sendPacket.setPort(port);
72
73 NtpV3Packet recMessage = new NtpV3Impl();
74 DatagramPacket receivePacket = recMessage.getDatagramPacket();
75
76
77
78
79
80
81
82 TimeStamp now = TimeStamp.getCurrentTime();
83
84
85
86 message.setTransmitTime(now);
87
88 _socket_.send(sendPacket);
89 _socket_.receive(receivePacket);
90
91 long returnTime = System.currentTimeMillis();
92
93 TimeInfo info = new TimeInfo(recMessage, returnTime, false);
94
95 return info;
96 }
97
98 /***
99 * Retrieves the time information from the specified server on the
100 * default NTP port and returns it. The time is the number of miliiseconds
101 * since 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
102 * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
103 * object that allows access to all the fields of the NTP message header.
104 * <p>
105 * @param host The address of the server.
106 * @return The time value retrieved from the server.
107 * @exception IOException If an error occurs while retrieving the time.
108 ***/
109 public TimeInfo getTime(InetAddress host) throws IOException
110 {
111 return getTime(host, NtpV3Packet.NTP_PORT);
112 }
113
114 /***
115 * Returns the NTP protocol version number that client sets on request packet
116 * that is sent to remote host (e.g. 3=NTP v3, 4=NTP v4, etc.)
117 *
118 * @return the NTP protocol version number that client sets on request packet.
119 * @see #setVersion(int)
120 ***/
121 public int getVersion()
122 {
123 return _version;
124 }
125
126 /***
127 * Sets the NTP protocol version number that client sets on request packet
128 * communicate with remote host.
129 *
130 * @param version the NTP protocol version number
131 ***/
132 public void setVersion(int version)
133 {
134 _version = version;
135 }
136
137 }