打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
NTP校时客户端
void *CNtpManager::NtpThreadProj()
{
    int time_cnt = 0;
while(1)
{
   ++time_cnt; //利用NTP线程定时同步系统时间到RTC, 间隔1小时
        if (time_cnt % 3600 == 3599)
        {
            int year, month, day, week, hour, minute, seconds;
            GetUtcSystemTime(&year, &month, &day, &week, &hour, &minute, &seconds);
            DEBUG_ERROR("Timer sync time to RTC, current UTC time=%04d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, seconds);
            CRtc::Rtc_SetTime(year, month, day, hour, minute, seconds);
            time_cnt = 0;
        }

if(m_bNtpEnable)
{
int ret = -1;
time_t curTime = 0;
if((m_ntpSwitch) || CheckTimeTickEvent())
{
ret = GetUtcTime();
if(ret == 0)
{
curTime = GetTimerUtcSecond();
DEBUG_DEBUG("ntp -->curTime=%ld - m_ntpTime=%ld = %d ", curTime, m_ntpTime, curTime - m_ntpTime);
if(abs(curTime - m_ntpTime) >= 5) //5秒差距
{
SystemTimeDef_S sysTime;
#if 0
m_ntpTime = m_ntpTime + m_timeZone * 3600;
#endif
ChangeUtcToSysinfo(m_ntpTime, &sysTime);
sysTime.timeZone = m_timeZone;

DEBUG_DEBUG("ntp -->befor EVENT_DEVMANAGE_SET_NTP_TIME ");
g_messageClient->SendMessage( 0, EVENT_DEVMANAGE_SET_NTP_TIME, 0, (const char *)&sysTime, sizeof(sysTime), 1);
DEBUG_DEBUG("ntp -->after EVENT_DEVMANAGE_SET_NTP_TIME ");
}
else
{
DEBUG_DEBUG("ntp -->time is Precise, cur[%lu]--ntp[%lu]!!!", curTime, m_ntpTime);
}
}
m_ntpSwitch = false;
m_lastUpdateTime =  GetTimerInterval();
}
}
usleep(1 * 1000 * 1000);
}
}



****************************************************************************************
int CNtpManager::GetUtcTime(void)
{
    /*
     * 220.130.158.71: 台湾授时中心
     * time.windows.com   微软
     * asia.pool.ntp.org 台湾
     * time.nist.gov 美国
     * ntp.fudan.edu.cn 复旦大学-----大陆推荐使用该主机
     * ntp.sjtu.edu.cn 上海交通大学
     *
    */
fd_set inset;
int sockfd;
struct timeval tv,tv1;
struct sockaddr_in addr;
int ret;
long long firsttimestamp, finaltimestamp;
long long diftime, delaytime;
NTPPacket_S ntppack, newpack;
char szIpAddr[16];

m_ntpTime = 0;
if((sockfd=socket(AF_INET, SOCK_DGRAM, 0))<0)
{
close(sockfd);
perror("create socket error!");
DEBUG_ERROR("GetUtcTime() err:ntp-->create socket error!");
return -1;
}

    /* 主机名转IP地址 */
    if (m_domain[0] == '\0') strcpy(m_domain, "220.130.158.71");
    struct hostent *h = NULL;
    if((h=gethostbyname(m_domain))==NULL)
    {
        h=gethostbyname(m_domain);
        if (h == NULL)
        {
            DEBUG_ERROR("gethostbyname return NULL !\n");
            return -1;
        }
    }

    memset(szIpAddr, 0x0, sizeof(szIpAddr));
    memcpy(szIpAddr, inet_ntoa(*((struct in_addr *)h->h_addr)), 16);
DEBUG_DEBUG("devmanage ntp adjust time, current create socket IP=%s(%s)\n", szIpAddr, m_domain);
addr.sin_family = AF_INET;          //IPV4协议
addr.sin_port = htons(NTPPORT);     //NTP专用的123端口
addr.sin_addr.s_addr = inet_addr(szIpAddr);   //校时服务器
bzero(&(addr.sin_zero), 8);

//wait 5s
tv.tv_sec=10;    //select等待时间为10S
tv.tv_usec=0;

FD_ZERO(&inset);
FD_SET(sockfd, &inset);

/*初始化数据请求包*/
memset(&ntppack, 0x0, sizeof(NTPPacket_S));
ntppack.li_vn_mode = 0x1b;//0|(3<<2)|(3<<5);
//获取初始时间戳T1
firsttimestamp = From00to70 + time(NULL);//-8*3600;
ntppack.oritimestamphigh = htonl(firsttimestamp);
//发送数据请求包
ret = sendto(sockfd, &ntppack, sizeof(ntppack), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr));
if(ret < 0)
{
perror("sendto");
close(sockfd);
return -1;
}
DEBUG_DEBUG("send socket ok");
//select巡视
if(select(sockfd + 1, &inset, NULL, NULL, &tv)<0)
{
perror("select error!");
close(sockfd);
return -1;
}
else
{
DEBUG_DEBUG("select OK");
if(FD_ISSET(sockfd, &inset))
{
// printf("OK\n");
ret = recv(sockfd, &newpack, sizeof(newpack), 0);
DEBUG_DEBUG("ntp -->recv ret = %d", ret);
if(ret < 0)        //?????????newpack?С?
{
perror("recv error!");
close(sockfd);
return -1;
}
DEBUG_DEBUG("revv socket ok");
//到达客户机时间戳T4
finaltimestamp = time(NULL) + From00to70;//-8*3600;

//将网络上传送的大端数据改为小端形式。
newpack.root_delay= ntohl(newpack.root_delay);
newpack.root_dispersion = ntohl(newpack.root_dispersion);
newpack.reftimestamphigh = ntohl(newpack.reftimestamphigh);
newpack.reftimestamplow = ntohl(newpack.reftimestamplow);
newpack.oritimestamphigh = ntohl(newpack.oritimestamphigh);
newpack.oritimestamplow = ntohl(newpack.oritimestamplow);
newpack.recvtimestamphigh = ntohl(newpack.recvtimestamphigh);
newpack.recvtimestamplow = ntohl(newpack.recvtimestamplow);
newpack.trantimestamphigh = ntohl(newpack.trantimestamphigh);
newpack.trantimestamplow = ntohl(newpack.trantimestamplow);

//求出客户机跟服务器的时间差=((T2-T1)+(T3-T4))/2
diftime = ((newpack.recvtimestamphigh - firsttimestamp) + (newpack.trantimestamphigh - finaltimestamp)) >> 1;
//求出延时
delaytime = ((newpack.recvtimestamphigh - firsttimestamp) - (newpack.trantimestamphigh - finaltimestamp)) >> 1;
//diftime=(5-9)>>1;

//求出真正时间的时间戳
tv1.tv_sec = time(NULL) + diftime + delaytime;
tv1.tv_usec=0;
#if 0
printf("\n\ndebug information ...\n");
printf("time(NULL) is %ld\n", time(NULL));
printf("different time is %lld\n", diftime);
printf("delaytime is %lld\n", delaytime);
printf("time(NULL)+diftime+delaytime = %lld\n", time(NULL) + diftime + delaytime);
printf("tv1.tv_sec is %ld\n\n", tv1.tv_sec);
//NTP_PrintfTime(firsttimestamp - From00to70);
//NTP_PrintfTime(newpack.recvtimestamphigh - From00to70);
//NTP_PrintfTime(newpack.trantimestamphigh - From00to70);
//NTP_PrintfTime(finaltimestamp - From00to70);
//NTP_PrintfTime(tv1.tv_sec);
#endif
m_ntpTime = tv1.tv_sec;
DEBUG_DEBUG("------------------------ntp=%d\n", m_ntpTime);
}
else
{
DEBUG_ERROR("GetUtcTime() err :ntp----->err\n");
close(sockfd);
return -1;
}
}

close(sockfd);
DEBUG_DEBUG("ntp-->ok!!");
return 0;
}

void CNtpManager::GetUpdateTime(void)
{
#if 0
if(s_ntpObj.ntp.synchronize >= 100)
m_synchronize= 100 * 3600;
else if(s_ntpObj.ntp.synchronize  <= 1)
m_synchronize = 3600;
else
m_synchronize = s_ntpObj.ntp.synchronize * 3600;
#endif
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux NTP协议
NTP协议实现
ntp编程中的时间函数(收集中
纯C语言实现的一个简单的web服务器
3个学习Socket编程的简单例子:TCP Server/Client, Select
unix domain socket示例一(SOCK
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服