NTP协议应用(获取网络时间)

1 篇文章 0 订阅
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
#include <stdlib.h>

#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);
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值