FileDocCategorySizeDatePackage
NTPUDPClient.javaAPI DocApache Commons NET 1.4.1 API5268Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.ntp

NTPUDPClient

public final class NTPUDPClient extends org.apache.commons.net.DatagramSocketClient
The NTPUDPClient class is a UDP implementation of a client for the Network Time Protocol (NTP) described in RFC 1305 as well as the Simple Network Time Protocol (SNTP) in RFC-2030. To use the class, merely open a local datagram socket with open and call getTime to retrieve the time. Then call close to close the connection properly. Successive calls to getTime are permitted without re-establishing a connection. That is because UDP is a connectionless protocol and the Network Time Protocol is stateless.
author
Jason Mathews, MITRE Corp
version
$Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $

Fields Summary
public static final int
DEFAULT_PORT
The default NTP port. It is set to 123 according to RFC 1305.
private int
_version
Constructors Summary
Methods Summary
public TimeInfogetTime(java.net.InetAddress host, int port)
Retrieves the time information from the specified server and port and returns it. The time is the number of miliiseconds since 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305. This method reads the raw NTP packet and constructs a TimeInfo object that allows access to all the fields of the NTP message header.

param
host The address of the server.
param
port The port of the service.
return
The time value retrieved from the server.
exception
IOException If an error occurs while retrieving the time.


                                                                                                  
           
    
        // if not connected then open to next available UDP port
        if (!isOpen())
        {
            open();
        }

        NtpV3Packet message = new NtpV3Impl();
        message.setMode(NtpV3Packet.MODE_CLIENT);
        message.setVersion(_version);
        DatagramPacket sendPacket = message.getDatagramPacket();
        sendPacket.setAddress(host);
        sendPacket.setPort(port);

        NtpV3Packet recMessage = new NtpV3Impl();
        DatagramPacket receivePacket = recMessage.getDatagramPacket();

        /*
         * Must minimize the time between getting the current time,
         * timestamping the packet, and sending it out which
         * introduces an error in the delay time.
         * No extraneous logging and initializations here !!!
         */
        TimeStamp now = TimeStamp.getCurrentTime();

        // Note that if you do not set the transmit time field then originating time
        // in server response is all 0's which is "Thu Feb 07 01:28:16 EST 2036".
        message.setTransmitTime(now);

        _socket_.send(sendPacket);
        _socket_.receive(receivePacket);

        long returnTime = System.currentTimeMillis();
        // create TimeInfo message container but don't pre-compute the details yet
        TimeInfo info = new TimeInfo(recMessage, returnTime, false);

        return info;
    
public TimeInfogetTime(java.net.InetAddress host)
Retrieves the time information from the specified server on the default NTP port and returns it. The time is the number of miliiseconds since 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305. This method reads the raw NTP packet and constructs a TimeInfo object that allows access to all the fields of the NTP message header.

param
host The address of the server.
return
The time value retrieved from the server.
exception
IOException If an error occurs while retrieving the time.

        return getTime(host, NtpV3Packet.NTP_PORT);
    
public intgetVersion()
Returns the NTP protocol version number that client sets on request packet that is sent to remote host (e.g. 3=NTP v3, 4=NTP v4, etc.)

return
the NTP protocol version number that client sets on request packet.
see
#setVersion(int)

        return _version;
    
public voidsetVersion(int version)
Sets the NTP protocol version number that client sets on request packet communicate with remote host.

param
version the NTP protocol version number

        _version = version;