예제 #1
0
        /// <summary>
        /// Gets the NTP offset from a specified time server.
        /// </summary>
        /// <param name="timeServer">The time server.</param>
        /// <returns></returns>
        public static Ntp GetNtpOffset(string timeserver)
        {
            if (string.IsNullOrEmpty(timeserver))
            {
                throw new ArgumentNullException("timeserver");
            }

            Logger.Debug("Retrieving NTP information from {0}", timeserver);

            int retryCount = 5;
            Ntp currentNtp = new Ntp();

            while (retryCount > 0)
            {
                try
                {
                    NtpClient ntpClient = new NtpClient(timeserver);
                    ntpClient.Connect(false);
                    currentNtp.offset      = ntpClient.LocalClockOffset;
                    currentNtp.currentTime = DateTime.Now;
                    break;
                }
                catch (SocketException se)
                {
                    Logger.Error("Error while retrieving ntp information: {0}", se.ToString());
                    currentNtp.message = se.Message;
                    retryCount--;
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error while retrieving ntp information: {0}", ex.ToString());
                    currentNtp.message = ex.Message;
                    break;
                }
            }
            return(currentNtp);
        }
예제 #2
0
        /// <summary>
        /// Gets the NTP offset from a specified time server.
        /// </summary>
        /// <param name="timeServer">The time server.</param>
        /// <returns></returns>
        public static Ntp GetNtpOffset(string timeserver)
        {
            if (string.IsNullOrEmpty(timeserver))
            {
                throw new ArgumentNullException("timeserver");
            }

            Logger.Debug("Retrieving NTP information from {0}", timeserver);

            int retryCount = 5;
            Ntp currentNtp = new Ntp();
            while (retryCount > 0)
            {
                try
                {
                    NtpClient ntpClient = new NtpClient(timeserver);
                    ntpClient.Connect(false);
                    currentNtp.offset = ntpClient.LocalClockOffset;
                    currentNtp.currentTime = DateTime.Now;
                    break;
                }
                catch (SocketException se)
                {
                    Logger.Error("Error while retrieving ntp information: {0}", se.ToString());
                    currentNtp.message = se.Message;
                    retryCount--;
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error while retrieving ntp information: {0}", ex.ToString());
                    currentNtp.message = ex.Message;
                    break;
                }
            }
            return currentNtp;
        }