NtpClient Class
An NTP client for querying NTP servers to retrieve accurate network time. Supports NTPv3 and NTPv4.
public sealed class NtpClientRemarks
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
| Name | Description |
|---|---|
| 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.csNtpClient(string, NtpClientOptions?)
NtpClient.csInitializes a new instance of the NtpClient class.
public NtpClient(string server, NtpClientOptions? options)Parameters
serverstring- The NTP server hostname or IP address.
optionsNtpClientOptions?- Optional configuration options.
Properties
| Name | Description |
|---|---|
| Server | Gets the host name or the IP address of the server. |
Server
NtpClient.csMethods
| Name | Description |
|---|---|
| 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.csQueries 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
optionsNtpMeasurementOptions?- The number of samples and the delay between them. Default is 4 samples, 2 seconds apart.
cancellationTokenCancellationToken- 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.csQueries 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
clientsIEnumerable<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.
optionsNtpMeasurementOptions?- The number of samples per server and the delay between them. Default is 4 samples, 2 seconds apart.
cancellationTokenCancellationToken- A cancellation token.
Returns
Task<NtpClockMeasurement> The combined clock offset, its bounds, and the measurement of each server.
Exceptions
ArgumentExceptionclientsis empty or containsnull.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.csQueries 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
cancellationTokenCancellationToken- 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.