资源简介
C++获得网络时间函数库,包含get_internet_time.cpp,get_internet_time.h两个文件,以及调用示例
代码片段和文件信息
#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 “ << inet_ntoa(sockAddr.sin_addr) << “:“ << SERVER_PORT << endl;
// 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: “ << e.what() << endl;
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 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\
- 上一篇:C++ 汉字识别源代码
- 下一篇:利用多个二维点拟合圆、多个三维点拟合球面
相关资源
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
- c++ 虚拟摄像头
- hook,捕获所有案件,查找所有窗口,
- C语言课设计算器
- c++ 简易贪吃蛇源码
- 高精度加法(c++代码)
- C++调用百度地图案例
- 北京化工大学计算方法(C/C++)讲义
- 基于VC++的SolidWorks二次开发SolidWorks
- c++ 模拟鼠标按键
- OFD编辑器
- Beginning C++17 From Novice to Professional
- C++ STL实现
- opencv手部轮廓识别以及轨迹识别
- 百度C++编码规范
- C++ sql2008 WebServer通讯.docx
- c++ 定时关机程序源码
- 基于VSCode和CMake实现C++开发
- c++语法查询工具
- c++ 账务系统源码
- GBT 28169-2011 嵌入式软件 C语言编码规范
- c++ 猜拳小游戏
- XUnZip Zip解压缩.rar
评论
共有 条评论