0

重複の可能性:
C#からNTPサーバーをクエリする方法

NTPサーバーから日時を取得しようとtime.windows.comしています。次のコードを使用しています。

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

これを修正する方法は?前もって感謝します。

4

1 に答える 1

4

以下は LINQPad で実行されるコードで、https://mschwarztoolkit.svn.codeplex.com/svn/NTP/NtpClient.csの元のバージョンからわずかに変更されています。

void Main()
{
    var x = NtpClient.GetNetworkTime();
   x.Dump();
}

/// <summary>
/// Static class to receive the time from a NTP server.
/// </summary>
public class NtpClient
{
    /// <summary>
    /// Gets the current DateTime from time-a.nist.gov.
    /// </summary>
    /// <returns>A DateTime containing the current time.</returns>
    public static DateTime GetNetworkTime()
    {
        return GetNetworkTime("time.windows.com"); // time-a.nist.gov
    }

    /// <summary>
    /// Gets the current DateTime from <paramref name="ntpServer"/>.
    /// </summary>
    /// <param name="ntpServer">The hostname of the NTP server.</param>
    /// <returns>A DateTime containing the current time.</returns>
    public static DateTime GetNetworkTime(string ntpServer)
    {
        IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;

        if(address == null || address.Length == 0)
            throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");

        IPEndPoint ep = new IPEndPoint(address[0], 123);

        return GetNetworkTime(ep);
    }

    /// <summary>
    /// Gets the current DateTime form <paramref name="ep"/> IPEndPoint.
    /// </summary>
    /// <param name="ep">The IPEndPoint to connect to.</param>
    /// <returns>A DateTime containing the current time.</returns>
    public static DateTime GetNetworkTime(IPEndPoint ep)
    {
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        s.Connect(ep);

        byte[] ntpData = new byte[48]; // RFC 2030 
        ntpData[0] = 0x1B;
        for (int i = 1; i < 48; i++)
            ntpData[i] = 0;

        s.Send(ntpData);
        s.Receive(ntpData);

        byte offsetTransmitTime = 40;
        ulong intpart = 0;
        ulong fractpart = 0;

        for (int i = 0; i <= 3; i++)
            intpart = 256 * intpart + ntpData[offsetTransmitTime + i];

        for (int i = 4; i <= 7; i++)
            fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];

        ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
        s.Close();

        TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);

        DateTime dateTime = new DateTime(1900, 1, 1);
        dateTime += timeSpan;

        TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
        DateTime networkDateTime = (dateTime + offsetAmount);

        return networkDateTime;
    }
}
于 2012-05-26T21:37:58.350 に答える