gpt4 book ai didi

vb.net - 连接到 ntp 时间服务器有困难

转载 作者:行者123 更新时间:2023-12-01 15:29:08 25 4
gpt4 key购买 nike

我正在尝试使用以下代码连接到时间服务器并获取时间,但没有成功:

Dim ntpServer As String = "time.windows.com"
Dim ntpData(47) As Byte
Dim addresses = Dns.GetHostEntry(ntpServer).AddressList
Dim EndP As IPEndPoint = New IPEndPoint(addresses(0), 123)

Dim soc As Socket = New Socket(AddressFamily.InterNetwork, _
SocketType.Dgram, ProtocolType.Udp)

soc.Connect(EndP)
soc.Send(ntpData)
soc.Receive(ntpData)

soc.Close()

通过程序跟踪我无法通过以下代码行 soc.Receive(ntpData)。我究竟做错了什么?

谢谢

最佳答案

您需要向服务器提供一些基本信息:

ntpData(0) = 27

ntpData(0) 包含一个名为 firstByteBits 的部分。

此部分需要在发送数据查询回复前设置。

第一个字节是

 0 1 2 3 4 5 6 7 
+-+-+-+-+-+-+-+-+
|LI | VN |Mode |
  • LI = 飞跃指示器(发送数据中为 0)
  • VN = 版本号(3,位 3 和 4 设置)
  • Mode = 模式(客户端模式 = 3,位 6 和 7 设置)

00011011 = 27 = 0x1B

可能还有更好的 NTP 服务器。 time.windows.com:123 服务器池已知速度慢,有时一会没 react ,准确率低。更好:pool.ntp.org:123(但请阅读 poo.ntp.org 上关于常规使用的内容)。

例如RFC 5905了解更多详情。

关于vb.net - 连接到 ntp 时间服务器有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683452/

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