c#2005如何获取网络时间?

解决方案 »

  1.   

    也没太明白..string time=System.DateTime.Now.ToLongTimeString();这样?
      

  2.   

    楼主指的应该是Internet时间.
      

  3.   

    参考下:从Internet时间服务器获取标准格林尼治时间
    http://www.vipcn.com/InfoView/Article_81027.html
      

  4.   

    using System.Threading; //需要使用多线程
    using System.Net; //需要使用网络
    using System.Net.Sockets; //需要使用网络。
    using System.Runtime.InteropServices; 
    class Win32API 

     [DllImport("kernel32.dll")] 
     public static extern int SetLocalTime (ref SystemTime lpSystemTime);
    }public struct SystemTime
    {
     public short wYear;
     public short wMonth;
     public short wDayOfWeek;
     public short wDay;
     public short wHour;
     public short wMinute;
     public short wSecond;
     public short wMilliseconds;
    }
    public class frmMain : System.Windows.Forms.Form
    {
    ////省略自动生成的代码
    ////…………… Thread thrTimeSync;
     private void TimeSyncProc()
     {
      while(true)
      {
       TcpClient c = new TcpClient();
       c.Connect("www.time.ac.cn", 37);   //连接到国内授时服务器   NetworkStream s;
       s = c.GetStream();          //读取数据流
       c.Close();   byte []buf = new byte[4];
       s.Read(buf,0,4);                   //把数据存到数组中   uint lTime;   //把服务器返回数据转换成1900/1/1 UTC 到现在所经过的秒数
       lTime = ((uint)buf[0] << 24) + ((uint)buf[1] << 16) +                  ((uint)buf[2] << 8) + (uint)buf[3];   //得到真实的本地时间
       System.DateTime datetime = new DateTime(1900,1,1,0,0,0,0);
       datetime = datetime.AddSeconds(lTime).ToLocalTime();   //这里可以显示出时间。有兴趣的朋友可以取消注释看一下效果。
       //MessageBox.Show(datetime.ToLongDateString() + datetime.ToLongTimeString());   //修改系统时间
       SystemTime sysDateTime = new SystemTime();   sysDateTime.wYear = (short)datetime.Year;
       sysDateTime.wDay = (short)datetime.Day;
       sysDateTime.wMonth = (short)datetime.Month;   sysDateTime.wHour = (short)datetime.Hour;
       sysDateTime.wMinute = (short)datetime.Minute;
       sysDateTime.wSecond = (short)datetime.Second;   Win32API.SetLocalTime(ref sysDateTime);   System.Threading.Thread.Sleep(1 * 1000);
      }
     } private void frmMain_Load(object sender, System.EventArgs e)
     {
      thrTimeSync = new Thread(new ThreadStart(TimeSyncProc));
      thrTimeSync.Start();
     } private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
     {
      thrTimeSync.Abort();
     }}我也是找的人家的作者是王晓光
      

  5.   

    首先介绍一下网络对表的原理。网络上存在很多叫做“授时服务器”的主机,它们负责提供精确的时间,一般是由原子钟产生的。因此,我们只要做一个小程序来与授时服务器通信并按照其返回的准确时间来调整本机时间就可以了。   其中RFC组织为网络时间协议规定了几个不同版本:   DayTime协议:这是以ASCII字符传递时间信息的一种协议。参见RFC0867。 
      Time协议:这是以32位整数传递时间信息的一种协议。参见RFC0868。 
      Simple Network Time协议:这是一种更加精确的传递时间信息的协议。参见RFC1769。   你可以在http://rfc.linuxforum.net/找到相关RFC协议。 
      

  6.   

    public System.DateTime GetServerDateTime()         {             System.DateTime ret;             int nSize;               ret = System.DateTime.MinValue;               byte[] RecvBuf = new byte[1024];             //byte[] SendBuf = new byte[0];             RecvBuf.Initialize();               IPEndPoint ServerEp = new IPEndPoint(IPAddress.Parse(FServerAddress),FPort);               using (System.Net.Sockets.Socket Time_Socket = new Socket(AddressFamily.InterNetwork,                     SocketType.Stream,                     ProtocolType.IP))             {                 Time_Socket.Connect(ServerEp);                 //Time_Socket.Send(SendBuf);                 nSize = Time_Socket.Receive(RecvBuf);                   Time_Socket.Close();             }               if (nSize ==4)//接收到一个位的整型             {                 try                 {                     // 这里将byte数组转换为int类型                     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);                     ret = ret.AddHours(8);  // 转换为东区时间                 }                 catch (Exception e)                 {                     Console.WriteLine(e.Message);                 }                             }               return ret;         }