NtpServer Class

An NTP server that responds to NTP time queries. Supports NTPv3 and NTPv4, mirroring the version sent by the client.

public sealed class NtpServer : IDisposable

Remarks

The server answers from NtpServerOptions.TimeProvider and does not discipline that clock, so it is only as accurate as the machine it runs on. It implements no authentication: neither NTS (RFC 8915) nor symmetric keys.

Implements

Constructors

NameDescription
NtpServer(NtpServerOptions?)Initializes a new instance of the NtpServer class.

NtpServer(NtpServerOptions?)

NtpServer.cs

Initializes a new instance of the NtpServer class.

public NtpServer(NtpServerOptions? options = null)

Parameters

options NtpServerOptions?
Optional server configuration.

Properties

NameDescription
CompletionGets a task that completes when the listen loop stops, either because the server was disposed or the start token was cancelled, or faulted because the loop hit an unrecoverable error.
PortGets the port the server is listening on. Only valid after NtpServer.StartAsync has been called.

Completion

NtpServer.cs

Gets a task that completes when the listen loop stops, either because the server was disposed or the start token was cancelled, or faulted because the loop hit an unrecoverable error.

public Task Completion { get; }

Property value

Task

Exceptions

InvalidOperationException
The server has not been started.

Gets the port the server is listening on. Only valid after NtpServer.StartAsync has been called.

public int Port { get; }

Property value

int

Methods

NameDescription
Dispose()Releases the resources used by this instance and stops listening.
StartAsync(CancellationToken)Starts the NTP server and begins listening for client requests.

Dispose()

NtpServer.cs

Releases the resources used by this instance and stops listening.

public void Dispose()

StartAsync(CancellationToken)

NtpServer.cs

Starts the NTP server and begins listening for client requests.

public Task StartAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token that stops the server when cancelled.

Returns

Task A task that completes when the server has started listening.

Exceptions

InvalidOperationException
The server has already been started.