#2
阳光上的桥2023-03-22 11:06
|
程序代码:
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
'连接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