linux C++通过ntp协议获取网络时间
转自:http://blog.csdn.net/ccjjyy/article/details/42871993
#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;
tmit-= 2209017600U;
//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;
}
最新文章
- VB.NET Winform的一些功能实现
- CSS样式案例(1)-文字的排版
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
- 【Android测试】【随笔】性能采集工具——小松鼠诞生记
- 浏览器获取ip地址
- 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数
- mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)” 解决办法
- 【Hadoop学习】Super用户以其他用户的名义执行操作
- UITableView的简单使用
- JAVA-前台编码,后台解码
- java导入导出excel常用操作小结及简单示例
- 获取客户端IP地址定位城市信息
- 我的学习笔记_Windows_HOOK计划 2009-12-03 11:19
- bzoj1588: [HNOI2002]营业额统计 splay瞎写
- 用户体验 | 寻找成套的 App SDK 服务
- [POI2009]KAM-Pebbles
- hive高阶1--sql和hive语句执行顺序、explain查看执行计划、group by生成MR
- SQL NOT NULL 约束
- BZOJ4816 [Sdoi2017]数字表格 数论 莫比乌斯反演
- XSS笔记
热门文章
- delphi 集合的用法
- UVA 562 Dividing coins (01背包)
- Date Math SimpleDateFormat 类
- 关于jquery选择器中:first和:first-child和:first-of-type的区别及:nth-child()和:nth-of-type()的区别
- Tomcat7调优及JVM性能优化for Linux环境
- orderby与groupby区别
- 字符串匹配算法之KMP
- jQuery实现的简单分页功能的详细解析
- 框架基础:ajax设计方案(一)---集成核心请求
- HIT Winter Day ACM入门