在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,你需要對代碼進行相應的修改。此外,這個示例代碼沒有處理可能的錯誤情況,例如服務器無法連接或響應超時等。在實際應用中,你可能需要添加更多的錯誤處理和異常捕獲。