VC自动与Internet时间服务器同步更新

      在VCKBASE、CSDN里挖了许久的坟,才找到一些有点用的资料,最后自己整理出这样的个函数,方面VC实现时间同步,多的不说,自己看源码,根据自己的需要可以适当修改源码:

[cpp] view plain copy
 
 print?
  1. #include <WinSock.h>  
  2. #pragma comment (lib,"Ws2_32")  
  3.   
  4. struct   NTP_Packet  
  5. {  
  6.     int      Control_Word;     
  7.     int      root_delay;     
  8.     int      root_dispersion;     
  9.     int      reference_identifier;     
  10.     __int64 reference_timestamp;     
  11.     __int64 originate_timestamp;     
  12.     __int64 receive_timestamp;     
  13.     int      transmit_timestamp_seconds;     
  14.     int      transmit_timestamp_fractions;     
  15. };  
[cpp] view plain copy
 
 print?
  1. /************************************************************************/  
  2. /* 函数说明:自动与时间服务器同步更新 
  3. /* 参数说明:无 
  4. /* 返 回 值:成功返回TRUE,失败返回FALSE 
  5. /************************************************************************/  
  6. BOOL UpdateSysTime()  
  7. {  
  8.     WORD    wVersionRequested;  
  9.     WSADATA wsaData;  
  10.       
  11.     // 初始化版本  
  12.     wVersionRequested = MAKEWORD( 1, 1 );  
  13.     if (0!=WSAStartup(wVersionRequested, &wsaData))   
  14.     {  
  15.         WSACleanup();  
  16.         return FALSE;  
  17.     }  
  18.     if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)   
  19.     {  
  20.         WSACleanup( );  
  21.         return FALSE;   
  22.     }  
  23.   
  24.     // 这个IP是中国大陆时间同步服务器地址,可自行修改  
  25.     SOCKET soc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);  
  26.     struct sockaddr_in addrSrv;  
  27.     addrSrv.sin_addr.S_un.S_addr=inet_addr("210.72.145.44");  
  28.     addrSrv.sin_family=AF_INET;  
  29.     addrSrv.sin_port=htons(123);  
  30.       
  31.     NTP_Packet NTP_Send,NTP_Recv;   
  32.     NTP_Send.Control_Word   =   htonl(0x0B000000);     
  33.     NTP_Send.root_delay     =   0;     
  34.     NTP_Send.root_dispersion   =   0;     
  35.     NTP_Send.reference_identifier   =   0;     
  36.     NTP_Send.reference_timestamp    =   0;     
  37.     NTP_Send.originate_timestamp    =   0;     
  38.     NTP_Send.receive_timestamp      =   0;     
  39.     NTP_Send.transmit_timestamp_seconds     =   0;     
  40.     NTP_Send.transmit_timestamp_fractions   =   0;   
  41.       
  42.     if(SOCKET_ERROR==sendto(soc,(const char*)&NTP_Send,sizeof(NTP_Send),  
  43.         0,(struct sockaddr*)&addrSrv,sizeof(addrSrv)))  
  44.     {  
  45.         closesocket(soc);  
  46.         return FALSE;  
  47.     }  
  48.     int sockaddr_Size =sizeof(addrSrv);  
  49.     if(SOCKET_ERROR==recvfrom(soc,(char*)&NTP_Recv,sizeof(NTP_Recv),  
  50.         0,(struct sockaddr*)&addrSrv,&sockaddr_Size))  
  51.     {  
  52.         closesocket(soc);  
  53.         return FALSE;  
  54.     }  
  55.     closesocket(soc);  
  56.     WSACleanup();  
  57.       
  58.     SYSTEMTIME  newtime;  
  59.     float       Splitseconds;  
  60.     struct      tm  *lpLocalTime;  
  61.     time_t      ntp_time;  
  62.   
  63.     // 获取时间服务器的时间  
  64.     ntp_time    = ntohl(NTP_Recv.transmit_timestamp_seconds)-2208988800;  
  65.     lpLocalTime = localtime(&ntp_time);  
  66.     if(lpLocalTime == NULL)  
  67.     {  
  68.         return FALSE;  
  69.     }  
  70.       
  71.     // 获取新的时间  
  72.     newtime.wYear      =lpLocalTime->tm_year+1900;  
  73.     newtime.wMonth     =lpLocalTime->tm_mon+1;  
  74.     newtime.wDayOfWeek =lpLocalTime->tm_wday;  
  75.     newtime.wDay       =lpLocalTime->tm_mday;  
  76.     newtime.wHour      =lpLocalTime->tm_hour;  
  77.     newtime.wMinute    =lpLocalTime->tm_min;  
  78.     newtime.wSecond    =lpLocalTime->tm_sec;  
  79.       
  80.     // 设置时间精度  
  81.     Splitseconds=(float)ntohl(NTP_Recv.transmit_timestamp_fractions);  
  82.     Splitseconds=(float)0.000000000200 * Splitseconds;  
  83.     Splitseconds=(float)1000.0 * Splitseconds;  
  84.     newtime.wMilliseconds   =   (unsigned   short)Splitseconds;  
  85.   
  86.     // 修改本机系统时间  
  87.     SetLocalTime(&newtime);  
  88.     return TRUE;  
  89. }  
 
 

http://blog.csdn.net/wangningyu/article/details/4522648

原文地址:https://www.cnblogs.com/findumars/p/5928698.html