在VCKBASE、CSDN裡挖了許久的墳,才找到一些有點用的資料,最後自己整理出這樣的個函數,方面VC實現時間同步,多的不說,自己看源碼,根據自己的需要可以適當修改源碼:
#include <WinSock.h><br />#pragma comment (lib,"Ws2_32")</p><p>struct NTP_Packet<br />{<br />int Control_Word;<br />int root_delay;<br />int root_dispersion;<br />int reference_identifier;<br />__int64reference_timestamp;<br />__int64 originate_timestamp;<br />__int64 receive_timestamp;<br />int transmit_timestamp_seconds;<br />int transmit_timestamp_fractions;<br />};
/************************************************************************/<br />/* 函數說明:自動與時間伺服器同步更新<br />/* 參數說明:無<br />/* 返 回 值:成功返回TRUE,失敗返回FALSE<br />/************************************************************************/<br />BOOL UpdateSysTime()<br />{<br />WORDwVersionRequested;<br />WSADATA wsaData;</p><p>// 初始化版本<br />wVersionRequested = MAKEWORD( 1, 1 );<br />if (0!=WSAStartup(wVersionRequested, &wsaData))<br />{<br />WSACleanup();<br />return FALSE;<br />}<br />if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)<br />{<br />WSACleanup( );<br />return FALSE;<br />}</p><p>// 這個IP是中國大陸時間同步伺服器位址,可自行修改<br />SOCKET soc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);<br />struct sockaddr_in addrSrv;<br />addrSrv.sin_addr.S_un.S_addr=inet_addr("210.72.145.44");<br />addrSrv.sin_family=AF_INET;<br />addrSrv.sin_port=htons(123);</p><p>NTP_Packet NTP_Send,NTP_Recv;<br />NTP_Send.Control_Word = htonl(0x0B000000);<br />NTP_Send.root_delay= 0;<br />NTP_Send.root_dispersion = 0;<br />NTP_Send.reference_identifier= 0;<br />NTP_Send.reference_timestamp= 0;<br />NTP_Send.originate_timestamp= 0;<br />NTP_Send.receive_timestamp= 0;<br />NTP_Send.transmit_timestamp_seconds= 0;<br />NTP_Send.transmit_timestamp_fractions = 0; </p><p>if(SOCKET_ERROR==sendto(soc,(const char*)&NTP_Send,sizeof(NTP_Send),<br />0,(struct sockaddr*)&addrSrv,sizeof(addrSrv)))<br />{<br />closesocket(soc);<br />return FALSE;<br />}<br />int sockaddr_Size =sizeof(addrSrv);<br />if(SOCKET_ERROR==recvfrom(soc,(char*)&NTP_Recv,sizeof(NTP_Recv),<br />0,(struct sockaddr*)&addrSrv,&sockaddr_Size))<br />{<br />closesocket(soc);<br />return FALSE;<br />}<br />closesocket(soc);<br />WSACleanup();</p><p>SYSTEMTIMEnewtime;<br />floatSplitseconds;<br />structtm*lpLocalTime;<br />time_tntp_time;</p><p>// 擷取時間伺服器的時間<br />ntp_time= ntohl(NTP_Recv.transmit_timestamp_seconds)-2208988800;<br />lpLocalTime = localtime(&ntp_time);<br />if(lpLocalTime == NULL)<br />{<br />return FALSE;<br />}</p><p>// 擷取新的時間<br />newtime.wYear =lpLocalTime->tm_year+1900;<br />newtime.wMonth =lpLocalTime->tm_mon+1;<br />newtime.wDayOfWeek =lpLocalTime->tm_wday;<br />newtime.wDay =lpLocalTime->tm_mday;<br />newtime.wHour =lpLocalTime->tm_hour;<br />newtime.wMinute =lpLocalTime->tm_min;<br />newtime.wSecond =lpLocalTime->tm_sec;</p><p>// 設定時間精度<br />Splitseconds=(float)ntohl(NTP_Recv.transmit_timestamp_fractions);<br />Splitseconds=(float)0.000000000200 * Splitseconds;<br />Splitseconds=(float)1000.0 * Splitseconds;<br />newtime.wMilliseconds = (unsigned short)Splitseconds;</p><p>// 修改本機系統時間<br />SetLocalTime(&newtime);<br />return TRUE;<br />}<br />