windows系统下向NTP服务器请求时间代码 C语言,vs2010,windows,socket,udp

#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系统下向NTP服务器请求时间代码  C语言,vs2010,windows,socket,udp

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

上一篇:recv与recvfrom的区别及基于udp实现ntp服务


下一篇:NTP服务器配置