C#获取网络时间

int nSize;
        /// <summary>
        /// 获取网络时间
        /// </summary>
        /// <returns></returns>
        public System.DateTime GetServerDateTime()
        {
            string[,] TimeServer = new string[14, 2];
            int[] SearchOrder = new int[] { 5, 2, 4, 8, 9, 6, 11, 1, 10, 0, 3, 7, 12 };
            TimeServer[0, 0] = "time-a.nist.gov";
            TimeServer[0, 1] = "129.6.15.28";
            TimeServer[1, 0] = "time-b.nist.gov";
            TimeServer[1, 1] = "129.6.15.29";
            TimeServer[2, 0] = "time-a.timefreq.bldrdoc.gov";
            TimeServer[2, 1] = "132.163.4.101";
            TimeServer[3, 0] = "time-b.timefreq.bldrdoc.gov";
            TimeServer[3, 1] = "132.163.4.102";
            TimeServer[4, 0] = "time-c.timefreq.bldrdoc.gov";
            TimeServer[4, 1] = "132.163.4.103";
            TimeServer[5, 0] = "utcnist.colorado.edu";
            TimeServer[5, 1] = "128.138.140.44";
            TimeServer[6, 0] = "time.nist.gov";
            TimeServer[6, 1] = "192.43.244.18";
            TimeServer[7, 0] = "time-nw.nist.gov";
            TimeServer[7, 1] = "131.107.1.10";
            TimeServer[8, 0] = "nist1.symmetricom.com";
            TimeServer[8, 1] = "69.25.96.13";
            TimeServer[9, 0] = "nist1-dc.glassey.com";
            TimeServer[9, 1] = "216.200.93.8";
            TimeServer[10, 0] = "nist1-ny.glassey.com";
            TimeServer[10, 1] = "208.184.49.9";
            TimeServer[11, 0] = "nist1-sj.glassey.com";
            TimeServer[11, 1] = "207.126.98.204";
            TimeServer[12, 0] = "nist1.aol-ca.truetime.com";
            TimeServer[12, 1] = "207.200.81.113";
            TimeServer[13, 0] = "nist1.aol-va.truetime.com";
            TimeServer[13, 1] = "64.236.96.53";

            System.DateTime ret;
            ret = System.DateTime.MinValue;
            byte[] RecvBuf = new byte[1024];
            RecvBuf.Initialize();
            string ServerIP;
            for (int i = 0; i < 13; i++)
            {
                ServerIP = TimeServer[SearchOrder[i], 1];
                IPEndPoint ServerEp = new IPEndPoint(IPAddress.Parse(ServerIP), 37);
                Socket Time_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                {
                    try
                    {
                        Time_Socket.Connect(ServerEp);
                        nSize = Time_Socket.Receive(RecvBuf);
                        Time_Socket.Close();
                        break;
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            if (nSize == 4)//接收到一个位的整型   
            {
                try
                {
                    int recvInt = BitConverter.ToInt32(RecvBuf, 0);
                    //   这里转换网络字节序为主机字节序   
                    recvInt = System.Net.IPAddress.NetworkToHostOrder(recvInt);
                    //   转换为真正的秒数   
                    uint ServerSecs = (uint)(recvInt);
                    //   The   ServerSecs   is   the   number   of   seconds   since   00:00   (midnight)   1   January   1900   GMT  
                    ret = DateTime.Parse("1900-01-01 00:00:00 ");
                    ret = ret.AddSeconds(ServerSecs - 8);//因为根据网络的快慢 减8 是为了减小秒间的差距
                    ret = ret.AddHours(8);     //   转换为东区时间   
                }
                catch
                {
                    GetServerDateTime();
                }
            }
            webserviceDB.ServerInstance.DateNow = ret;
            return ret;
        }

 

VS08上可行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值