#include
#include
#include
#include
#include
#include
#define debug 1
#define TIMEOUT 3
#define JAN_1970 0x83AA7E80
#define NTP_SERVER_1 "114.80.81.1" //cn.pool.ntp.org
#define NTP_SERVER_2 "122.226.192.4" //cn.pool.ntp.org
#define NTP_SERVER_3 "218.75.4.130" //cn.pool.ntp.org
void construct_ntp_packet(char content[])
{
long timer;
memset(content, 0, 48);
content[0] = 0x1b; // LI = 0 ; VN = 3 ; Mode = 3 (client);
time((time_t *)&timer);
timer = htonl(timer + JAN_1970 );
memcpy(content + 40, &timer, sizeof(timer)); //trans_timastamp
}
int get_ntp_time(int sockfd, struct sockaddr_in *server_addr, struct tm *net_tm)
{
char content[256];
time_t timet;
long temp;
int addr_len = 16;
struct timeval block_time;
fd_set sockfd_set;
FD_ZERO(&sockfd_set);
FD_SET(sockfd, &sockfd_set);
block_time.tv_sec = TIMEOUT; //time out
block_time.tv_usec = 0;
construct_ntp_packet(content);
if (sendto(sockfd, content, 48, 0, (struct sockaddr *)server_addr, addr_len) < 0) {
#if debug
perror("sendto error");
#endif
return (-1);
}
if(select(sockfd + 1, &sockfd_set, NULL, NULL, &block_time ) > 0) {
if (recvfrom(sockfd, content, 256, 0, (struct sockaddr *)server_addr, (socklen_t *)&addr_len) < 0) {
#if debug
perror("recvfrom error");
#endif
return (-1);
}
else {
memcpy(&temp, content + 40, 4);
temp = (time_t)(ntohl(temp) - JAN_1970 );
timet = (time_t)temp;
memcpy(net_tm, gmtime(&timet), sizeof(struct tm));
net_tm->tm_hour = net_tm->tm_hour + 8; //beijing time zone
}
}
else {
return(-1);
}
return(0);
}
int main()
{
int sockfd, i;
struct tm *net_tm;
struct sockaddr_in addr;
char ip[4][16]= { { NTP_SERVER_1 } , { NTP_SERVER_2 }, { NTP_SERVER_3 } };
char date_buf[50];
net_tm = (struct tm *)malloc(sizeof(struct tm));
for (i = 0 ; i < 3 ; i++ ) {
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = inet_addr( ip[i] );
addr.sin_port = htons(123);
if((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
#if debug
perror ("socket error");
#endif
return (-1);
}
if (get_ntp_time(sockfd, &addr, net_tm) == 0) {
break;
}
close(sockfd);
}
strftime(date_buf, sizeof(date_buf), "date -s \"%F %T\"", net_tm);
system(date_buf);
return (0);
}
//tm 结构体
struct tm {
int tm_sec; /* 秒–取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值从1900开始 */
int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
tm->tm_sec;.....
阅读(823) | 评论(0) | 转发(0) |