Page 4 of 4
Getting the Time
Now we can wait for the time server to send a datagram back to us. First we need to initialise a new IPEndPoint object and a new SNTPpacket for the reply. We could reuse the old SNTP packet if you are trying to save time and storage.
SNTPpacket reply=new SNTPpacket();
Again for simplicity we use the blocking form of the receive method. Notice that this will hang forever if no packets are recieved back from the time server. In practice a much better idea is to use the non-blocking BeginRecieve method:
reply.buffer =UDP.Receive(ref remoteEP);
Now we can process the data in the buffer, assuming that a datagram was received and place the results in the SNTPpacket properties, again using a method we have yet to write:
We have to now write the ConvertFromBuffer method and this is slightly more complicated than the ConvertToBuffer method. However converting the first four bytes is easy enough and more or less just a reversal of the ConverToBuffer method.
public void ConvertFromBuffer()
Mode =(byte)( buffer & 0x7);
VN = (byte)((buffer >>3) & 0x7);
LI = (byte)((buffer >>6) & 0x3);
Stratum = buffer;
Poll = buffer;
Precision = buffer;
The next three 32bit values are also easy enough as we do not attempt any data conversions to more sophisticated data types:
RootDispersion = convertInt32(8);
RefId = Encoding.ASCII.
Next we have to convert the 64 bit time values and transfer them to the DateTime properties. Again it is simpler to suppose that we have a method that does the job and solve the problem of writing it later:
RefTimeStamp = ConvertFromSNTPTime(16);
where the ConvertFromSNTPTime(start) method converts the 8 bytes starting the "start" position in the buffer.
Finally we need to write the ConvertFromSNTPTime method. Again this is complicated by the need to reverse the byte order as we unpack the buffer into two temporary arrays one for the high value i.e. the integer number of seconds and one for the low value the fractional seconds:
byte tempHigh=new byte;
byte tempLow = new byte;
for (int i = 0; i < 4; i++)
tempHigh[i]=buffer[start + 3 - i];
tempLow[i]=buffer[start + 4 + 3 - i];
Now that the byte order is reversed we can use BitConverter to get the high and low 32bits into unsigned 32 bit integers:
UInt32 tempTime = BitConverter.ToUInt32(
UInt32 tempFrac = BitConverter.ToUInt32(
To convert this raw second count into a DateTime we can make things easy by first converting to a double that represents the total number of seconds, including any fractional portion since 1 Jan 1900:
double SNTPTime = tempTime+
Now we can convert this to a DateTime object and add the appropriate number of seconds to the start of the SNTP time count i.e. 1 Jan 1990:
return new DateTime(1900, 1, 1).
This completes the unpacking and all of the methods we have assumed were available have been written - time to try it all out.
Trying it out
Now we can try out the control. Add a command button the Form project and enter the following code:
private void button1_Click(
object sender, RoutedEventArgs e)
SNTP SntpTime = new SNTP();
SntpTime.TimeServer = "pool.ntp.org";
pool.,ntp.org uses a public server from the pool using a round robin algorithm. You can use any
If you need to know more, or find a more local server, look at http://www.ntp.org/ .
To access the code for this project, once you have registered, click on CodeBin.