Как мне синхронизировать время с интернет-ресурсом на WM?

В приложении Windows я использую w32tm, чтобы заставить компьютер синхронизировать время с определенным временным ресурсом. Но сейчас я делаю приложение для КПК на WM5.0, w32tm больше не доступен и понятия не имею, с чего начать после небольшого поиска в google.


person Kelvin    schedule 21.01.2010    source источник


Ответы (1)


Вот хороший пример.

Для полноты, вот код из статьи в блоге:

public DateTime GetNTPTime()
{
    // 0x1B == 0b11011 == NTP version 3, client - see RFC 2030
    byte[] ntpPacket = new byte[] { 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    IPAddress[] addressList = Dns.GetHostEntry("pool.ntp.org").AddressList;

    if (addressList.Length == 0)
    {
        // error
        return DateTime.MinValue;
    }

    IPEndPoint ep = new IPEndPoint(addressList[0], 123);
    UdpClient client = new UdpClient();
    client.Connect(ep);
    client.Send(ntpPacket, ntpPacket.Length);
    byte[] data = client.Receive(ref ep);

    // receive date data is at offset 32
    // Data is 64 bits - first 32 is seconds - we'll toss the fraction of a second
    // it is not in an endian order, so we must rearrange
    byte[] endianSeconds = new byte[4];
    endianSeconds[0] = data[32 + 3];
    endianSeconds[1] = data[32 + 2];
    endianSeconds[2] = data[32 + 1];
    endianSeconds[3] = data[32 + 0];
    uint seconds = BitConverter.ToUInt32(endianSeconds, 0);

    return (new DateTime(1900, 1, 1)).AddSeconds(seconds);
}
person ctacke    schedule 21.01.2010