VC automatically synchronizes updates with Internet time servers

Source: Internet
Author: User

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?
  1. #include <WinSock.h>
  2. #pragma comment (lib, "Ws2_32")
  3. struct Ntp_packet
  4. {
  5. int Control_word;
  6. int root_delay;
  7. int root_dispersion;
  8. int reference_identifier;
  9. __int64 Reference_timestamp;
  10. __int64 Originate_timestamp;
  11. __int64 Receive_timestamp;
  12. int transmit_timestamp_seconds;
  13. int transmit_timestamp_fractions;
  14. };

[CPP]View PlainCopyprint?
  1. /************************************************************************/
  2. /* Function Description: Automatically synchronize updates with the time server
  3. /* Parameter Description: None
  4. /* Return Value: Success returns TRUE, Failure returns false
  5. /************************************************************************/
  6. BOOL Updatesystime ()
  7. {
  8. WORD wversionrequested;
  9. Wsadata Wsadata;
  10. //Initialize version
  11. wversionrequested = Makeword (1, 1);
  12. if (0!=wsastartup (wversionrequested, &wsadata))
  13. {
  14. WSACleanup ();
  15. return FALSE;
  16. }
  17. if (Lobyte (wsadata.wversion)!=1 | | Hibyte (wsadata.wversion)!=1)
  18. {
  19. WSACleanup ();
  20. return FALSE;
  21. }
  22. //This IP is China mainland time synchronization server address, can be modified by itself
  23. SOCKET Soc=socket (AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  24. struct sockaddr_in addrsrv;
  25. Addrsrv.sin_addr. S_un.  S_addr=inet_addr ("210.72.145.44");
  26. Addrsrv.sin_family=af_inet;
  27. Addrsrv.sin_port=htons (123);
  28. Ntp_packet Ntp_send,ntp_recv;
  29. Ntp_send.control_word = htonl (0x0b000000);
  30. Ntp_send.root_delay = 0;
  31. ntp_send.root_dispersion = 0;
  32. Ntp_send.reference_identifier = 0;
  33. Ntp_send.reference_timestamp = 0;
  34. Ntp_send.originate_timestamp = 0;
  35. Ntp_send.receive_timestamp = 0;
  36. ntp_send.transmit_timestamp_seconds = 0;
  37. ntp_send.transmit_timestamp_fractions = 0;
  38. if (Socket_error==sendto (Soc, (const char*) &ntp_send,sizeof (ntp_send),
  39. 0, (struct sockaddr*) &addrsrv,sizeof (ADDRSRV)))
  40. {
  41. Closesocket (SOC);
  42. return FALSE;
  43. }
  44. int sockaddr_size =sizeof (ADDRSRV);
  45. if (Socket_error==recvfrom (Soc,char*) &ntp_recv,sizeof (NTP_RECV),
  46. 0, (struct sockaddr*) &addrsrv,&sockaddr_size))
  47. {
  48. Closesocket (SOC);
  49. return FALSE;
  50. }
  51. Closesocket (SOC);
  52. WSACleanup ();
  53. SYSTEMTIME NewTime;
  54. float splitseconds;
  55. struct TM *lplocaltime;
  56. time_t ntp_time;
  57. //Get time on server
  58. Ntp_time = Ntohl (ntp_recv.transmit_timestamp_seconds)-2208988800;
  59. Lplocaltime = LocalTime (&ntp_time);
  60. if (lplocaltime = = NULL)
  61. {
  62. return FALSE;
  63. }
  64. //Get new Time
  65. Newtime.wyear =lplocaltime->tm_year+1900;
  66. Newtime.wmonth =lplocaltime->tm_mon+1;
  67. Newtime.wdayofweek =lplocaltime->tm_wday;
  68. Newtime.wday =lplocaltime->tm_mday;
  69. Newtime.whour =lplocaltime->tm_hour;
  70. Newtime.wminute =lplocaltime->tm_min;
  71. Newtime.wsecond =lplocaltime->tm_sec;
  72. //Set time accuracy
  73. splitseconds= (float) Ntohl (ntp_recv.transmit_timestamp_fractions);
  74. splitseconds= (float) 0.000000000200 * splitseconds;
  75. splitseconds= (float) 1000.0 * splitseconds;
  76. Newtime.wmilliseconds = (unsigned short ) splitseconds;
  77. //Modify the native system time
  78. Setlocaltime (&newtime);
  79. return TRUE;
  80. }

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

VC automatically synchronizes updates with Internet time servers

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.