在C++中,你可以使用网络时间协议(NTP)来获取网络时间
#include<iostream>
#include <ctime>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
const int NTP_PACKET_SIZE = 48;
const int NTP_PORT = 123;
const int NTP_LEAP_MASK = 0xC0000000;
const int NTP_VERSION_MASK = 0x38000000;
const int NTP_MODE_MASK = 0x07000000;
const int NTP_STRATUM_MASK = 0x00FF0000;
const int NTP_POLL_MASK = 0x0000E000;
const int NTP_PRECISION_MASK = 0x000000E0;
const int NTP_DELAY_MASK = 0x00001F00;
const int NTP_DISPERSION_MASK = 0x0000001F;
unsigned long ntohl(unsigned long x) {
return (((x & 0x000000ff) << 24) |
((x & 0x0000ff00) << 8) |
((x & 0x00ff0000) >> 8) |
((x & 0xff000000) >> 24));
}
int main() {
int sockfd;
struct sockaddr_in serv_addr;
char buffer[NTP_PACKET_SIZE];
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd < 0) {
std::cerr << "Error: Unable to create socket"<< std::endl;
return -1;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(NTP_PORT);
inet_pton(AF_INET, "pool.ntp.org", &(serv_addr.sin_addr));
memset(buffer, 0, NTP_PACKET_SIZE);
*(unsigned long *)buffer = htonl((NTP_LEAP_MASK | NTP_VERSION_MASK | NTP_MODE_MASK) >> 8);
if (sendto(sockfd, buffer, NTP_PACKET_SIZE, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Error: Unable to send data"<< std::endl;
return -1;
}
if (recvfrom(sockfd, buffer, NTP_PACKET_SIZE, 0, nullptr, nullptr) < 0) {
std::cerr << "Error: Unable to receive data"<< std::endl;
return -1;
}
unsigned long seconds = ntohl(*(unsigned long *)&buffer[40]);
unsigned long fraction = ntohl(*(unsigned long *)&buffer[44]);
time_t network_time = (time_t)(seconds - 2208988800UL);
std::cout << "Network time: " << ctime(&network_time);
close(sockfd);
return 0;
}
这个示例代码首先创建一个UDP套接字,然后将其连接到NTP服务器。接下来,它发送一个NTP请求包,并接收服务器的响应。最后,它从响应中提取网络时间,并将其打印出来。
注意:这个示例代码仅适用于IPv4地址。要使其支持IPv6,你需要对代码进行相应的修改。此外,这个示例代码没有处理可能的错误情况,例如服务器无法连接或响应超时等。在实际应用中,你可能需要添加更多的错误处理和异常捕获。