vc++6.0 同步本机时间到Internet NTP服务器 编译通过
来源:互联网 发布:linux 邮件发送外网 编辑:程序博客网 时间:2024/05/22 14:11
#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://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
- vc++6.0 同步本机时间到Internet NTP服务器 编译通过
- 本机时间自动与Internet时间服务器同步出错怎么办?
- NTP物理机时间同步应用
- access 中如何使本机时间与服务器时间同步?
- ARM-Linux使用ntpdate同步本机时间
- Linux内网服务器从Windows通过NTP同步时间
- 时间同步服务器NTP
- ntp时间同步服务器
- NTP服务器时间同步
- NTP时间同步服务器
- 取本机时间
- 获取本机时间
- 根据服务器的时间自动调整本机时间
- VC自动与Internet时间服务器同步更新
- 通过 NTP 进行时钟同步
- 与服务器同步时间NTP
- NTP服务器(时间同步)
- ntp常用时间同步服务器
- 性能测试(并发负载压力)测试分析
- SMT设备操作指导
- 更全面的 用Squid搭建Cache服务器
- 性能测试(并发负载压力)测试分析
- vi/vim键盘图
- vc++6.0 同步本机时间到Internet NTP服务器 编译通过
- CSS+DIV 成为网站标准的原因
- 《VC编程指南》第十章 数据库编程(一)
- FaceBook - social network
- Hibernate 数据不同同步,重启Tomcat后才同步的问题解决
- linux下设置默认启动选项
- 中国500大优秀网站(1)
- 10.04在ati显卡上花屏解决方法
- 面试题