NtpClient Class
Namespace
Assembly
Represents UDP socket used to communicate with RFC4330-compliant SNTP/NTP server.
public class NtpClient : IDisposable
Implements
Remarks
See project homepage for guidance on how to use GuerrillaNtp.
Most applications should just call GetCorrectionOffset()
after instantiating this class. Method Query()
can be used to obtain additional details stored in reply NtpPacket
.
This class holds unmanaged resources (the socket) and callers are responsible
for calling Dispose()
when they are done,
perhaps by instantiating this class in using
block.
It is application responsibility to be a good netizen, which most importantly means using reasonable polling intervals and exponential backoff when querying public NTP server.
Constructors
Creates new NtpClient
from server's IP address and optional port.
Creates new NtpClient
from server endpoint.
Properties
Gets or sets the timeout for SNTP queries.
Methods
Releases all resources held by NtpClient
.
Queries the SNTP server and returns correction offset.
Queries the SNTP server with default options.
Queries the SNTP server with configurable NtpPacket
request.
NtpClient(IPAddress, Int32) Constructor
Creates new NtpClient
from server's IP address and optional port.
public NtpClient(IPAddress address, int port = 123);
Parameters
address
IPAddress
IP address of remote SNTP server
port
Int32
Port of remote SNTP server. Default is 123 (standard NTP port).
See also
NtpClient(IPEndPoint) Constructor
Creates new NtpClient
from server endpoint.
public NtpClient(IPEndPoint endpoint);
Parameters
endpoint
IPEndPoint
Endpoint of the remote SNTP server.
See also
Timeout Property
Gets or sets the timeout for SNTP queries.
public TimeSpan Timeout { get; set; }
Value
Timeout for SNTP queries. Default is one second.
Dispose() Method
Releases all resources held by NtpClient
.
public void Dispose();
Remarks
NtpClient
holds reference to Socket
,
which must be explicitly released to avoid memory leaks.
GetCorrectionOffset() Method
Queries the SNTP server and returns correction offset.
public TimeSpan GetCorrectionOffset();
Returns
Offset that should be added to local time to match server time.
Exceptions
Thrown when the server responds with invalid reply packet.
Thrown when no reply is received before Timeout
is reached
or when there is an error communicating with the server.
Remarks
Use this method if you just want correction offset from the server.
Call Query()
to obtain NtpPacket
with additional information besides NtpPacket.CorrectionOffset
.
See also
Query() Method
Queries the SNTP server with default options.
public NtpPacket Query();
Returns
SNTP reply packet returned by the server.
Exceptions
Thrown when the server responds with invalid reply packet.
Thrown when no reply is received before Timeout
is reached
or when there is an error communicating with the server.
Remarks
Use this method to obtain additional details from the returned NtpPacket
besides NtpPacket.CorrectionOffset
.
If you just need the correction offset, call GetCorrectionOffset()
instead.
You can customize request packed by calling Query(NtpPacket)
.
See also
Query(NtpPacket) Method
Queries the SNTP server with configurable NtpPacket
request.
public NtpPacket Query(NtpPacket request);
Parameters
request
NtpPacket
SNTP request packet to use when querying the network time server.
Returns
SNTP reply packet returned by the server.
Exceptions
Thrown when the request packet is invalid or when the server responds with invalid reply packet.
Thrown when no reply is received before Timeout
is reached
or when there is an error communicating with the server.
Remarks
NtpPacket()
constructor
creates valid request packet, which you can further customize.
If you don't need any customization of the request packet, call Query()
instead.
Returned NtpPacket
contains correction offset in
NtpPacket.CorrectionOffset
property.