2012-02-17 23 views
5

मैं एक सी ++ सॉफ़्टवेयर बना रहा हूं जिसे रिमोट एनटीपी सर्वर के साथ सिस्टम घड़ी को सिंक्रनाइज़ करने की आवश्यकता है। अभी के लिए, मैं कंसोल "ntpdate" कमांड को कॉल करने के लिए "system" कमांड का उपयोग कर रहा हूं।क्या कोई रिमोट एनटीपी सर्वर से कनेक्ट करने के लिए कोई सी/सी ++ लाइब्रेरी है?

.. लेकिन मुझे लगता है कि ऐसा करने के लिए एक बदसूरत तरीका है।

क्या आप कोई पुस्तकालय जानते हैं जो मुझे दूरस्थ एनटीपी सर्वर से कनेक्ट करने देता है? धन्यवाद।

+1

निश्चित रूप से एक बेहतर तरीका सिर्फ 'ntpd' डेमॉन स्थापित करने के लिए है, जो रखेंगे है आपके सिस्टम की घड़ी हर समय सिंक्रनाइज़ होती है? –

उत्तर

4

क्या यह आपके सिस्टम को मैन्युअल रूप से सिंक जारी करने और संभावित रूप से अन्य अनुप्रयोगों के साथ समस्याएं उत्पन्न करने के बजाय घड़ी को सही करने के लिए सही सिस्टम सुनिश्चित करने के लिए बेहतर समाधान नहीं होगा, विशेष रूप से पीछे की तरफ अचानक समय कूदने का आनंद नहीं लेना।

ऐसा कहा जा रहा है कि libntp मेरा मानना ​​है।

मैं और चीजों को छोड़ दूंगा क्योंकि Google उन्हें मेरे लिए ढूंढता है।

+2

'ntpd' के लिए +1 और वाक्यांश' अचानक समय कूदता है '। वापस भविष्य में! –

+0

@ ली-ऑंगवाईप, हाँ, मैन्युअल रूप से ntpdate का उपयोग करने के बाद, जब भविष्य में फाइलें होती हैं तो सभी प्रकार की प्रक्रिया विफल होती है। –

+1

मेरे पास सबसे मजेदार (उपयोगकर्ता दृश्यमान) है कि घड़ी को कूदने से केडीई के कंपोजिटिंग विंडो मैनेजर को बाहर निकलने और मरने का कारण बनता है। स्पष्ट रूप से 'kwm' अंतिम फ्रेम प्रदान किए जाने के बाद से टाइमस्टैम्प के साथ अपने प्रदर्शन का ट्रैक रखता है, अगर चीजें बहुत धीमी हो जाती हैं तो कंपोजिटिंग बंद करने का इरादा होता है। घड़ी को आगे बढ़ने से 10 सेकंड लगता है कि यह केवल 0.1 एफपीएस प्राप्त कर रहा है और 'केडब्ल्यूएम की मौत ट्रिगर करता है। ;) –

4

यह सी ++ (सी से अनुकूलित) में काम करता है। यह आपको पूल.एनटीपी.बीआर से यूटीसी समय प्राप्त करेगा (आपको आईपी का उपयोग करना होगा)। यदि आप काम करने के लिए प्रबंधन करते हैं कि डेटाइम (डेलाइट सेविंग - होरियो वेराओ) कैसे प्राप्त करें, तो कृपया सलाह दें। मैं UFRJ पैड सर्वर से प्राप्त कर सकते हैं, लेकिन 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 ([email protected]) 
* 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; 
}