void sendPacket(intfd)
{
unsignedint data[12];structtimeval now;if (sizeof(data) != 48)
{
fprintf(stderr,"size error");return;
}
memset((char*)data, 0, sizeof(data));
data[0] = htonl((LI << 30) | (VN << 27) | (MODE << 24) | (STRATUM << 16) | (POLL << 8) | (PREC & 0xff));
data[1] = htonl(1<<16);
data[2] = htonl(1<<16);
gettimeofday(&now, NULL);
data[10] = htonl(now.tv_sec +JAN_1970);
data[11] =htonl(NTPFRAC(now.tv_usec));
send(fd, data,48, 0);
}void getNewTime(unsigned int *data,struct timeval *ptimeval)
{structNtpTime trantime;
trantime.coarse= Data(10);
trantime.fine= Data(11);
ptimeval->tv_sec = trantime.coarse -JAN_1970;
ptimeval->tv_usec =USEC(trantime.fine);
}double getNtpTime(char *pIP)
{intsockfd;structsockaddr_in addr_src,addr_dst;
fd_set fds;intret;intrecv_len;
unsignedint buf[12];
memset(buf,0,sizeof(buf));intaddr_len;int count = 0;structtimeval recvtv;struct timeval *ptimeval;doubleoffset;structtimeval timeout;
addr_len= sizeof(structsockaddr_in);//local
memset(&addr_src, 0, addr_len);
addr_src.sin_family=AF_INET;
addr_src.sin_addr.s_addr=htonl(INADDR_ANY);
addr_src.sin_port= htons(0);//remote
memset(&addr_dst, 0, addr_len);
addr_dst.sin_family=AF_INET;
addr_dst.sin_addr.s_addr=inet_addr(pIP);
addr_dst.sin_port= htons(123);//ntp默认端口123
if(-1==(sockfd =socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)))
{
debugpri("create socket error!");return -1;
}
ret= bind(sockfd, (struct sockaddr*)&addr_src, addr_len);if(-1==ret)
{
debugpri("bind error!");
close(sockfd);return -1;
}
ret= connect(sockfd, (struct sockaddr*)&addr_dst, addr_len);if(-1==ret)
{
debugpri("connect error!");
close(sockfd);return -1;
}
sendPacket(sockfd);while (count < 20)
{
FD_ZERO(&fds);
FD_SET(sockfd,&fds);
timeout.tv_sec= 0;
timeout.tv_usec= 100000;
ret= select(sockfd + 1, &fds, NULL, NULL, &timeout);if (0 ==ret)
{
count++;
debugpri("ret == 0");
sendPacket(sockfd);
usleep(100*1000);continue;
}if(FD_ISSET(sockfd, &fds))
{
recv_len= recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&addr_dst, (socklen_t*)&addr_len);if(-1==recv_len)
{
debugpri("recvfrom error");
close(sockfd);return -1;
}else if(recv_len > 0)
{
debugpri("receiv data");
getNewTime(buf,ptimeval);
debugpri("sec = %d usec = %d",ptimeval->tv_sec ,ptimeval->tv_usec);break;
}
}else{
debugpri("count %d",count);
usleep(50*1000);
count++;
}
}if(count >=20)
{
debugpri("getNewTime timeout !");
close(sockfd);return -1;
}
close(sockfd);return ptimeval->tv_sec;
}