久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
嵌入式系統NTP時間同步軟件例子程序
[打印本頁]
作者:
wolfgun
時間:
2017-10-10 15:38
標題:
嵌入式系統NTP時間同步軟件例子程序
嵌入式系統NTP時間同步軟件例子程序
source code
/* 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>
void ntpdate();
int main() {
ntpdate();
return 0;
}
void ntpdate() {
char *hostname="192.168.15.100";
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
int 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<48;i++)
//printf("%d\t%-8x\n",i,ntohl(buf[i]));
printf("%02X-", buf[i]);
/*
* 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
printf("\ntmit=%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));
i=time(0);
//printf("%d-%d=%d\n",i,tmit,i-tmit);
printf("System time is %d seconds off\n",i-tmit);
}
復制代碼
全部資料下載地址:
ntpdate.rar
(1.84 KB, 下載次數: 48)
2017-10-10 15:38 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
热久久性
|
国产精品国产三级国产aⅴ无密码
|
日本精品一区二区三区视频
|
国产成人小视频
|
9久9久9久女女女九九九一九
|
99久久精品国产毛片
|
国产亚洲精品精品国产亚洲综合
|
国产乱码精品一品二品
|
99久久精品免费
|
91精品国产一区
|
日韩在线高清
|
欧美久久久久久久
|
久久精品播放
|
欧美日本一区二区
|
在线视频一区二区三区
|
一区二区三区国产
|
欧美一区二区在线观看
|
日本不卡在线观看
|
a级大片
|
久久国产精品网
|
日本精品一区二区三区在线观看
|
男插女下体视频
|
色欧美片视频在线观看
|
久久综合香蕉
|
久久久久久久av
|
一级特黄网站
|
成人久久网
|
亚洲二区在线
|
aaaa日韩
|
国产精品成人在线播放
|
91一区二区三区
|
日韩精品一区二区三区久久
|
奇米超碰在线
|
一级片在线视频
|
日本中文字幕在线视频
|
99免费
|
国产丝袜一区二区三区免费视频
|
久久久国产精品
|
国产精品欧美精品
|
国产毛片视频
|
超碰成人免费观看
|