1. //方法2、获取ntp时间
    2. public static DateTime DataStandardTime()//使用时,将static 关键字删除,在其它位置方可使用?2010-11-24
    3. {//返回国际标准时间
    4. //只使用的TimerServer的IP地址,未使用域名
    5. string[,] TimerServer = new string[14, 2];
    6. int[] ServerTab = new int[] { 3, 2, 4, 8, 9, 6, 11, 5, 10, 0, 1, 7, 12 };
    7. TimerServer[0, 0] = "time-a.nist.gov";
    8. TimerServer[0, 1] = "129.6.15.28";
    9. TimerServer[1, 0] = "time-b.nist.gov";
    10. TimerServer[1, 1] = "129.6.15.29";
    11. TimerServer[2, 0] = "time-a.timefreq.bldrdoc.gov";
    12. TimerServer[2, 1] = "132.163.4.101";
    13. TimerServer[3, 0] = "time-b.timefreq.bldrdoc.gov";
    14. TimerServer[3, 1] = "132.163.4.102";
    15. TimerServer[4, 0] = "time-c.timefreq.bldrdoc.gov";
    16. TimerServer[4, 1] = "132.163.4.103";
    17. TimerServer[5, 0] = "utcnist.colorado.edu";
    18. TimerServer[5, 1] = "128.138.140.44";
    19. TimerServer[6, 0] = "time.nist.gov";
    20. TimerServer[6, 1] = "192.43.244.18";
    21. TimerServer[7, 0] = "time-nw.nist.gov";
    22. TimerServer[7, 1] = "131.107.1.10";
    23. TimerServer[8, 0] = "nist1.symmetricom.com";
    24. TimerServer[8, 1] = "69.25.96.13";
    25. TimerServer[9, 0] = "nist1-dc.glassey.com";
    26. TimerServer[9, 1] = "216.200.93.8";
    27. TimerServer[10, 0] = "nist1-ny.glassey.com";
    28. TimerServer[10, 1] = "208.184.49.9";
    29. TimerServer[11, 0] = "nist1-sj.glassey.com";
    30. TimerServer[11, 1] = "207.126.98.204";
    31. TimerServer[12, 0] = "nist1.aol-ca.truetime.com";
    32. TimerServer[12, 1] = "207.200.81.113";
    33. TimerServer[13, 0] = "nist1.aol-va.truetime.com";
    34. TimerServer[13, 1] = "64.236.96.53";
    35. int portNum = 13;
    36. string hostName;
    37. byte[] bytes = new byte[1024];
    38. int bytesRead = 0;
    39. System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
    40. for (int i = 0; i < 13; i++)
    41. {
    42. hostName = TimerServer[ServerTab[i], 0];
    43. // Debug.WriteLine("hostName:" + hostName);
    44. try
    45. {
    46. client.Connect(hostName, portNum);
    47. System.Net.Sockets.NetworkStream ns = client.GetStream();
    48. bytesRead = ns.Read(bytes, 0, bytes.Length);
    49. client.Close();
    50. break;
    51. }
    52. catch (Exception)
    53. {
    54. Debug.WriteLine("错误!");
    55. }
    56. }
    57. char[] sp = new char[1];
    58. sp[0] = ' ';
    59. DateTime dt = new DateTime();
    60. string str1 = Encoding.ASCII.GetString(bytes, 0, bytesRead);
    61. if (!(str1 == string.Empty))
    62. {
    63. //Debug.WriteLine("ntp time:" + str1);
    64. string[] s = str1.Split(sp);
    65. try
    66. {
    67. dt = DateTime.Parse(s[1] + " " + s[2]);
    68. }
    69. catch (Exception)
    70. {
    71. MessageBox.Show(str1);
    72. return DateTime.Parse("2099-1-1");
    73. }
    74. //得到标准时间
    75. //Debug.WriteLine("get:" + dt.ToShortTimeString());
    76. dt = dt.AddHours(8);//得到北京时间*/
    77. return dt;
    78. }
    79. else
    80. {
    81. MessageBox.Show("请检查网络");
    82. return DateTime.Parse("2099-1-1");
    83. }
    84. }