More .NET components

NTP and SNTP protocol

NTP (Network Time Protocol) is a network protocol for clock synchronization between computers. It is defined by 1305/5905 and runs over UDP port 123.

SNTP is a simplified and less accurate variant of NTP that uses the same protocol but doesn't require any state storage. It is defined by RFC 1305/1769/2030/4330.

Synchronizing system clock #

To synchronize local system time with an NTP time server, use SynchronizeSystemClock method:

CSharp

// create Ntp client instance
var client = new Rebex.Net.Ntp("test.rebex.net");

// synchronize local time with time server
client.SynchronizeSystemClock();

VisualBasic

' create Ntp client instance
Dim client = New Rebex.Net.Ntp("test.rebex.net")

' synchronize local time with time server
client.SynchronizeSystemClock()

Alternatively, the static variant of Ntp.SynchronizeSystemClock method makes it possible to synchronize system time with a single line of code:

CSharp

// synchronize system clock using SNTP protocol
Rebex.Net.Ntp.SynchronizeSystemClock("test.rebex.net");

VisualBasic

' synchronize system clock using SNTP protocol
Rebex.Net.Ntp.SynchronizeSystemClock("test.rebex.net")

Tip: For a list of publicly available NTP servers, check out The NTP Public Services Project.
However, if you use public NTP servers from your applications, please make sure that you comply with their usage policy.

System clock synchronization is supported on Windows, Windows CE and Linux operating systems.

Note: To update the system time, administrator privileges are needed. Check out the TimeWinFormClient sample code to see how to elevate the application to obtain them.

Determining time difference #

The result of SNTP request is the difference between the client's and server's current time.

CSharp

// create Ntp client instance
var client = new Rebex.Net.Ntp("test.rebex.net");

// get NTP response from server
NtpResponse response = client.GetTime();

// get local and remote time difference
NtpTimestampDifference offset = response.TimeOffset;

// calculate the adjusted local time
NtpTimestamp now = NtpTimestamp.Now + offset;

// the timestamp can be converted to local or UTC DateTime
DateTime localTime = now.ToLocalTime();
DateTime utcTime = now.ToUniversalTime();

// it can also be easily converted to "Unix time"
// (number of seconds elapsed since 1st Jan 1970)
int unixTime = now.ToUnixTime();

// it's possible to adjust the local system time
// by the time difference received from the server
Rebex.SystemClock.AdjustClock(offset);

VisualBasic

' create Ntp client instance
Dim client = New Rebex.Net.Ntp("test.rebex.net")

' get NTP response from server
Dim response As NtpResponse = client.GetTime()

' get local and remote time difference
Dim offset As NtpTimestampDifference = response.TimeOffset

' calculate the adjusted local time
Dim now As NtpTimestamp = NtpTimestamp.Now + offset

' the timestamp can be converted to local or UTC DateTime
Dim localTime As DateTime = now.ToLocalTime()
Dim utcTime As DateTime = now.ToUniversalTime()

' it can also be easily converted to "Unix time"
' (number of seconds elapsed since 1st Jan 1970)
Dim unixTime As Integer = now.ToUnixTime()

' it's possible to adjust the local system time
' by the time difference received from the server
Rebex.SystemClock.AdjustClock(offset)

Stratum, precision, and other info #

It's possible to retrieve more information from NTP time servers using the GetTime method:

CSharp

// create Ntp client instance
var client = new Rebex.Net.Ntp("test.rebex.net");

// get NTP response from server
NtpResponse response = client.GetTime();

// print some info
DateTime localTime = DateTime.Now;
DateTime serverTime = localTime.Add(response.TimeOffset.ToTimeSpan());
Console.WriteLine("Current local time: {0}", localTime);
Console.WriteLine("Current server time: {0}", serverTime);
Console.WriteLine("Time difference: {0}", response.TimeOffset);
Console.WriteLine("Protocol version: {0}", response.Packet.VersionNumber);
Console.WriteLine("Precision: {0}", response.Packet.Precision);
Console.WriteLine("Stratum: {0}", response.Stratum);

VisualBasic

' create Ntp client instance
Dim client = New Rebex.Net.Ntp("test.rebex.net")

' get NTP response from server
Dim response As NtpResponse = client.GetTime()

' print some info
Dim localTime As DateTime = DateTime.Now
Dim serverTime As DateTime = localTime.Add(response.TimeOffset.ToTimeSpan())
Console.WriteLine("Current local time: {0}", localTime)
Console.WriteLine("Current server time: {0}", serverTime)
Console.WriteLine("Time difference: {0}", response.TimeOffset)
Console.WriteLine("Protocol version: {0}", response.Packet.VersionNumber)
Console.WriteLine("Precision: {0}", response.Packet.Precision)
Console.WriteLine("Stratum: {0}", response.Stratum)

Version 3 and 4 #

By default, the Ntp object uses the most common version 3 of the SNTP/NTP protocol. Use Ntp.Version property to specify a different version. Several other options are tweakable as well.

CSharp

// create Ntp client instance
var client = new Rebex.Net.Ntp(hostname, 123);

// specify SNTP version to use
client.VersionNumber = 4;

// set response timeout to 5 seconds
client.Timeout = 5000;

// retrieve remote time, synchronize system clock, etc.

VisualBasic

' create Ntp client instance
Dim client = New Rebex.Net.Ntp(hostname, 123)

' specify SNTP version to use
client.VersionNumber = 4

' set response timeout to 5 seconds
client.Timeout = 5000

' retrieve remote time, synchronize system clock, etc.