ntp 校时程序
//effect:send ntp packet and get the ntp packet ,make the time OK
//2014.7.31 is OK
//
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <netinet/udp.h>
#include <linux/if_ether.h>
#include <pthread.h>
#include <netdb.h>
#include <stddef.h>
///////////////////something for the ntp control;do by zhb //////////////////////////////////////////////
#define int8 char
#define uint8 unsigned char
#define uint32 unsigned int
#define ulong32 unsigned long
#define long32 long
#define int32 int
#define long64 long long
#define u_int32 unsigned long
#define NTP_PORT 123 /*NTP专用端口号字符串*/
#define TIME_PORT 37 /* TIME/UDP端口号 */
#define NTP_PORT_STR "123" /*NTP专用端口号字符串*/
#define NTP_SERVER_IP "192.168.1.21" /*ntp service IP*/
//3600s*24h*(365days*70years+17days)
#define JAN_1970 0x83aa7e80U // /* 1900年~1970年之间的时间秒数 */
///////////////////////////////////////////////////////////
#define PKT_MODE(li_vn_mode) ((u_char)((li_vn_mode) & 0x7))
#define PKT_VERSION(li_vn_mode) ((u_char)(((li_vn_mode) >> 3) & 0x7))
#define PKT_LEAP(li_vn_mode) ((u_char)(((li_vn_mode) >> 6) & 0x3))
//#define debug 1
struct ntp_packet
{
uint8 li_vn_mode;
uint8 stratum;
uint8 poll;
uint8 precision;
ulong32 root_delay;
ulong32 root_dispersion;
// int8 ref_id[4];
ulong32 ref_id;
ulong32 reftimestamphigh;
ulong32 reftimestamplow;
ulong32 oritimestamphigh;
ulong32 oritimestamplow;
ulong32 recvtimestamphigh;
ulong32 recvtimestamplow;
ulong32 trantimestamphigh;
ulong32 trantimestamplow;
};
long64 firsttimestamp,finaltimestamp;
int sendpkt(int sockfd,struct addrinfo * res)
{
struct ntp_packet ntppack,newpack;
//put the date into the ntppack
ntppack.li_vn_mode = 0x23;
ntppack.stratum = 0x02;
ntppack.poll = 0x04;
ntppack.precision = 0xec;
ntppack.root_delay = htonl(1<<16);//root_delay = 1.0sec
ntppack.root_dispersion = htonl(1<<8);//root_dispersion = 0.0039sec
ntppack.ref_id = inet_addr(NTP_SERVER_IP);
//获取初始时间戳T1
firsttimestamp=JAN_1970+time(NULL);//-8*3600
// printf("%lx,%lx\n",JAN_1970,firsttimestamp);
ntppack.reftimestamphigh = htonl(firsttimestamp);
ntppack.oritimestamphigh = htonl(firsttimestamp);
ntppack.recvtimestamphigh = htonl(firsttimestamp);
ntppack.trantimestamphigh= htonl(firsttimestamp);
int i;
for(i=0; i<1; i++)
{
int ret = sendto(sockfd,&ntppack,sizeof(ntppack),0, res->ai_addr, res->ai_addrlen);
if(ret < 0){
perror("sendto");
return 1;
}
}
}
int getresponse(int sockfd,struct addrinfo * res,struct ntp_packet rpkt)
{
// struct ntp_packet rpkt,newpack;
fd_set pending_data;
struct timeval block_time;
char *refid ;
/*调用select()函数,并设定超时时间为1s*/
FD_ZERO(&pending_data);
FD_SET(sockfd, &pending_data);
block_time.tv_sec=10;//how time to ask
block_time.tv_usec=0;
if (select(sockfd + 1, &pending_data, NULL, NULL, &block_time) > 0)
{
int num;
/*接收服务器端的信息*/
if ((num = recvfrom(sockfd, &rpkt,
sizeof(rpkt), 0, res->ai_addr, &res->ai_addrlen)) < 0)
{
perror("recvfrom");
return 0;
}
/* 设置接收NTP包的数据结构 */
int mode = PKT_MODE(rpkt.li_vn_mode);
int version = PKT_VERSION(rpkt.li_vn_mode);
int leap = PKT_LEAP(rpkt.li_vn_mode);
int stratum = rpkt.stratum;
int poll = rpkt.poll;
int precision = rpkt.precision;
//到达客户机时间戳T4
finaltimestamp=time(NULL)+JAN_1970;//-8*3600;
///将网络上传送的大端数据改为小端形式。
rpkt.root_dispersion= ntohl(rpkt.root_dispersion);
rpkt.reftimestamphigh=ntohl(rpkt.reftimestamphigh);
rpkt.reftimestamplow= ntohl(rpkt.reftimestamplow);
rpkt.oritimestamphigh= ntohl(rpkt.oritimestamphigh);
rpkt.oritimestamplow= ntohl(rpkt.oritimestamplow);
rpkt.recvtimestamphigh= ntohl(rpkt.recvtimestamphigh);
rpkt.recvtimestamplow= ntohl(rpkt.recvtimestamplow);
rpkt.trantimestamphigh= ntohl(rpkt.trantimestamphigh);
rpkt.trantimestamplow= ntohl(rpkt.trantimestamplow);
#ifdef debug
printf("li=%d,version=%d,mode=%d\n",leap,version,mode);
printf("stratum=%d,poll=%d,precision=%d\n",stratum,poll,precision);
printf("################ data ####################\n");
printf("root_delay=%ld\n",rpkt.root_delay);
printf("dispersion=%ld\n",rpkt.root_dispersion);
// printf("Id=%s\n",(*(int*)&rpkt.ref_id));
printf("refh=%lx\n",rpkt.reftimestamphigh);
printf("relw=%lx\n",rpkt.reftimestamplow);
printf("orih=%lx\n",rpkt.oritimestamphigh);
printf("oril=%lx\n",rpkt.oritimestamplow);
printf("rech=%lx\n",rpkt.recvtimestamphigh);
printf("recl=%lx\n",rpkt.recvtimestamplow);
printf("trah=%lx\n",rpkt.trantimestamphigh);
printf("tral=%lx\n",rpkt.trantimestamplow);
#endif
long64 diftime,delaytime;
//求出客户机跟服务器的时间差=((T2-T1)+(T3-T4))/2
diftime=((rpkt.recvtimestamphigh-firsttimestamp)+(rpkt.trantimestamphigh-finaltimestamp))>>1;
//求出延时
delaytime=((rpkt.recvtimestamphigh-firsttimestamp)-(rpkt.trantimestamphigh-finaltimestamp))>>1;
//diftime=(5-9)>>1;
//求出真正时间的时间戳
struct timeval tv1;
tv1.tv_sec=time(NULL)+diftime+delaytime;
tv1.tv_usec=0;
settimeofday(&tv1,NULL);
#ifdef debug
printf("\n\ndebug information ...\n\n");
printf("time(NULL) is %ld\n",time(NULL));
printf("different time is %ld\n",diftime);
printf("delaytime is %ld\n",delaytime);
printf("time(NULL)+diftime+delaytime=%ld\n",time(NULL)+diftime+delaytime);
printf("tv1.tv_sec is %ld\n\n", tv1.tv_sec);
#endif
}
return 1;
}
int main()
{
struct addrinfo *res = NULL,hints;
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
/*调用getaddrinfo()函数,获取地址信息*/
int rc = getaddrinfo(NTP_SERVER_IP, NTP_PORT_STR, &hints, &res);
if (rc != 0)
{
perror("getaddrinfo");
return 1;
}
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(fd < 0){
perror("socket");
return 0;
}
sendpkt(fd,res);
int i;
struct ntp_packet rpkt;
i = getresponse(fd,res,rpkt);
printf("time is OK\n");
close(fd);
}
http://www.gpstime.com.cn/chanpin/pinlv/227.html
ntp 校时程序的更多相关文章
- GPS北斗NTP校时服务器原理及功能介绍
在科技的发展下GPS北斗NTP校时服务器也得到了广泛应用,比如工业.科研.航空航天.公共场所等领域都用到了GPS北斗NTP校时服务器,该时间服务器以卫星时间为基准授时准确,替代了传统钟表授时的单一和时 ...
- NTP校时设置
一.Windows Server 2008 – Time Server 前言: 国家时间与频率标准实验室 && NTP服务器 也可以忽略1~6 直接跳7 如果已改过机码请使用 1 ...
- Linux NTP校时
1.安装客户端(root权限运行) apt-get install ntpdate 2.修改配置文件:“/etc/default/ntpdate”, NTPSERVERS="ntp. ...
- 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器
北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 论述当下网络时间同步 ...
- 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器
网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 论述当下网络时间同步的重要性 北京华人开创科技发展有限公 ...
- GPS校时器,GPS时钟装置,NTP网络时间服务器
GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间 ...
- 实现基于NTP协议的网络校时功能
无论PC端还是移动端系统都自带时间同步功能,基于的都是NTP协议,这里使用C#来实现基于NTP协议的网络校时功能(也就是实现时间同步). 1.NTP原理 NTP[Network Time Protoc ...
- KNY团队与“易校”小程序介绍
一.团队介绍 “KNY”团队是软件工程专业中的一支充满了斗志,充满了自信的队伍,由三人组成,每个队员都在为我们共同一致的目标而努力:我们三个人的小程序的知识都相对薄弱,但我们不甘落后,一直在努力的学习 ...
- 记一个有想法却没能力实现的硬件产品——mp3校时闹钟
枕头旁的闹钟,我想大家都用过,很便宜.用一节干电池供电.但其最大的缺点就是不太准,不能校时. 电池啥事用光,也不知道.钟是走的很慢,没按时闹,搞的自己迟了到. 于是就有了我的漫长思考过程... 先说手 ...
随机推荐
- 【刷题】洛谷 P2675 《瞿葩的数字游戏》T3-三角圣地
题目背景 国王1带大家到了数字王国的中心:三角圣地. 题目描述 不是说三角形是最稳定的图形嘛,数字王国的中心便是由一个倒三角构成.这个倒三角的顶端有一排数字,分别是1 ~ N.1 ~ N可以交换位置. ...
- C++ 指针[转+原创]
要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区. 指针的类型 从语法的角度看,你只要把指针声明语句里的指针名字去掉 ...
- 洛谷 P2900 [USACO08MAR]土地征用Land Acquisition 解题报告
P2900 [USACO08MAR]土地征用Land Acquisition 题目描述 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选 ...
- 洛谷 P4754 True Vegetable 解题报告
P4754 True Vegetable 题目描述 小A现在有N道题,编号为1,2,⋯,N.每道题的起始毒瘤程度为0或1.在每天,小A可以将编号连续的K道题的毒瘤程度+1.但小B因为本身比较菜,不是很 ...
- 第一个python程序(2)
输入和输出 输出 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' pr ...
- luoguP5105 不强制在线的动态快速排序
emm 可重集合没用用.直接变成不可重复集合 有若干个区间 每个区间形如[L,R] [L,R]计算的话,就是若干个连续奇数的和.拆位统计1的个数 平衡树维护 加入一个[L,R],把相交的区间合并.之后 ...
- 微信小程序传值
方式一:通过设置id方式传值 <button class="btninvest" bindtap="goinvet" id="{{item.tx ...
- c#代码访问https服务器以及https的webservice
代码访问https类似浏览器操作 1.验证证书 2.如果要求客户端证书,提供客户端证书 具体代码如下: 访问https的web public static void ProcessRequest() ...
- js获取本周、上周的开始结束时间
这两天在做一个报表体统,其中涉及到了一个根据本周,上周,本月,上月的时间来进行查询的问题,在这个我就教一下大家怎么实现,大家如果有更好的实现方法的,我也希望大家能说出来,我们交流交流. 首先呢,我写了 ...
- 集合框架小结-List
List集合存储的元素是有序的,可重复的,有索引 List特有方法: 增加 add(index, element); addAll(index, Collection); 删除 remove(inde ...