gpt4 book ai didi

c# - 我怎样才能将时间与 WM 上的互联网资源同步?

转载 作者:行者123 更新时间:2023-11-30 15:49:04 24 4
gpt4 key购买 nike

在 Windows 应用程序中,我使用 w32tm 强制计算机将时间与特定时间资源同步。但现在我正在 WM5.0 上做一个 PDA 应用程序,w32tm 不再可用,在谷歌上搜索了一下后不知道如何开始。

最佳答案

Here is a good example .

为了完整性,这里是博客文章中的代码:

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);
}

关于c# - 我怎样才能将时间与 WM 上的互联网资源同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2113538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com