C#的SNTP客户端取SNTP时间服务器代码


代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading ;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Net;

 

namespace AutoExitWindows
{
 // Leap indicator field values
 public enum _LeapIndicator
 {
  NoWarning,  // 0 - No warning
  LastMinute61, // 1 - Last minute has 61 seconds
  LastMinute59, // 2 - Last minute has 59 seconds
  Alarm   // 3 - Alarm condition (clock not synchronized)
 }

 //Mode field values
 public enum _Mode
 {
  SymmetricActive, // 1 - Symmetric active
  SymmetricPassive, // 2 - Symmetric pasive
  Client,    // 3 - Client
  Server,    // 4 - Server
  Broadcast,   // 5 - Broadcast
  Unknown    // 0, 6, 7 - Reserved
 }

 // Stratum field values
 public enum _Stratum
 {
  Unspecified,   // 0 - unspecified or unavailable
  PrimaryReference,  // 1 - primary reference (e.g. radio-clock)
  SecondaryReference,  // 2-15 - secondary reference (via NTP or SNTP)
  Reserved    // 16-255 - reserved
 }

 /// <summary>
 /// SNTPTimeClient 的摘要说明。
 ///
 /// Public class members:
 ///
 /// LeapIndicator - Warns of an impending leap second to be inserted/deleted in the last
 /// minute of the current day. (See the _LeapIndicator enum)
 ///
 /// VersionNumber - Version number of the protocol (3 or 4).
 ///
 /// Mode - Returns mode. (See the _Mode enum)
 ///
 /// Stratum - Stratum of the clock. (See the _Stratum enum)
 ///
 /// PollInterval - Maximum interval between successive messages.
 ///
 /// Precision - Precision of the clock.
 ///
 /// RootDelay - Round trip time to the primary reference source.
 ///
 /// RootDispersion - Nominal error relative to the primary reference source.
 ///
 /// ReferenceID - Reference identifier (either a 4 character string or an IP address).
 ///
 /// ReferenceTimestamp - The time at which the clock was last set or corrected.
 ///
 /// OriginateTimestamp - The time at which the request departed the client for the server.
 ///
 /// ReceiveTimestamp - The time at which the request arrived at the server.
 ///
 /// Transmit Timestamp - The time at which the reply departed the server for client.
 ///
 /// RoundTripDelay - The time between the departure of request and arrival of reply.
 ///
 /// LocalClockOffset - The offset of the local clock relative to the primary reference
 /// source.
 ///
 /// Initialize - Sets up data structure and prepares for connection.
 ///
 /// Connect - Connects to the time server and populates the data structure.
 ///
 /// IsResponseValid - Returns true if received data is valid and if comes from
 /// a NTP-compliant time server.
 ///
 /// ToString - Returns a string representation of the object.
 ///
 /// -----------------------------------------------------------------------------
 /// Structure of the standard NTP header (as described in RFC 2030)
 ///                       1                   2                   3
 ///   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 ///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 ///  |LI | VN  |Mode |    Stratum    |     Poll      |   Precision   |
 ///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 ///  |                          Root Delay                           |
 ///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 ///  |                       Root Dispersion                         |
 ///  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 ///  |                    

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值