优化
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -68,7 +68,7 @@
|
|||||||
"RelativeDocumentMoniker": "Form1.cs",
|
"RelativeDocumentMoniker": "Form1.cs",
|
||||||
"ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs",
|
"ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs",
|
||||||
"RelativeToolTip": "Form1.cs",
|
"RelativeToolTip": "Form1.cs",
|
||||||
"ViewState": "AgIAAIUAAAAAAAAAAAAIwI8AAAAAAAAAAAAAAA==",
|
"ViewState": "AgIAAIUAAAAAAAAAAAAIwI0AAAARAAAAAAAAAA==",
|
||||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||||
"WhenOpened": "2025-06-27T12:15:25.553Z",
|
"WhenOpened": "2025-06-27T12:15:25.553Z",
|
||||||
"EditorCaption": ""
|
"EditorCaption": ""
|
||||||
|
@@ -68,7 +68,7 @@
|
|||||||
"RelativeDocumentMoniker": "Form1.cs",
|
"RelativeDocumentMoniker": "Form1.cs",
|
||||||
"ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs",
|
"ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs",
|
||||||
"RelativeToolTip": "Form1.cs",
|
"RelativeToolTip": "Form1.cs",
|
||||||
"ViewState": "AgIAAIUAAAAAAAAAAAAIwI8AAAAAAAAAAAAAAA==",
|
"ViewState": "AgIAAIUAAAAAAAAAAAAIwI0AAAARAAAAAAAAAA==",
|
||||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||||
"WhenOpened": "2025-06-27T12:15:25.553Z",
|
"WhenOpened": "2025-06-27T12:15:25.553Z",
|
||||||
"EditorCaption": ""
|
"EditorCaption": ""
|
||||||
|
17
Form1.cs
17
Form1.cs
@@ -135,15 +135,23 @@ namespace Ntpsync
|
|||||||
byte[] ntpData = new byte[48];
|
byte[] ntpData = new byte[48];
|
||||||
ntpData[0] = 0x1B; // LI = 0 (no warning), VN = 3, Mode = 3 (Client)
|
ntpData[0] = 0x1B; // LI = 0 (no warning), VN = 3, Mode = 3 (Client)
|
||||||
|
|
||||||
|
// 解析 IPv4 地址
|
||||||
|
var ipList = await Task.Run(() => Dns.GetHostAddresses(host));
|
||||||
|
var ipv4 = ipList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
|
||||||
|
if (ipv4 == null)
|
||||||
|
{
|
||||||
|
return (DateTime.Now, false, $"{host} 无可用 IPv4 地址");
|
||||||
|
}
|
||||||
|
|
||||||
|
IPEndPoint endPoint = new IPEndPoint(ipv4, 123);
|
||||||
|
|
||||||
using (var udp = new UdpClient(AddressFamily.InterNetwork))
|
using (var udp = new UdpClient(AddressFamily.InterNetwork))
|
||||||
{
|
{
|
||||||
udp.Client.ReceiveTimeout = 3000; // 3 秒超时
|
udp.Client.ReceiveTimeout = 3000; // 3 秒超时
|
||||||
udp.Client.SendTimeout = 3000;
|
udp.Client.SendTimeout = 3000;
|
||||||
|
|
||||||
udp.Connect(host, 123);
|
// 发送请求到指定 IPv4 端点
|
||||||
|
await udp.SendAsync(ntpData, ntpData.Length, endPoint);
|
||||||
// 发送请求
|
|
||||||
await udp.SendAsync(ntpData, ntpData.Length);
|
|
||||||
|
|
||||||
// 接收响应(带超时或取消)
|
// 接收响应(带超时或取消)
|
||||||
var receiveTask = udp.ReceiveAsync();
|
var receiveTask = udp.ReceiveAsync();
|
||||||
@@ -161,6 +169,7 @@ namespace Ntpsync
|
|||||||
return (DateTime.Now, false, $"从 {host} 获取时间失败: 超时");
|
return (DateTime.Now, false, $"从 {host} 获取时间失败: 超时");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Anycast 场景下可能由不同 IP 返回,只要端口正确即可接受
|
||||||
ntpData = receiveTask.Result.Buffer;
|
ntpData = receiveTask.Result.Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user