[转]取标准时间 // 中国国家授时中心的IP地址是:210.72.145.44

[转]取标准时间 // 中国国家授时中心的IP地址是:210.72.145.44


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;
};

BOOL CImageMovableApp::UpdateSysTime()
{
    WORD    wVersionRequested;
    WSADATA wsaData;
   
    // 初始化版本
    wVersionRequested = MAKEWORD( 1, 1 );
    if (0!=WSAStartup(wVersionRequested, &wsaData))
    {
        WSACleanup();
        return FALSE;
    }
    if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)
    {
        WSACleanup( );
        return FALSE;
    }
   
    // 这个IP是中国大陆时间同步服务器地址,可自行修改
 // 中国国家授时中心的IP地址是:210.72.145.44
    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_addr.S_un.S_addr=inet_addr("time-a.timefreq.bldrdoc.gov");
    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;
   
    // 获取时间服务器的时间
    ntp_time    = ntohl(NTP_Recv.transmit_timestamp_seconds)-2208988800;
    lpLocalTime = localtime(&ntp_time);
    if(lpLocalTime == NULL)
    {
        return FALSE;
    }
   
    // 获取新的时间
    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 TRUE;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值