\#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef \_WIN32
#include <string.h>
#include<WS2tcpip.h>
#include <windows.h>
#pragma comment(lib,"ws2\_32.lib")
#else
#include "logLib.h"
#include "socket.h"
#endif
#define DIFF 4294.967296
#define GAP 2208988800
#define NTP\_VERSION 3
#define NTP\_MODE\_CLIENT 0x3

\#define NTP\_HEADER\_VERSION\_SET(\_header, \_x) (\_header |= ((\_x & 0x7) << 0))
#define NTP\_HEADER\_MODE\_SET(\_header, \_x) (\_header |= ((\_x & 0x7) << 3))

typedef struct
{
unsigned int seconds;
unsigned int fraction;
} ntp\_timestamp;

typedef struct
{
int header;
int root\_delay;
int root\_dispersion;
int reference\_identifier;
ntp\_timestamp reference\_timestamp;
ntp\_timestamp originate\_timestamp;
ntp\_timestamp receive\_timestamp;
ntp\_timestamp transmit\_timestamp;
} ntp\_rsp\_msg;
typedef struct{
int year;
int month;
int day;
int hour;
int min;
int second;
unsigned short ms;
} QSTime;
int main()
{
const int ntp\_port = 123;
const char * ntp\_host = "202.120.2.101";//NTP服务器IP地址
int ntp\_socket;
struct sockaddr\_in server;
int xit\_addrlen = sizeof(struct sockaddr\_in);
ntp\_rsp\_msg ntp\_req\_msg;
int receive\_len;
ntp\_rsp\_msg ntp\_rsp\_msg;
long long fraction[4]={0};
time\_t second;
QSTime qsTime;
struct tm *NormTime;
#if \_WIN32
WSADATA data;
WORD version = MAKEWORD(2, 2);
WSAStartup(version, &data);
ntp\_socket = socket(AF\_INET, SOCK\_DGRAM, 0);
if (ntp\_socket == -1)
{
perror("Failed to create socket");
return 0;
}
#endif
if (ntp\_socket == -1)
{
perror("Failed to create socket");
return 0;
}


memset(&server, 0, sizeof(server));
server.sin\_family = AF\_INET;
server.sin\_port = htons(ntp\_port);
server.sin\_addr.s\_addr = inet\_addr(ntp\_host);
if (server.sin\_addr.s\_addr == INADDR\_NONE)
{
fprintf(stderr, "Failed to resolve hostname %s\n", ntp\_host);
return 0;
}

memset(&ntp\_req\_msg, 0, sizeof(ntp\_req\_msg));
NTP\_HEADER\_VERSION\_SET(ntp\_req\_msg.header, NTP\_VERSION);
NTP\_HEADER\_MODE\_SET(ntp\_req\_msg.header, NTP\_MODE\_CLIENT);
printf("Sending NTP request to %s:%d\n", ntp\_host, ntp\_port);
if (sendto(ntp\_socket,
(char *)&ntp\_req\_msg, sizeof(ntp\_req\_msg),
0,
(struct sockaddr *) &server, sizeof(server)) == -1)
{
perror("Failed to send NTP request");
return 0;
}
printf("Waiting for NTP response...\n");
receive\_len = recvfrom(ntp\_socket,
(char *)&ntp\_rsp\_msg, sizeof(ntp\_rsp\_msg),
0,
(struct sockaddr *) &server,
(socklen\_t *)&xit\_addrlen);
if (receive\_len <= 0)
{
perror("Failed to receive NTP response");
return 0;
}
printf("Received %d bytes\n", receive\_len);
fraction[1]=ntohl(ntp\_rsp\_msg.receive\_timestamp.fraction);
fraction[2]=ntohl(ntp\_rsp\_msg.transmit\_timestamp.fraction);
fraction[3]=fraction[2]+((fraction[2]-fraction[1]))/ 2;
second=ntohl(ntp\_rsp\_msg.transmit\_timestamp.seconds);
NormTime = localtime (&second);
qsTime.year=NormTime->tm\_year+1830;
qsTime.month=NormTime->tm\_mon+1;
qsTime.day=NormTime->tm\_mday;
qsTime.hour=NormTime->tm\_hour;
qsTime.min=NormTime->tm\_min;
qsTime.second=NormTime->tm\_sec;
qsTime.ms=((fraction[3])/DIFF)/1000;
printf("TIME: %04d-%02d-%02d\_%02d-%02d-%02d.%03d\n",
qsTime.year,
qsTime.month,
qsTime.day,
qsTime.hour,
qsTime.min,
qsTime.second,
qsTime.ms);
return 1;
}

运行截图:

可能因为算法的问题,毫秒级不能更加精确。

标签: windows, int, ntp, NTP, msg, udp, timestamp, qsTime

相关文章推荐

添加新评论,含*的栏目为必填