diff --git a/.vs/Ntpsync/FileContentIndex/236fe584-ec21-46fc-9d6d-bb5553e5a971.vsidx b/.vs/Ntpsync/FileContentIndex/236fe584-ec21-46fc-9d6d-bb5553e5a971.vsidx deleted file mode 100644 index c48f23a..0000000 Binary files a/.vs/Ntpsync/FileContentIndex/236fe584-ec21-46fc-9d6d-bb5553e5a971.vsidx and /dev/null differ diff --git a/.vs/Ntpsync/FileContentIndex/666311a6-e74e-4890-9877-958af5749b18.vsidx b/.vs/Ntpsync/FileContentIndex/666311a6-e74e-4890-9877-958af5749b18.vsidx new file mode 100644 index 0000000..eb6ce5a Binary files /dev/null and b/.vs/Ntpsync/FileContentIndex/666311a6-e74e-4890-9877-958af5749b18.vsidx differ diff --git a/.vs/Ntpsync/v17/.suo b/.vs/Ntpsync/v17/.suo index ee4f109..e4a6d92 100644 Binary files a/.vs/Ntpsync/v17/.suo and b/.vs/Ntpsync/v17/.suo differ diff --git a/.vs/Ntpsync/v17/DocumentLayout.backup.json b/.vs/Ntpsync/v17/DocumentLayout.backup.json index b7ba069..7cca3aa 100644 --- a/.vs/Ntpsync/v17/DocumentLayout.backup.json +++ b/.vs/Ntpsync/v17/DocumentLayout.backup.json @@ -68,7 +68,7 @@ "RelativeDocumentMoniker": "Form1.cs", "ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs", "RelativeToolTip": "Form1.cs", - "ViewState": "AgIAAIUAAAAAAAAAAAAIwI8AAAAAAAAAAAAAAA==", + "ViewState": "AgIAAIUAAAAAAAAAAAAIwI0AAAARAAAAAAAAAA==", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "WhenOpened": "2025-06-27T12:15:25.553Z", "EditorCaption": "" diff --git a/.vs/Ntpsync/v17/DocumentLayout.json b/.vs/Ntpsync/v17/DocumentLayout.json index b7ba069..7cca3aa 100644 --- a/.vs/Ntpsync/v17/DocumentLayout.json +++ b/.vs/Ntpsync/v17/DocumentLayout.json @@ -68,7 +68,7 @@ "RelativeDocumentMoniker": "Form1.cs", "ToolTip": "D:\\File\\Library\\Tools\\Ntpsync\\Form1.cs", "RelativeToolTip": "Form1.cs", - "ViewState": "AgIAAIUAAAAAAAAAAAAIwI8AAAAAAAAAAAAAAA==", + "ViewState": "AgIAAIUAAAAAAAAAAAAIwI0AAAARAAAAAAAAAA==", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "WhenOpened": "2025-06-27T12:15:25.553Z", "EditorCaption": "" diff --git a/Form1.cs b/Form1.cs index f304cf1..89bd14b 100644 --- a/Form1.cs +++ b/Form1.cs @@ -135,15 +135,23 @@ namespace Ntpsync byte[] ntpData = new byte[48]; 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)) { udp.Client.ReceiveTimeout = 3000; // 3 秒超时 - udp.Client.SendTimeout = 3000; + udp.Client.SendTimeout = 3000; - udp.Connect(host, 123); - - // 发送请求 - await udp.SendAsync(ntpData, ntpData.Length); + // 发送请求到指定 IPv4 端点 + await udp.SendAsync(ntpData, ntpData.Length, endPoint); // 接收响应(带超时或取消) var receiveTask = udp.ReceiveAsync(); @@ -161,6 +169,7 @@ namespace Ntpsync return (DateTime.Now, false, $"从 {host} 获取时间失败: 超时"); } + // Anycast 场景下可能由不同 IP 返回,只要端口正确即可接受 ntpData = receiveTask.Result.Buffer; } diff --git a/bin/Debug/Ntpsync.exe b/bin/Debug/Ntpsync.exe index c245cb2..63940cd 100644 Binary files a/bin/Debug/Ntpsync.exe and b/bin/Debug/Ntpsync.exe differ diff --git a/bin/Debug/Ntpsync.pdb b/bin/Debug/Ntpsync.pdb index 8918a36..bcf3032 100644 Binary files a/bin/Debug/Ntpsync.pdb and b/bin/Debug/Ntpsync.pdb differ diff --git a/obj/Debug/Ntpsync.exe b/obj/Debug/Ntpsync.exe index c245cb2..63940cd 100644 Binary files a/obj/Debug/Ntpsync.exe and b/obj/Debug/Ntpsync.exe differ diff --git a/obj/Debug/Ntpsync.pdb b/obj/Debug/Ntpsync.pdb index 8918a36..bcf3032 100644 Binary files a/obj/Debug/Ntpsync.pdb and b/obj/Debug/Ntpsync.pdb differ