可能重复:
如何从 C# 查询 NTP 服务器
我正在尝试从time.windows.com
NTP 服务器获取日期时间,我正在使用以下代码:
using System;
using System.IO;
using System.Net.Sockets;
namespace ntpdate2
{
class MainClass
{
public static void Main (string[] args)
{
var client = new TcpClient("time.windows.com", 123);
client.SendTimeout = 60;
using (var streamReader = new StreamReader(client.GetStream()))
{
var response = streamReader.ReadToEnd();
Console.WriteLine(response);
streamReader.Close();
}
}
}
}
但它给出了一个:
Unhandled Exception: System.Net.Sockets.SocketException: Connection timed out
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remote_end) [0x00000]
at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000]
at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000]
The application was terminated by a signal: SIGHUP
如何解决这个问题?提前致谢。