当前位置: 首页 > news >正文

linux中获取网络时间,linux C++通过ntp协议获取网络时间

转自:http://blog.csdn.net/ccjjyy/article/details/42871993

#include

#include

#include

#include

#include

#include

#include

#include

#include

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

",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..

");

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%-8x

",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

",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;

tmit-= 2209017600U;

//printf("tmit=%d

",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

",i,tmit,i-tmit);

//printf("System time is %d seconds off

",(i-tmit));

std::cout << "System time is " << (i-tmit) << " seconds off" << std::endl;

}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • IT书讯 -- Inside Microsoft SQL Server 2005 Query Tuning and Optimization
  • Linux取消函数折叠,Linux下面对于VIM编辑器的代码折叠使用与screen
  • linux下如何修改终端名称,修改linux终端显示目录和主机名称
  • SQL 2008 T-Prep 之旅
  • 树莓派Linux中文怎样修改,树莓派修改键盘布局和支持中文
  • 计算机基础知识
  • linux中如何输入代码提示错误信息,在linux下代码运行出现段错误,求大神
  • BIEE安装及启动错误的处理(rhel5amp;hpux)
  • 制作linux发行程序,Linux发行版制作过程中的各个步骤
  • 使用screen管理会话
  • linux存储是什么意思,Linux 内存管理中的 RSS 和 VSZ 是什么意思?
  • Microsoft Licenses Flash Lite for Windows Mobile Users
  • HPUX下启动scheduler遇到的问题及解决
  • linux上samba配置全过程,Samba服务器配置和使用全过程
  • linux怎么查看32位,查看Linux是32位还是63位的方法
  • @jsonView过滤属性
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • vue的全局变量和全局拦截请求器
  • vue中实现单选
  • 聊聊redis的数据结构的应用
  • 盘点那些不知名却常用的 Git 操作
  • 入手阿里云新服务器的部署NODE
  • 什么软件可以剪辑音乐?
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 消息队列系列二(IOT中消息队列的应用)
  • 应用生命周期终极 DevOps 工具包
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • #{}和${}的区别?
  • #Lua:Lua调用C++生成的DLL库
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (1)Nginx简介和安装教程
  • (2)Java 简介
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (pojstep1.3.1)1017(构造法模拟)
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (二)springcloud实战之config配置中心
  • (二刷)代码随想录第15天|层序遍历 226.翻转二叉树 101.对称二叉树2
  • (回溯) LeetCode 131. 分割回文串
  • (区间dp) (经典例题) 石子合并
  • (贪心 + 双指针) LeetCode 455. 分发饼干
  • (一)认识微服务
  • (转) ns2/nam与nam实现相关的文件
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • ..回顾17,展望18
  • .form文件_SSM框架文件上传篇
  • .gitignore文件忽略的内容不生效问题解决
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • :class的用法及应用
  • @NestedConfigurationProperty 注解用法
  • @Pointcut 使用
  • [24年新算法]NRBO-XGBoost回归+交叉验证基于牛顿拉夫逊优化算法-XGBoost多变量回归预测