More .NET components

Time protocol

Time protocol provides a site-independent, machine readable date and time. It was defined in 1983 by RFC 868 and runs over TCP or UDP port 37.

Although it's not as accurate as more sophisticated SNTP/NTP protocol, it can run over TCP (unlike SNTP/NTP), which makes it useful in scenarios where UDP is not available.

Synchronizing system clock #

To synchronize local system time with a Daytime server, use SynchronizeSystemClock method:

CSharp

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

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

VisualBasic

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

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

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

CSharp

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

VisualBasic

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

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.

Getting server time #

To retrieve the current time from a Time server, call the GetTime method:

CSharp

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

// get current time at the server
DateTime now = client.GetTime();

VisualBasic

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

' get current time at the server
Dim now As DateTime = client.GetTime()

TCP and UDP #

Time protocol can be used over TCP and UDP protocols. Several other options are tweakable as well.

CSharp

// create Time client instance
var client = new Rebex.Net.Time(hostname, 37);

// use TCP instead of default UDP
client.UseTcp = true;

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

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

VisualBasic

' create Time client instance
Dim client = New Rebex.Net.Time(hostname, 37)

' use TCP instead of default UDP
client.UseTcp = True

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

' retrieve remote time, synchronize system clock, etc.