In the vckbase, Csdn dug a long tomb, only to find some kind of information, and finally sorted out such a function, the VC to achieve time synchronization, many do not say, their own view of the source, according to their own needs can be appropriate to modify the source code:
[CPP]View PlainCopyprint?
- #include <WinSock.h>
- #pragma comment (lib, "Ws2_32")
- struct Ntp_packet
- {
- int Control_word;
- int root_delay;
- int root_dispersion;
- int reference_identifier;
- __int64 Reference_timestamp;
- __int64 Originate_timestamp;
- __int64 Receive_timestamp;
- int transmit_timestamp_seconds;
- int transmit_timestamp_fractions;
- };
[CPP]View PlainCopyprint?
- /************************************************************************/
- /* Function Description: Automatically synchronize updates with the time server
- /* Parameter Description: None
- /* Return Value: Success returns TRUE, Failure returns false
- /************************************************************************/
- BOOL Updatesystime ()
- {
- WORD wversionrequested;
- Wsadata Wsadata;
- //Initialize version
- wversionrequested = Makeword (1, 1);
- if (0!=wsastartup (wversionrequested, &wsadata))
- {
- WSACleanup ();
- return FALSE;
- }
- if (Lobyte (wsadata.wversion)!=1 | | Hibyte (wsadata.wversion)!=1)
- {
- WSACleanup ();
- return FALSE;
- }
- //This IP is China mainland time synchronization server address, can be modified by itself
- SOCKET Soc=socket (AF_INET,SOCK_DGRAM,IPPROTO_UDP);
- struct sockaddr_in addrsrv;
- Addrsrv.sin_addr. S_un. S_addr=inet_addr ("210.72.145.44");
- Addrsrv.sin_family=af_inet;
- Addrsrv.sin_port=htons (123);
- Ntp_packet Ntp_send,ntp_recv;
- Ntp_send.control_word = htonl (0x0b000000);
- Ntp_send.root_delay = 0;
- ntp_send.root_dispersion = 0;
- Ntp_send.reference_identifier = 0;
- Ntp_send.reference_timestamp = 0;
- Ntp_send.originate_timestamp = 0;
- Ntp_send.receive_timestamp = 0;
- ntp_send.transmit_timestamp_seconds = 0;
- ntp_send.transmit_timestamp_fractions = 0;
- if (Socket_error==sendto (Soc, (const char*) &ntp_send,sizeof (ntp_send),
- 0, (struct sockaddr*) &addrsrv,sizeof (ADDRSRV)))
- {
- Closesocket (SOC);
- return FALSE;
- }
- int sockaddr_size =sizeof (ADDRSRV);
- if (Socket_error==recvfrom (Soc,char*) &ntp_recv,sizeof (NTP_RECV),
- 0, (struct sockaddr*) &addrsrv,&sockaddr_size))
- {
- Closesocket (SOC);
- return FALSE;
- }
- Closesocket (SOC);
- WSACleanup ();
- SYSTEMTIME NewTime;
- float splitseconds;
- struct TM *lplocaltime;
- time_t ntp_time;
- //Get time on server
- Ntp_time = Ntohl (ntp_recv.transmit_timestamp_seconds)-2208988800;
- Lplocaltime = LocalTime (&ntp_time);
- if (lplocaltime = = NULL)
- {
- return FALSE;
- }
- //Get new Time
- Newtime.wyear =lplocaltime->tm_year+1900;
- Newtime.wmonth =lplocaltime->tm_mon+1;
- Newtime.wdayofweek =lplocaltime->tm_wday;
- Newtime.wday =lplocaltime->tm_mday;
- Newtime.whour =lplocaltime->tm_hour;
- Newtime.wminute =lplocaltime->tm_min;
- Newtime.wsecond =lplocaltime->tm_sec;
- //Set time accuracy
- splitseconds= (float) Ntohl (ntp_recv.transmit_timestamp_fractions);
- splitseconds= (float) 0.000000000200 * splitseconds;
- splitseconds= (float) 1000.0 * splitseconds;
- Newtime.wmilliseconds = (unsigned short ) splitseconds;
- //Modify the native system time
- Setlocaltime (&newtime);
- return TRUE;
- }
http://blog.csdn.net/wangningyu/article/details/4522648
VC automatically synchronizes updates with Internet time servers