标题:无法获取ntp时间,如何解决?
取消只看楼主
yuma
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:银河系
等 级:贵宾
威 望:33
帖 子:1883
专家分:2904
注 册:2009-12-22
结帖率:89.13%
 问题点数:0 回复次数:0 
无法获取ntp时间,如何解决?
程序代码:
Private Sub Command1_Click()
    '连接NTP服务器
    Dim ntpServer As String
    ntpServer = "pool." '可以替换为其他NTP服务器地址
    Winsock1.RemoteHost = ntpServer
    Winsock1.RemotePort = 123
    Winsock1.Connect
    
    '发送NTP请求
    Dim request(47) As Byte
    request(0) = &H1B
    Winsock1.SendData request, 48, 0
    
    '等待NTP响应
    Do While Winsock1.State <> sckClosed
        DoEvents
    Loop
    
    '解析NTP响应
    Dim response(47) As Byte
    Winsock1.GetData response, vbArray + vbBinary
    Dim seconds As Long
    seconds = CLng(response(40)) * 2 ^ 24 + CLng(response(41)) * 2 ^ 16 + CLng(response(42)) * 2 ^ 8 + CLng(response(43))
    Dim ntpTime As Date
    ntpTime = DateAdd("s", seconds - 2208988800#, "1/1/1900")
    
    '显示NTP时间
    MsgBox "NTP时间:" & ntpTime
End Sub
搜索更多相关主题的帖子: 时间 request 获取 response Dim 
2023-03-21 13:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511623-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.106664 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved