因为上次那个获取网络时间失效了(使用的是37端口),获取网络时间并刷新本地时间(源码1)
所以网上又找了一份代码,使用的是123端口,虽然没有测试成功,这里也做下记录,估计是本机环境问题,过两天再进行测试一下。
// getNTPTime.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
using namespace std;
char *GetHostIP(char *strHost)
{
char *strIP = NULL;
PHOSTENT hostinfo = gethostbyname(strHost);
if(hostinfo != NULL)
{
strIP = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
}
return strIP;
}
BOOL SetTimeout(SOCKET s, int nTime, BOOL bRecv)
{
int ret = ::setsockopt(s, SOL_SOCKET, bRecv ? SO_RCVTIMEO : SO_SNDTIMEO, (char *)&nTime, sizeof(nTime));
return ret != SOCKET_ERROR;
}
time_t getSNTPTime()
{
WSADATA wsa;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 0);
if (WSAStartup(wVersionRequested , &wsa)!=0)
{
cerr << "--->>> Winsock Initialization failed." << endl;
}
time_t t = 0;
vector<char *> vecTimeSvr;
vecTimeSvr.push_back("ntp1.aliyun.com");
vecTimeSvr.push_back("ntp2.aliyun.com");
vecTimeSvr.push_back("ntp3.aliyun.com");
vecTimeSvr.push_back("ntp4.aliyun.com");
vecTimeSvr.push_back("ntp5.aliyun.com");
vecTimeSvr.push_back("ntp6.aliyun.com");
vecTimeSvr.push_back("ntp7.aliyun.com");
/*
vecTimeSvr.push_back("cn.pool.ntp.org");
vecTimeSvr.push_back("0.cn.pool.ntp.org");
vecTimeSvr.push_back("1.cn.pool.ntp.org");
vecTimeSvr.push_back("2.cn.pool.ntp.org");
vecTimeSvr.push_back("3.cn.pool.ntp.org");
vecTimeSvr.push_back("time.windows.com");
/* //开放本段,可以在列表中添加更多的NTP服务器
vecTimeSvr.push_back("pool.ntp.org");
vecTimeSvr.push_back("north-america.pool.ntp.org");
vecTimeSvr.push_back("south-america.pool.ntp.org");
vecTimeSvr.push_back("africa.pool.ntp.org");
vecTimeSvr.push_back("ca.pool.ntp.org");
vecTimeSvr.push_back("uk.pool.ntp.org");
vecTimeSvr.push_back("us.pool.ntp.org");
vecTimeSvr.push_back("au.pool.ntp.org");
vecTimeSvr.push_back("time.nist.gov");
vecTimeSvr.push_back("time-nw.nist.gov");
vecTimeSvr.push_back("europe.pool.ntp.org");
vecTimeSvr.push_back("asia.pool.ntp.org");
vecTimeSvr.push_back("oceania.pool.ntp.org");
*/
SYSTEMTIME systime;
for(size_t i = 0; i < vecTimeSvr.size(); ++i)
{
char *strHostName = vecTimeSvr[i];
char *strIP = GetHostIP(strHostName);
int SIZE_SOCKADDR_IN = sizeof(SOCKADDR_IN);
sockaddr_in SvrIp;
SvrIp.sin_family = AF_INET;
SvrIp.sin_addr.s_addr = inet_addr(strIP);
SvrIp.sin_port = htons(37);
int nLenRcv = 0;
char szBuff[48] = {'\0'};
szBuff[0] = 3 | (4 << 3);
SOCKET sSession = ::socket(AF_INET, SOCK_DGRAM, 0);
SetTimeout(sSession, 6000, FALSE);
if(SOCKET_ERROR != sendto(sSession, szBuff, 48, 0, (sockaddr*)&SvrIp, SIZE_SOCKADDR_IN))
{
SetTimeout(sSession, 6000, TRUE);
nLenRcv = recvfrom(sSession, szBuff, 48, 0, (sockaddr*)&SvrIp, &SIZE_SOCKADDR_IN);
}
else
{
cerr << "--->>> " << strHostName << ": " << strIP <<endl;
cerr << "--->>> send SNTP request failed!" << endl;
}
closesocket(sSession);
if(nLenRcv == 48)
{
t = 0;
for(int i = 40; i <= 43; i++)
{
unsigned char c = (unsigned char)szBuff[i];
t = (t<<8) | c;
}
tm _t;
t -= 2208988800L; // 1970.1.1 0:0:0 - 1900.1.1 0:0:0
localtime_s(&_t, &t);
cerr << "--->>> SNTP's Time - " << (_t.tm_year + 1900) << "-" << (_t.tm_mon + 1) << "-" << (_t.tm_mday) << " " << _t.tm_hour << ":" << _t.tm_min << ":" << _t.tm_sec << endl;
cerr << "--->>> " << strHostName << ": " << strIP <<endl;
cerr << endl;
systime.wYear = _t.tm_year +1900;
systime.wMonth = _t.tm_mon + 1;
systime.wDay = _t.tm_mday;
systime.wHour = _t.tm_hour;
systime.wMinute = _t.tm_min;
systime.wSecond = _t.tm_sec;
systime.wMilliseconds = 0;
systime.wDayOfWeek = -1;
SetLocalTime(&systime);
break;
}
}
if (t == 0)
{
cerr << "--->>> recieve all SNTP's time is failed!" << endl;
}
return t;
}
int _tmain(int argc, _TCHAR* argv[])
{
time_t t_1 = getSNTPTime();
if (t_1 > 0)
{
char strTime[50];
ctime_s(strTime, sizeof(strTime), &t_1);
cerr << "--->>> now is " << strTime << endl;
}
cerr << endl;
system("pause");
return 0;
}