【问题标题】:Is there any C/C++ library to connect with a remote NTP server? [closed]是否有任何 C/C++ 库可以连接远程 NTP 服务器? [关闭]
【发布时间】:2012-03-08 18:14:21
【问题描述】:

我正在制作一个需要将系统时钟与远程 NTP 服务器同步的 C++ 软件。 目前,我使用“system”命令调用控制台“ntpdate”命令。

..但我认为这样做是一种丑陋的方式。

您知道任何可以让我连接到远程 NTP 服务器的库吗? 谢谢。

【问题讨论】:

  • 当然更好的方法就是安装ntpd 守护程序,它可以让您的系统时钟始终保持同步?
  • 另请参阅stackoverflow.com/q/65342065,了解特定于 Linux 的问答。

标签: c++ ntp


【解决方案1】:

在所述系统上运行 ntpd 以确保时钟正确,而不是让您的软件手动发出同步并可能导致其他应用程序无法享受突然的时间跳跃,尤其是向后跳跃,这不是更好的解决方案吗?

话虽如此,我相信有 libntp。

Google 会为我找到更多内容。

【讨论】:

  • +1 表示ntpd 和短语sudden time jumps。回到未来!
  • @Li-aungYip,是的,手动使用 ntpdate 当文件来自未来时,我遇到了各种进程失败。
  • 我遇到过的最有趣(用户可见)的一个是跳时钟会导致 KDE 的合成窗口管理器崩溃并死掉。显然kwm 自最后一帧渲染以来使用时间戳跟踪其性能,其目的是在事情变得太慢时关闭合成。将时钟向前跳 10 秒使它认为它只获得 0.1 FPS 并触发kwms 死亡。 ;)
  • 这听起来很有趣,并且可能是如何解决的,也许需要尝试唠叨一些 kwin 开发人员关于这是一个多么好的想法。不,他们只是告诉我修复它并在那里发送补丁没有乐趣。
  • 是的,ntpd 似乎是正确的方式:)。感谢您的建议和有趣的 cmets。
【解决方案2】:

这适用于 c++(改编自 c)。它会从 pool.ntp.br 获取 UTC 时间(你必须使用 IP)。如果您设法弄清楚如何获得 DAYTIME(夏令时 - horario verao),请告知。我可以从 UFRJ pads 服务器上得到它,但是 UFRJ 不可靠,他们罢工了半年……

/* This code will query a ntp server for the local time and display

 * it.  it is intended to show how to use a NTP server as a time
 * source for a simple network connected device.
 * This is the C version.  The orignal was in Perl
 *
 * For better clock management see the offical NTP info at:
 * http://www.eecis.udel.edu/~ntp/
 *
 * written by Tim Hogard (thogard@abnormal.com)
 * Thu Sep 26 13:35:41 EAST 2002
 * Converted to C Fri Feb 21 21:42:49 EAST 2003
 * this code is in the public domain.
 * it can be found here http://www.abnormal.com/~thogard/ntp/
 *
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
#include <iostream>

void ntpdate();

int main() {
    ntpdate();
    return 0;
}

void ntpdate() {
//char *hostname=(char *)"163.117.202.33";
//char *hostname=(char *)"pool.ntp.br";
char    *hostname=(char *)"200.20.186.76";
int portno=123;     //NTP is port 123
int maxlen=1024;        //check our buffers
int i;          // misc var i
unsigned char msg[48]={010,0,0,0,0,0,0,0,0};    // the packet we send
unsigned long  buf[maxlen]; // the buffer we get back
//struct in_addr ipaddr;        //  
struct protoent *proto;     //
struct sockaddr_in server_addr;
int s;  // socket
long tmit;   // the time -- This is a time_t sort of

//use Socket;
//
//#we use the system call to open a UDP socket
//socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
proto=getprotobyname("udp");
s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
perror("socket");
//
//#convert hostname to ipaddress if needed
//$ipaddr   = inet_aton($HOSTNAME);
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
//argv[1] );
//i   = inet_aton(hostname,&server_addr.sin_addr);
server_addr.sin_port=htons(portno);
//printf("ipaddr (in hex): %x\n",server_addr.sin_addr);

/*
 * build a message.  Our message is all zeros except for a one in the
 * protocol version field
 * msg[] in binary is 00 001 000 00000000 
 * it should be a total of 48 bytes long
*/

// send the data
printf("sending data..\n");
i=sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
perror("sendto");
// get the data back
struct sockaddr saddr;
socklen_t saddr_l = sizeof (saddr);
i=recvfrom(s,buf,48,0,&saddr,&saddr_l);
perror("recvfr:");

//We get 12 long words back in Network order
/*
for(i=0;i<12;i++) {
    //printf("%d\t%-8x\n",i,ntohl(buf[i]));
    long tmit2=ntohl((time_t)buf[i]);
    std::cout << "Round number " << i << " time is " << ctime(&tmit2)  << std::endl;
}
*/
/*
 * The high word of transmit time is the 10th word we get back
 * tmit is the time in seconds not accounting for network delays which
 * should be way less than a second if this is a local NTP server
 */

//tmit=ntohl((time_t)buf[10]);    //# get transmit time
tmit=ntohl((time_t)buf[4]);    //# get transmit time
//printf("tmit=%d\n",tmit);

/*
 * Convert time to unix standard time NTP is number of seconds since 0000
 * UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January
 * 1970 There has been a trend to add a 2 leap seconds every 3 years.
 * Leap seconds are only an issue the last second of the month in June and
 * December if you don't try to set the clock then it can be ignored but
 * this is importaint to people who coordinate times with GPS clock sources.
 */

tmit-= 2208988800U; 
//printf("tmit=%d\n",tmit);
/* use unix library function to show me the local time (it takes care
 * of timezone issues for both north and south of the equator and places
 * that do Summer time/ Daylight savings time.
 */


//#compare to system time
//printf("Time: %s",ctime(&tmit));

std::cout << "time is " << ctime(&tmit)  << std::endl;
i=time(0);
//printf("%d-%d=%d\n",i,tmit,i-tmit);
//printf("System time is %d seconds off\n",(i-tmit));
std::cout << "System time is " << (i-tmit) << " seconds off" << std::endl;
}

【讨论】:

  • NTPClient 是使用 boost 库的 Windows 的优雅替代方案(确保编译并添加路径)。正如here 所讨论的,unsigned char msg[48]={010,0,0,0,0,0,0,0,0} 中的 ntp.flags.mode 和tmit=ntohl((time_t)buf[4]); 中的缓冲区提取位置可能必须根据您的目标进行更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多