org.apache.commons.net.ntp

## Class NTPUDPClient

- java.lang.Object
- - org.apache.commons.net.DatagramSocketClient
  - - org.apache.commons.net.ntp.NTPUDPClient

-     public final class NTPUDPClient
      extends 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.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_PORT` The default NTP port.

    - ### Fields inherited from class org.apache.commons.net.DatagramSocketClient

      `_isOpen_, _socket_, _socketFactory_, _timeout_`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NTPUDPClient()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `TimeInfo` | `getTime(InetAddress host)` Retrieves the time information from the specified server on the default NTP port and returns it.
    `TimeInfo` | `getTime(InetAddress host, int port)` Retrieves the time information from the specified server and port and returns it.
    `int` | `getVersion()` Returns the NTP protocol version number that client sets on request packet that is sent to remote host (e.g.
    `void` | `setVersion(int version)` Sets the NTP protocol version number that client sets on request packet communicate with remote host.

    - ### Methods inherited from class org.apache.commons.net.DatagramSocketClient

      `close, getCharset, getCharsetName, getDefaultTimeout, getLocalAddress, getLocalPort, getSoTimeout, isOpen, open, open, open, setCharset, setDatagramSocketFactory, setDefaultTimeout, setSoTimeout`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_PORT

          public static final int DEFAULT_PORT

      The default NTP port. It is set to 123 according to RFC 1305.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### NTPUDPClient

          public NTPUDPClient()

  - ### Method Detail

    - #### getTime

          public TimeInfo getTime(InetAddress host)
                           throws IOException

      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.

      Parameters:
        `host` - The address of the server.
      Returns:
        The time value retrieved from the server.
      Throws:
        `IOException` - If an error occurs while retrieving the time.

    - #### getTime

          public TimeInfo getTime(InetAddress host,
                                  int port)
                           throws IOException

      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.

      Parameters:
        `host` - The address of the server.
        `port` - The port of the service.
      Returns:
        The time value retrieved from the server.
      Throws:
        `IOException` - If an error occurs while retrieving the time or if received packet does not match the request.

    - #### getVersion

          public int getVersion()

      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.)
      Returns:
        the NTP protocol version number that client sets on request packet.
      See Also:
        `setVersion(int)`

    - #### setVersion

          public void setVersion(int version)

      Sets the NTP protocol version number that client sets on request packet communicate with remote host.
      Parameters:
        `version` - the NTP protocol version number

Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.