VC automatically synchronizes updates with the Internet Time Server

Source: Internet
Author: User

After digging for a long time in vckbase and csdn, I found some useful information and finally sorted out such a function. In this way, VC achieves time synchronization, not to mention it, you can modify the source code as needed:

 

# 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/> /* function Description: automatic synchronization with the time server <br/>/* parameter description: None <br/>/* return value: True is returned successfully, false is returned for failure. <br/> /******************************** **************************************** /<br/> bool updatesystime () <br/>{< br/> wordwversionrequested; <br/> wsadata; </P> <p> // initialize the version <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> // this IP address is a time synchronization server address in mainland China. You can modify it by yourself <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/> latency = 0; <br/> latency = 0; <br/> ntp_send.originate_timestamp = 0; <br/> ntp_send.receive_timestamp = 0; <br/> ntp_send.transmit_timestamp_seconds = 0; <br/> latency = 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> // obtain the time server time <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> // obtain the new time <br/> newtime. wyear = lplocaltime-> maid + 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> // set the time precision <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> // modify the local system time <br/> setlocaltime (& newtime); <br/> return true; <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.