vc++6.0 同步本机时间到Internet NTP服务器 编译通过
2010-11-17 08:44
435 查看
vc++6.0 同步本机时间到Internet NTP服务器 编译通过
2009-07-24 23:50
原文网址: http://hi.baidu.com/paty/blog/item/b7b58db708ba79fd31add13e.html
从授时服务器上获得时间(非常详尽)http://blog.csdn.net/shjte/archive/2010/04/29/5543420.aspx
NTP服务器列表: http://support.ntp.org/bin/view/Servers/NTPPoolServers
2009-07-24 23:50
#include <windows.h> #include <winsock.h> #include <time.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; }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 1, 1 ); if (0!=WSAStartup(wVersionRequested, &wsaData)) { WSACleanup(); return 0; } if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) { WSACleanup( ); return 0; } SOCKET soc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); struct sockaddr_in addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("192.43.244.18"); 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 0; } 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 0; } closesocket(soc); WSACleanup(); SYSTEMTIME newtime; struct tm *lpLocalTime; float Splitseconds; time_t ntp_time=ntohl(NTP_Recv.transmit_timestamp_seconds)-2208988800; lpLocalTime=localtime(&ntp_time); if(lpLocalTime==NULL) { return 0; } 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; Splitseconds=(float)ntohl(NTP_Recv.transmit_timestamp_fractions); Splitseconds=(float)0.000000000200 * Splitseconds; Splitseconds=(float)1000.0 * Splitseconds; newtime.wMilliseconds = (unsigned short)Splitseconds; SetLocalTime(&newtime); return 0; } |
从授时服务器上获得时间(非常详尽)http://blog.csdn.net/shjte/archive/2010/04/29/5543420.aspx
NTP服务器列表: http://support.ntp.org/bin/view/Servers/NTPPoolServers
相关文章推荐
- 通过NTP服务器实现时间同步
- 电脑时间经常出错怎么解决?win7同步Internet时间设置教程
- 自动调整linux系统时间和时区与Internet时间同步
- 通过Excel分析测试数据同步复制持续时间
- LInux下可以使用命令自动与网络的NTP服务器同步时间 CSDN验证码验证失败问题解决
- Win10 时间与Internet时间同步超时
- FFMPEG教程5_同步视频(使用2014年新SDK重新整理编译通过)
- Android 通过NTP服务器自动获取时间的方法
- 调整linux系统时间和时区与Internet时间同步
- Windows局域网通过NTP设置时间同步
- server2008r2域控时间设置internet时间同步的方法
- 在VC++6.0中,编译,调试都能通过,但运行到某一步的时候就报错的可能原因。
- Linux内网服务器从Windows通过NTP同步时间
- 使用NTP服务器完美解决VMware Linux时间无法同步问题
- FFMPEG教程6_同步音频(使用2014年新SDK重新整理编译通过)
- Linux系统日期时间设定方法及与Internet时间同步的设置
- python第三方库推荐 - 通过ntplib在windows上同步时间
- Windows下通过VC++6.0编译live555工程(可调试)
- windows 2008自动与internet同步时间的方法
- WinCE与PC通过ActiveSync同步后时间相差1小时