NtpClient Class

An NTP client for querying NTP servers to retrieve accurate network time. Supports NTPv3 and NTPv4.

public sealed class NtpClient

Remarks

Responses are validated as described on NtpClientOptions.ValidateResponse. They are authenticated only when NtpClientOptions.AuthenticationKey is set: otherwise, an attacker who can inject packets on the path to the server can still control the reported time.

Constructors

NameDescription
NtpClient(string)Initializes a new instance of the NtpClient class using default options (NTPv4, port 123).
NtpClient(string, NtpClientOptions?)Initializes a new instance of the NtpClient class.

NtpClient overloads

NtpClient(string)

NtpClient.cs

Initializes a new instance of the NtpClient class using default options (NTPv4, port 123).

public NtpClient(string server)
Parameters
server string
The NTP server hostname or IP address.

NtpClient(string, NtpClientOptions?)

NtpClient.cs

Initializes a new instance of the NtpClient class.

public NtpClient(string server, NtpClientOptions? options)
Parameters
server string
The NTP server hostname or IP address.
options NtpClientOptions?
Optional configuration options.

Properties

NameDescription
ServerGets the host name or the IP address of the server.

Gets the host name or the IP address of the server.

public string Server { get; }

Property value

string

Methods

NameDescription
MeasureAsync(NtpMeasurementOptions?, CancellationToken)Queries the NTP server several times and keeps the sample with the shortest round-trip delay, whose offset is the most accurate, as the clock filter of RFC 5905 does.
MeasureAsync(IEnumerable<NtpClient>, NtpMeasurementOptions?, CancellationToken)Queries several NTP servers, discards the ones whose time disagrees with the majority, and combines the others, as the selection and combine algorithms of RFC 5905 do.
QueryAsync(CancellationToken)Queries the NTP server and returns the time response.

MeasureAsync overloads

MeasureAsync(NtpMeasurementOptions?, CancellationToken)

NtpClient.cs

Queries the NTP server several times and keeps the sample with the shortest round-trip delay, whose offset is the most accurate, as the clock filter of RFC 5905 does.

net10.0

public Task<NtpServerMeasurement> MeasureAsync(NtpMeasurementOptions? options = null, CancellationToken cancellationToken = default)

net11.0

public Task<NtpServerMeasurement> MeasureAsync(NtpMeasurementOptions? options = null, CancellationToken cancellationToken = default)
Parameters
options NtpMeasurementOptions?
The number of samples and the delay between them. Default is 4 samples, 2 seconds apart.
cancellationToken CancellationToken
A cancellation token.
Returns

Task<NtpServerMeasurement> The samples, the best one, and the jitter and root distance of the measurement.

Exceptions
TimeoutException
No query got a valid reply in time.
AggregateException
No query got a valid reply, for differing reasons.

MeasureAsync(IEnumerable<NtpClient>, NtpMeasurementOptions?, CancellationToken)

NtpClient.cs

Queries several NTP servers, discards the ones whose time disagrees with the majority, and combines the others, as the selection and combine algorithms of RFC 5905 do.

net10.0

public static Task<NtpClockMeasurement> MeasureAsync(IEnumerable<NtpClient> clients, NtpMeasurementOptions? options = null, CancellationToken cancellationToken = default)

net11.0

public static Task<NtpClockMeasurement> MeasureAsync(IEnumerable<NtpClient> clients, NtpMeasurementOptions? options = null, CancellationToken cancellationToken = default)
Parameters
clients IEnumerable<NtpClient>
One client per server, each with its own options. Query at least 3 servers, so that a majority can outvote one that is wrong, and do not query the same server twice: each client is one vote.
options NtpMeasurementOptions?
The number of samples per server and the delay between them. Default is 4 samples, 2 seconds apart.
cancellationToken CancellationToken
A cancellation token.
Returns

Task<NtpClockMeasurement> The combined clock offset, its bounds, and the measurement of each server.

Exceptions
ArgumentException
clients is empty or contains null.
TimeoutException
No server gave a valid reply in time.
AggregateException
No server gave a valid reply, for differing reasons.
NtpClockSelectionException
Servers answered, but no majority of them agrees on the time.
Remarks

The servers are queried concurrently. A server that does not answer is listed in NtpClockMeasurement.FailedServers.

QueryAsync(CancellationToken)

NtpClient.cs

Queries the NTP server and returns the time response.

net10.0

public Task<NtpResponse> QueryAsync(CancellationToken cancellationToken = default)

net11.0

public Task<NtpResponse> QueryAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task<NtpResponse> The NTP response containing timestamps and computed offset/delay.

Exceptions

TimeoutException
Every resolved address timed out.
AggregateException
Every resolved address failed, for differing reasons.