Mountaineer sync System Clock with time-server


I have Mountaineer Ethernet board and try to work with it in Internet.
I can connect to and receive data using TCP with nearby computer.

But I can’t sync system clock with any NTP server. I try to use code from Mountaineer site, try to use another codes from this site, but UDP not working - ntpSocket.Poll returns false. :frowning:

Is there some tricks to sync clock with Internet?


TimeService class not working too. The program hangs on the call Start() method… :frowning:

I use the following to get the time from a server. I can’t recall where I got the sample from but this works over a GPRS connection so should work over Ethernet too.

        // Gets the current DateTime from time server
        // Returns A DateTime containing the current time

        static DateTime GetNetworkTime()
            return GetNetworkTime("");

        // Gets the current DateTime from <paramref name="ntpServer"/>.
        // The hostname of the NTP server.
        // Returns a DateTime containing the current time

        static DateTime GetNetworkTime(string ntpServer)
            IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;

            if (address == null || address.Length == 0)
                throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");

            IPEndPoint ep = new IPEndPoint(address[0], 123);

            return GetNetworkTime(ep);

        // Gets the current DateTime from IPEndPoint.
        // Returns a DateTime containing the current time.

        static DateTime GetNetworkTime(IPEndPoint ep)
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


            byte[] ntpData = new byte[48]; // RFC 2030 
            ntpData[0] = 0x1B;
            for (int i = 1; i < 48; i++)
                ntpData[i] = 0;


            byte offsetTransmitTime = 40;
            ulong intpart = 0;
            ulong fractpart = 0;

            for (int i = 0; i <= 3; i++)
                intpart = 256 * intpart + ntpData[offsetTransmitTime + i];

            for (int i = 4; i <= 7; i++)
                fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];

            ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);

            TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);

            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime += timeSpan;

            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;

Sorry to all. My admin forgot to tell me that he had blocked the NTP protocol. Now, the protocol is open, and everything works fine.

@ Dave McLaughlin - Does TimeZone.CurrentTimeZone.GetUtcOffset() work in NETMF? And if yes how is it possible to set current time zone?

Good point. I have not used the code for some time and at least not since 4.1 and the ExtendedTimeZone.SetTimeZone is not longer in the framework.

I’ll need to look into this again.

There is another NTP option in Codeshare but not sure if this is up to date either.