NTP Time获取NULL值

时间:2014-01-21 19:06:44

标签: c++ windows visual-c++ time ntp

我想使用NTP获取网络时间,搜索并使用下面的示例示例。

Is there any C/C++ library to connect with a remote NTP server?

似乎工作问题是在将NTP时间规范化为Windows时间之后它会打印出NULL值。我使用的代码如下。如果我打印时间为

printf("tmit=%s\n",ctime(&tmit));

之前

 tmit-= 2208988800U; 

行然后它给了我1969年12月31日,但之后结果是NULL。什么是没有时间的原因?

提前致谢

#include "stdafx.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <tchar.h>

#include <conio.h>
#include <iostream>


#pragma comment(lib, "Ws2_32.lib")

void ntpdate();

int main() {
ntpdate();
return 0;
}

void ntpdate() {

char    *hostname=(char *)"200.20.186.76";
int portno=123;     //NTP is port 123
int maxlen=1024;        //check our buffers
int i;          // misc var i
char msg[48]={010,0,0,0,0,0,0,0,0};    // the packet we send
char  buf[1024]; // the buffer we get back
//struct in_addr ipaddr;        //  
struct protoent *proto;     //
struct sockaddr_in server_addr;
SOCKET s;  // socket
time_t tmit;   // the time -- This is a time_t sort of

//Initialise the winsock stack
WSADATA wsaData;
BYTE wsMajorVersion = 1;
BYTE wsMinorVersion = 1;
WORD wVersionRequested = MAKEWORD(wsMinorVersion, wsMajorVersion);   
if (WSAStartup(wVersionRequested, &wsaData) != 0) 
{
    _tprintf(_T("Failed to load winsock stack\n"));
    WSACleanup();
    return;
}
if (LOBYTE(wsaData.wVersion) != wsMajorVersion || HIBYTE(wsaData.wVersion) != wsMinorVersion)
{
    _tprintf(_T("Winsock stack does not support version which this program requires\n"));
    WSACleanup();
    return;
}

//use Socket;
proto=getprotobyname("udp");
int err = GetLastError();
s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
if(s) {
    perror("asd");
    printf("socket=%d\n",s);
}

memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
server_addr.sin_port=htons(portno);

/*
* build a message.  Our message is all zeros except for a one in the
* protocol version field
* msg[] in binary is 00 001 000 00000000 
* it should be a total of 48 bytes long
*/

// send the data

printf("sending data..\n");
i=sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
perror("sendto");

// get the data back

struct sockaddr saddr;
socklen_t saddr_l = sizeof (saddr);
i=recvfrom(s,buf,48,0,&saddr,&saddr_l);
perror("recvfr:");


* The high word of transmit time is the 10th word we get back
* tmit is the time in seconds not accounting for network delays which
* should be way less than a second if this is a local NTP server
*/

tmit=ntohl((time_t)buf[10]);    //# get transmit time

/*
* Convert time to unix standard time NTP is number of seconds since 0000
* UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January
* 1970 There has been a trend to add a 2 leap seconds every 3 years.
* Leap seconds are only an issue the last second of the month in June and
* December if you don't try to set the clock then it can be ignored but
* this is importaint to people who coordinate times with GPS clock sources.
*/

tmit-= 2208988800U; 

/* use unix library function to show me the local time (it takes care
* of timezone issues for both north and south of the equator and places
* that do Summer time/ Daylight savings time.
*/


//#compare to system time
printf("\nTime: %s",ctime(&tmit));

i=time(0);

printf("\nSystem time is %d seconds off\n",(i-tmit));
getch();
 }

0 个答案:

没有答案