public static int Main() { SNTPClient sntpClient = new SNTPClient(); sntpClient.UpdateLocalDateTime = true; sntpClient.Timeout = 3000; sntpClient.QueryServerCompleted += sntpClient_QueryServerCompleted; _canWork = sntpClient.QueryServerAsync(); if (_canWork) _wh.WaitOne(); else return -1; return (int)_returnValue; }
/// <summary> /// Gets the real local date and time using the default server and the specified timeout. /// If there is an error or exception, DateTime.MinValue is returned. /// </summary> /// <param name="remoteSNTPServer">The server to use.</param> /// <param name="timeout">The timeout in milliseconds used for sending and receiving.</param> /// <returns>The real local date and time.</returns> public static DateTime GetNow(RemoteSNTPServer remoteSNTPServer, int timeout) { SNTPClient sntpClient = new SNTPClient(); sntpClient.UpdateLocalDateTime = false; sntpClient.RemoteSNTPServer = remoteSNTPServer; sntpClient.Timeout = timeout; QueryServerCompletedEventArgs args = sntpClient.QueryServer(); if (args.Succeeded) { return(DateTime.Now.AddSeconds(args.Data.LocalClockOffset)); } else { return(DateTime.MinValue); } }
/// <summary> /// Gets the real local date and time using the default server and the specified timeout. /// If there is an error or exception, DateTime.MinValue is returned. /// </summary> /// <param name="remoteSNTPServer">The server to use.</param> /// <param name="timeout">The timeout in milliseconds used for sending and receiving.</param> /// <returns>The real local date and time.</returns> public static DateTime GetNow(RemoteSNTPServer remoteSNTPServer, int timeout) { SNTPClient sntpClient = new SNTPClient(); sntpClient.UpdateLocalDateTime = false; sntpClient.RemoteSNTPServer = remoteSNTPServer; sntpClient.Timeout = timeout; QueryServerCompletedEventArgs args = sntpClient.QueryServer(); if (args.Succeeded) return DateTime.Now.AddSeconds(args.Data.LocalClockOffset); else return DateTime.MinValue; }