c# 擷取網路標準時間

來源:互聯網
上載者:User
public static DateTime DataStandardTime()        {//返回國際標準時間            //只使用的時間伺服器的IP地址,未使用網域名稱            string[,] 時間伺服器 = new string[14, 2];            int[] 搜尋順序 = new int[] { 3, 2, 4, 8, 9, 6, 11, 5, 10, 0, 1, 7, 12 };            時間伺服器[0, 0] = "time-a.nist.gov";            時間伺服器[0, 1] = "129.6.15.28";            時間伺服器[1, 0] = "time-b.nist.gov";            時間伺服器[1, 1] = "129.6.15.29";            時間伺服器[2, 0] = "time-a.timefreq.bldrdoc.gov";            時間伺服器[2, 1] = "132.163.4.101";            時間伺服器[3, 0] = "time-b.timefreq.bldrdoc.gov";            時間伺服器[3, 1] = "132.163.4.102";            時間伺服器[4, 0] = "time-c.timefreq.bldrdoc.gov";            時間伺服器[4, 1] = "132.163.4.103";            時間伺服器[5, 0] = "utcnist.colorado.edu";            時間伺服器[5, 1] = "128.138.140.44";            時間伺服器[6, 0] = "time.nist.gov";            時間伺服器[6, 1] = "192.43.244.18";            時間伺服器[7, 0] = "time-nw.nist.gov";            時間伺服器[7, 1] = "131.107.1.10";            時間伺服器[8, 0] = "nist1.symmetricom.com";            時間伺服器[8, 1] = "69.25.96.13";            時間伺服器[9, 0] = "nist1-dc.glassey.com";            時間伺服器[9, 1] = "216.200.93.8";            時間伺服器[10, 0] = "nist1-ny.glassey.com";            時間伺服器[10, 1] = "208.184.49.9";            時間伺服器[11, 0] = "nist1-sj.glassey.com";            時間伺服器[11, 1] = "207.126.98.204";            時間伺服器[12, 0] = "nist1.aol-ca.truetime.com";            時間伺服器[12, 1] = "207.200.81.113";            時間伺服器[13, 0] = "nist1.aol-va.truetime.com";            時間伺服器[13, 1] = "64.236.96.53";            int portNum = 13;            string hostName;            byte[] bytes = new byte[1024];            int bytesRead = 0;            System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();            for (int i = 0; i < 13; i++)            {                hostName = 時間伺服器[搜尋順序[i], 1];                try                {                    client.Connect(hostName, portNum);                    System.Net.Sockets.NetworkStream ns = client.GetStream();                    bytesRead = ns.Read(bytes, 0, bytes.Length);                    client.Close();                    break;                }                catch (System.Exception)                {                }            }            char[] sp = new char[1];            sp[0] = ' ';            System.DateTime dt = new DateTime();            string str1;            str1 = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRead);            string[] s;            s = str1.Split(sp);            dt = System.DateTime.Parse(s[1] + " " + s[2]);//得到標準時間            dt = dt.AddHours(8);//得到北京時間*/            return dt;        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.