PocketPC - TimeGetter

PocketPC - TimeGetter

Connect get time and (S)NTP server info including stratum.

The sample show:

  • Connect SNTP/NTP server.
  • Get server timestamp.
  • Get protocol version supported by the server.
  • Show server stratum.

C#

Ntp ntp = new Ntp(serverName.Text);

// Since we want to know the highest version the server supports, we
// must declare the highest ourselves (otherwise it would use
// compatibility mode).
ntp.VersionNumber = 4;

// Even an emulator takes a couple of seconds to contact its host
// computer - let's take some reserve.
ntp.Timeout = 10000;

UpdateStatus("Sending time request...");
NtpResponse response = ntp.GetTime();
NtpPacket packet = response.Packet;
UpdateStatus("");

protocolVersion.Text = packet.VersionNumber.ToString();
stratum.Text = packet.Stratum.ToString();

txtReport.Text = string.Format(
    "We sent the request to the server at {0} UTC and recieved its response at {1} UTC.\r\n\r\n",
    packet.OriginateTimestamp,
    packet.DestinationTimestamp
);

txtReport.Text += string.Format(
    "The server at {0} has stratum {1} and runs NTP version {2}.\r\n\r\n",
    serverName.Text,
    packet.Stratum,
    packet.VersionNumber
);

txtReport.Text += string.Format(
    "It received our request at {0} UTC and responded to it at {1} UTC.\r\n\r\n",
    packet.ReceiveTimestamp,
    packet.TransmitTimestamp
);

DateTime current = DateTime.Now;

DateTime exact = current + response.TimeOffset.ToTimeSpan();
txtReport.Text += string.Format(
    "The current local time is {0} and it should be adjusted to {1}.",
    current,
    exact
);

VB.NET

Dim ntpClient As Ntp = New Ntp(serverName.Text)

' Since we want to know the highest version the server supports, we
' must declare the highest ourselves (otherwise it would use
' compatibility mode).
ntpClient.VersionNumber = 4

' Even an emulator takes a couple of seconds to contact its host
' computer - let's take some reserve.
ntpClient.Timeout = 10000

UpdateStatus("Sending time request...")
Dim response As NtpResponse = ntpClient.GetTime()
Dim packet As NtpPacket = response.Packet
UpdateStatus("")

protocolVersion.Text = packet.VersionNumber.ToString()
stratum.Text = packet.Stratum.ToString()

txtReport.Text = String.Format( _
    "We sent the request to the server at {0} UTC and recieved its response at {1} UTC." & vbCrLf & vbCrLf, _
    packet.OriginateTimestamp, _
    packet.DestinationTimestamp _
)

txtReport.Text += String.Format( _
    "The server at {0} has stratum {1} and runs NTP version {2}." & vbCrLf & vbCrLf, _
    serverName.Text, _
    packet.Stratum, _
    packet.VersionNumber _
)

txtReport.Text += String.Format( _
    "It received our request at {0} UTC and responded to it at {1} UTC." & vbCrLf & vbCrLf, _
    packet.ReceiveTimestamp, _
    packet.TransmitTimestamp _
)

Dim current As DateTime = DateTime.Now
Dim exact As DateTime = current.Add(response.TimeOffset.ToTimeSpan())
txtReport.Text += String.Format( _
    "The current local time is {0} and it should be adjusted to {1}.", _
    current, _
    exact _
)

Included in

See also: