資源簡(jiǎn)介
C++獲得網(wǎng)絡(luò)時(shí)間函數(shù)庫(kù),包含get_internet_time.cpp,get_internet_time.h兩個(gè)文件,以及調(diào)用示例

代碼片段和文件信息
#include?“get_internet_time.h“
using?namespace?std;
/*
Name: FindHostIP
Function: get?IP?address?across?ServerName
params: const?char?*pServerName
return: IPNumber
*/
IPNumber?FindHostIP(const?char?*pServerName)
{
HOSTENT?*pHostent;
//?Get?hostent?structure?for?hostname:
if?(!(pHostent?=?gethostbyname(pServerName)))
throw?HRException(“could?not?resolve?hostname.“);
//?Extract?primary?IP?address?from?hostent?structure:
if?(pHostent->h_addr_list?&&?pHostent->h_addr_list[0])
return?*reinterpret_cast(pHostent->h_addr_list[0]);
return?0;
}
/*
Name: FillSockAddr
Function: Fill?socket?with?address(serverName)
params: sockaddr_in?*pSockAddr?const?char?*pServerName?int?portNumber
return: None
*/
void?FillSockAddr(sockaddr_in?*pSockAddr?const?char?*pServerName?int?portNumber)
{
//?Set?family?port?and?find?IP
pSockAddr->sin_family?=?AF_INET;
pSockAddr->sin_port?=?htons(portNumber);
pSockAddr->sin_addr.S_un.S_addr?=?FindHostIP(pServerName);
}
/*
Name: RequestHeaders
Function: Key?Function?Request?time?from?time?server
params: const?char?*pServername?time_t?*timeRecv
return: bool?if?get?internet?time?successfullyreturn?trueelsereturn?false
*/
bool?RequestHeaders(const?char?*pServername?time_t?*timeRecv)
{
int?slen?=?0;
//time_t?timeRecv;
SOCKET??????hSocket?=?INVALID_SOCKET;
char????????recvBuf[REC_BUFFER_SIZE];
sockaddr_in?sockAddr?=?{?0?};
bool????????bSuccess?=?true;
try{
//?Lookup?hostname?and?fill?sockaddr_in?structure:
FillSockAddr(&sockAddr?pServername?SERVER_PORT);
//?Create?socket
if?((hSocket?=?socket(AF_INET?SOCK_DGRAM?IPPROTO_UDP))?==?INVALID_SOCKET)
throw?HRException(“could?not?create?socket.“);
//?Connect?to?server
if?(connect(hSocket?reinterpret_cast(&sockAddr)?sizeof(sockAddr))?!=?0)
throw?HRException(“could?not?connect.“);
//cout?<“connected?“?<
//?send?request?part?1
if?(send(hSocket?msg?sizeof(msg)?0)?==?SOCKET_ERROR)
throw?HRException(“failed?to?send?data.“);
//cout?<“Dumping?received?data...\n\n“;
//?Loop?to?print?all?data
slen?=?recv(hSocket?recvBuf?REC_BUFFER_SIZE?0);
//part?where?we?take?time?out?of?recvBuf???????
*timeRecv?=?ntohl(((time_t*)recvBuf)[4]);
if?(*timeRecv>0)*timeRecv?-=?2208988800U;
}
catch?(HRException?e){
cerr?<“\nError:?“?< bSuccess?=?false;
}
if?(hSocket?!=?INVALID_SOCKET){
closesocket(hSocket);
}
return?bSuccess;
}
/*
Name: time_t_2_tm
Function: Transfer?time_t?to?tm
params: const?time_t?*tt?struct?tm?*tim
return: None
Author: Leo?Ma
Date: 2019.09.17
*/
void?time_t_2_tm(const?time_t?*tt?struct?tm?*tim)
{
tim?=?localtime(tt);
tim->tm_year?+=?1900;
tim->tm_mon?+=?1;
}
/*
Name: get_internet_time
Function: check?windows?socket?version?and?get?internet
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????5175??2019-09-18?14:46??get?internet?time\get_internet_time.cpp
?????文件????????1419??2019-09-17?16:58??get?internet?time\get_internet_time.h
?????目錄???????????0??2019-09-18?14:47??get?internet?time\
評(píng)論
共有 條評(píng)論