Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have the following codes in C#. I am using a server "asia.pool.ntp.org" to get the time returned. Is there any possibility that the "asia.pool.ntp.org" is down and it will return exception when I call the server?
Below are my codes:

C#
public static DateTime GetNetworkTime()
{
    //default Windows time server
    //asia.pool.ntp.org
    const string ntpServer = "asia.pool.ntp.org";

    // NTP message size - 16 bytes of the digest (RFC 2030)
    var ntpData = new byte[48];

    //Setting the Leap Indicator, Version Number and Mode values
    ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)

    var addresses = Dns.GetHostEntry(ntpServer).AddressList;

    //The UDP port number assigned to NTP is 123
    var ipEndPoint = new IPEndPoint(addresses[0], 123);
    //NTP uses UDP
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    socket.Connect(ipEndPoint);

    //Stops code hang if NTP is blocked
    socket.ReceiveTimeout = 5000;

    socket.Send(ntpData);
    socket.Receive(ntpData);
    socket.Close();

    //Offset to get to the "Transmit Timestamp" field (time at which the reply
    //departed the server for the client, in 64-bit timestamp format."
    const byte serverReplyTime = 40;

    //Get the seconds part
    ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);

    //Get the seconds fraction
    ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);

    //Convert From big-endian to little-endian
    intPart = SwapEndianness(intPart);
    fractPart = SwapEndianness(fractPart);

    var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);

    //**UTC** time
    var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);

    return networkDateTime.ToUniversalTime();
}
Posted
Comments
BacchusBeale 20-Apr-15 23:05pm    
I suggest using a Ping Request before trying to connect, so your application is not waiting for connection that is down. If it is down it cannot send any reply.
Sergey Alexandrovich Kryukov 21-Apr-15 0:34am    
There is no such thing as "return exception".
—SA
Jamie888 21-Apr-15 1:47am    
Yes sir. Sorry for my mistake. Originally I meant return error.

1 solution

Yes, there is an exception that is thrown if the NTP server is offline. If you look at the MSDN documentation for Socekt.Connect[^], you will see the list of exceptions that it could throw.

If my memory serves me correctly, you should get a SocketException with the details being connection timed out. The easiest way to test this is to modify your host file to point asia.pool.ntp.org to 127.0.0.1 so that it tries to use your local machine to pull the time information. This will simulate them being offline.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900