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, but they are never authenticated cryptographically. 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.

Methods

NameDescription
QueryAsync(CancellationToken)Queries the NTP server and returns the time response.

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.