How to Query an NTP Server using C#?

前端 未结 6 2028
一生所求
一生所求 2020-11-22 03:09

All I need is a way to query an NTP Server using C# to get the Date Time of the NTP Server returned as either a string or as a DateTime.

H

6条回答
  •  滥情空心
    2020-11-22 03:49

    http://www.codeproject.com/Articles/237501/Windows-Phone-NTP-Client is going to work well for Windows Phone .

    Adding the relevant code

    /// 
    /// Class for acquiring time via Ntp. Useful for applications in which correct world time must be used and the 
    /// clock on the device isn't "trusted."
    /// 
    public class NtpClient
    {
        /// 
        /// Contains the time returned from the Ntp request
        /// 
        public class TimeReceivedEventArgs : EventArgs
        {
            public DateTime CurrentTime { get; internal set; }
        }
    
        /// 
        /// Subscribe to this event to receive the time acquired by the NTP requests
        /// 
        public event EventHandler TimeReceived;
    
        protected void OnTimeReceived(DateTime time)
        {
            if (TimeReceived != null)
            {
                TimeReceived(this, new TimeReceivedEventArgs() { CurrentTime = time });
            }
        }
    
    
        /// 
        /// Not reallu used. I put this here so that I had a list of other NTP servers that could be used. I'll integrate this
        /// information later and will provide method to allow some one to choose an NTP server.
        /// 
        public string[] NtpServerList = new string[]
        {
            "pool.ntp.org ",
            "asia.pool.ntp.org",
            "europe.pool.ntp.org",
            "north-america.pool.ntp.org",
            "oceania.pool.ntp.org",
            "south-america.pool.ntp.org",
            "time-a.nist.gov"
        };
    
        string _serverName;
        private Socket _socket;
    
        /// 
        /// Constructor allowing an NTP server to be specified
        /// 
        /// the name of the NTP server to be used
        public NtpClient(string serverName)
        {
            _serverName = serverName;
        }
    
    
        /// 
        /// 
        /// 
        public NtpClient()
            : this("time-a.nist.gov")
        { }
    
        /// 
        /// Begins the network communication required to retrieve the time from the NTP server
        /// 
        public void RequestTime()
        {
            byte[] buffer = new byte[48];
            buffer[0] = 0x1B;
            for (var i = 1; i < buffer.Length; ++i)
                buffer[i] = 0;
            DnsEndPoint _endPoint = new DnsEndPoint(_serverName, 123);
    
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            SocketAsyncEventArgs sArgsConnect = new SocketAsyncEventArgs() { RemoteEndPoint = _endPoint };
            sArgsConnect.Completed += (o, e) =>
            {
                if (e.SocketError == SocketError.Success)
                {
                    SocketAsyncEventArgs sArgs = new SocketAsyncEventArgs() { RemoteEndPoint = _endPoint };
                    sArgs.Completed +=
                        new EventHandler(sArgs_Completed);
                    sArgs.SetBuffer(buffer, 0, buffer.Length);
                    sArgs.UserToken = buffer;
                    _socket.SendAsync(sArgs);
                }
            };
            _socket.ConnectAsync(sArgsConnect);
    
        }
    
        void sArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                byte[] buffer = (byte[])e.Buffer;
                SocketAsyncEventArgs sArgs = new SocketAsyncEventArgs();
                sArgs.RemoteEndPoint = e.RemoteEndPoint;
    
                sArgs.SetBuffer(buffer, 0, buffer.Length);
                sArgs.Completed += (o, a) =>
                {
                    if (a.SocketError == SocketError.Success)
                    {
                        byte[] timeData = a.Buffer;
    
                        ulong hTime = 0;
                        ulong lTime = 0;
    
                        for (var i = 40; i <= 43; ++i)
                            hTime = hTime << 8 | buffer[i];
                        for (var i = 44; i <= 47; ++i)
                            lTime = lTime << 8 | buffer[i];
                        ulong milliseconds = (hTime * 1000 + (lTime * 1000) / 0x100000000L);
    
                        TimeSpan timeSpan =
                            TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
                        var currentTime = new DateTime(1900, 1, 1) + timeSpan;
                        OnTimeReceived(currentTime);
    
                    }
                };
                _socket.ReceiveAsync(sArgs);
            }
        }
    }
    

    Usage :

    public partial class MainPage : PhoneApplicationPage
    {
        private NtpClient _ntpClient;
        public MainPage()
        {
            InitializeComponent();
            _ntpClient = new NtpClient();
            _ntpClient.TimeReceived += new EventHandler(_ntpClient_TimeReceived);
        }
    
        void _ntpClient_TimeReceived(object sender, NtpClient.TimeReceivedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
                                            {
                                                txtCurrentTime.Text = e.CurrentTime.ToLongTimeString();
                                                txtSystemTime.Text = DateTime.Now.ToUniversalTime().ToLongTimeString();
                                            });
        }
    
        private void UpdateTimeButton_Click(object sender, RoutedEventArgs e)
        {
            _ntpClient.RequestTime();
        }
    }
    

提交回复
热议问题