C 如何编写NTP客户端?

C 如何编写NTP客户端?,c,ntp,C,Ntp,我正在寻找用C编写的NTP客户端代码示例。我发现: 修复了代码中的一些问题,并且它能够编译。但在“发送数据…”之后,它并不是什么都没有。我不知道如何解决这个问题。代码或服务器中的问题在哪里?提前谢谢 /* * This code will query a ntp server for the local time and display * it. it is intended to show how to use a NTP server as a time * source for

我正在寻找用C编写的NTP客户端代码示例。我发现: 修复了代码中的一些问题,并且它能够编译。但在“发送数据…”之后,它并不是什么都没有。我不知道如何解决这个问题。代码或服务器中的问题在哪里?提前谢谢

/*
 * This code will query a ntp server for the local time and display
 * it.  it is intended to show how to use a NTP server as a time
 * source for a simple network connected device.
 * This is the C version.  The orignal was in Perl
 *
 * For better clock management see the offical NTP info at:
 * http://www.eecis.udel.edu/~ntp/
 *
 * written by Tim Hogard (thogard@abnormal.com)
 * Thu Sep 26 13:35:41 EAST 2002
 * Converted to C Fri Feb 21 21:42:49 EAST 2003
 * this code is in the public domain.
 * it can be found here http://www.abnormal.com/~thogard/ntp/
 *
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <string.h>

void ntpdate();

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

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

//use Socket;
//
//#we use the system call to open a UDP socket
//socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
proto=getprotobyname("udp");
s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
if(s) {
    perror("asd");
    printf("socket=%d\n",s);
}
//
//#convert hostname to ipaddress if needed
//$ipaddr   = inet_aton($HOSTNAME);
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
//argv[1] );
//i   = inet_aton(hostname,&server_addr.sin_addr);
server_addr.sin_port=htons(portno);
//printf("ipaddr (in hex): %x\n",server_addr.sin_addr);

/*
 * 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));

// get the data back
i=recv(s,buf,sizeof(buf),0);
printf("recvfr: %d\n",i);
//perror("recvfr:");

//We get 12 long words back in Network order
/*
for(i=0;i<12;i++)
    printf("%d\t%-8x\n",i,ntohl(buf[i]));
*/

/*
 * 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
//printf("tmit=%d\n",tmit);

/*
 * 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; 
//printf("tmit=%d\n",tmit);
/* 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("Time: %s",ctime(&tmit));
i=time(0);
//printf("%d-%d=%d\n",i,tmit,i-tmit);
printf("System time is %d seconds off\n",i-tmit);
}
/*
*此代码将查询ntp服务器的本地时间和显示
*它。它旨在展示如何使用NTP服务器作为时间节点
*简单网络连接设备的源代码。
*这是C版本。原始文件是用Perl编写的
*
*有关更好的时钟管理,请参阅以下官方NTP信息:
* http://www.eecis.udel.edu/~ntp/
*
*蒂姆·霍格德写(thogard@abnormal.com)
*2002年9月26日星期四13:35:41东部
*已转换为2003年2月21日星期五21:42:49东部
*此代码位于公共域中。
*可以在这里找到http://www.abnormal.com/~thogard/ntp/
*
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void ntpdate();
int main(){
ntpdate();
返回0;
}
void ntpdate(){
char*hostname=“tick.usno.navy.mil”;
int-portno=123;//NTP是端口123
int maxlen=1024;//检查缓冲区
int i;//杂项变量i
unsigned char msg[48]={010,0,0,0,0,0,0};//我们发送的数据包
无符号长buf[maxlen];//返回的缓冲区
//_addr ipaddr;中的结构
结构-原型*proto//
服务器地址中的结构sockaddr\u;
int s;//套接字
int tmit;//时间——这是一种时间
//使用插座;
//
//#我们使用系统调用打开UDP套接字
//套接字(socket、PF_INET、SOCK_DGRAM、getprotobyname(“udp”))或die“socket:$!”;
proto=getprotobyname(“udp”);
s=插座(PF INET、SOCK DGRAM、proto->p_proto);
若有(s){
佩罗尔(“asd”);
printf(“套接字=%d\n”,s);
}
//
//#如果需要,将主机名转换为IP地址
//$ipaddr=inet\u aton($HOSTNAME);
memset(&server_addr,0,sizeof(server_addr));
服务器地址sin\u family=AF\u INET;
服务器\u addr.sin\u addr.s\u addr=inet\u addr(主机名);
//argv[1]);
//i=inet\u aton(主机名和服务器地址sin\u addr);
服务器地址sin\u端口=htons(端口号);
//printf(“ipaddr(十六进制):%x\n”,服务器地址sin\u addr);
/*
*构建一条消息。我们的消息除了在
*协议版本字段
*二进制的msg[]是00 001 000 00000000
*它的总长度应为48字节
*/
//发送数据
printf(“发送数据..\n”);
i=sendto(s,msg,sizeof(msg),0,(struct sockaddr*)和server_addr,sizeof(server_addr));
//取回数据
i=recv(s,buf,sizeof(buf),0);
printf(“recvfr:%d\n”,i);
//perror(“recvfr:”);
//我们按网络顺序回复了12个长单词
/*

对于(i=0;i不确定sendto()是否阻塞

要测试这一点,您可能需要添加

printf("receiving data..\n");
在调用
recv()
之前


另外,
recv()
调用可能会阻塞

它等待
sizeof(buf)
字节,即
1024*sizeof(long)

从源注释中,我们知道有12个长单词,因此您可以考虑通过调用“<代码> ReV())<代码> > > < >:< /P>

i = recv(s, buf, 12*sizeof(buf[0]), 0);

尽管如此,我还是强烈建议在系统调用中添加错误检查,例如
sendo()
recv()
。要对这两个调用执行此操作,请测试它们返回的值是否为
-1
,如果是,请执行一些错误处理


对于其他系统调用,其他值可能表示错误。有关详细信息,请参阅
man

以下是我修改并编译和执行的代码。
它是在Server2003上测试的,而不是在2008上测试的。这是与您提供的Unix相同的windows代码

#include <stdio.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>
//#include <netdb.h>
#include <time.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <tchar.h>

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

void ntpdate();

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

void ntpdate() {
char    *hostname="64.27.26.1";//"tick.usno.navy.mil";
int portno=123;     //NTP is port 123
int maxlen=1024;        //check our buffers
long i;          // misc var i
char msg[48]={010,0,0,0,0,0,0,0,0};    // the packet we send
char  *buf = new char[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


//=====================================================================================
//THIS IS WHAT IS MISSING MAJORILY  
//=====================================================================================
        //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;
//
//#we use the system call to open a UDP socket
//socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "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);
}
//
//#convert hostname to ipaddress if needed
//$ipaddr   = inet_aton($HOSTNAME);
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
//argv[1] );
//i   = inet_aton(hostname,&server_addr.sin_addr);
server_addr.sin_port=htons(portno);
//printf("ipaddr (in hex): %x\n",server_addr.sin_addr);

/*
 * 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));

int iResult = -1;
    // Receive until the peer closes the connection
    //do {

        iResult = recv(s, buf, 1024, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed: %d\n", WSAGetLastError());

    //} while( iResult > 0 );

/*
 * 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
//printf("tmit=%d\n",tmit);

/*
 * 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; 
//printf("tmit=%d\n",tmit);
/* 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("Time: %s",ctime(&tmit));
i=time(0);
//printf("%d-%d=%d\n",i,tmit,i-tmit);
printf("System time is %d seconds off\n",i-tmit);
}
#包括
//#包括
//#包括
//#包括
//#包括
//#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“Ws2_32.lib”)
void ntpdate();
int main(){
ntpdate();
返回0;
}
void ntpdate(){
char*hostname=“64.27.26.1”;/“tick.usno.navy.mil”;
int-portno=123;//NTP是端口123
int maxlen=1024;//检查缓冲区
长i;//杂项变量i
char msg[48]={010,0,0,0,0,0,0};//我们发送的数据包
char*buf=new char[1024];//我们得到的缓冲区
//_addr ipaddr;中的结构
结构-原型*proto//
服务器地址中的结构sockaddr\u;
套接字s;//套接字
时间;//时间——这是一种时间
//=====================================================================================
//这是大部分人所缺少的
//=====================================================================================
//初始化winsock堆栈
WSADATA WSADATA;
字节wsMajorVersion=1;
字节wsMinorVersion=1;
WORD wVersionRequested=MAKEWORD(wsMinorVersion,wsMajorVersion);
如果(WSAStartup(wVersionRequested,&wsaData)!=0)
{
_tprintf(_T(“加载winsock堆栈失败”);
WSACleanup();
返回;
}
如果(LOBYTE(wsaData.wVersion)!=wsMajorVersion | | HIBYTE(wsaData.wVersion)!=wsMinorVersion)
{
_tprintf(_T(“Winsock堆栈不支持此程序所需的版本”);
WSACleanup();
返回;
}
//=====================================================================================
//使用插座;
//
//#我们使用系统调用打开UDP套接字
//套接字(socket、PF_INET、SOCK_DGRAM、getprotobyname(“udp”))或die“socket:$!”;
proto=getprotobyname(“udp”);
int err=GetLastError();
s=插座(PF INET、SOCK DGRAM、proto->p_proto);
若有(s){
佩罗尔(“asd”);
printf(“套接字=%d\n”,s);
}
//
//#如果需要,将主机名转换为IP地址
//$ipaddr=inet\u aton($HOSTNAME);
memset(&server_addr,0,sizeof(server_addr));
服务器地址sin\u family=AF\u INET;
服务器\u addr.sin\u addr.s\u addr=inet\u addr(主机名);
//argv[1]);
//i=inet\u aton(主机名和服务器地址sin\u addr);
服务器地址sin\u端口=htons(端口号);
//printf(“ipaddr(十六进制):%x\n”,服务器地址sin\u addr);
/*
*创建一条消息。我们的消息都是ze
 /* This code will query a ntp server for the local time and display

 * it.  it is intended to show how to use a NTP server as a time
 * source for a simple network connected device.
 * This is the C version.  The orignal was in Perl
 *
 * For better clock management see the offical NTP info at:
 * http://www.eecis.udel.edu/~ntp/
 *
 * written by Tim Hogard (thogard@abnormal.com)
 * Thu Sep 26 13:35:41 EAST 2002
 * Converted to C Fri Feb 21 21:42:49 EAST 2003
 * this code is in the public domain.
 * it can be found here http://www.abnormal.com/~thogard/ntp/
 *
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <string.h>

void ntpdate();

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

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

//use Socket;
//
//#we use the system call to open a UDP socket
//socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
proto=getprotobyname("udp");
s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
perror("socket");
//
//#convert hostname to ipaddress if needed
//$ipaddr   = inet_aton($HOSTNAME);
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
//argv[1] );
//i   = inet_aton(hostname,&server_addr.sin_addr);
server_addr.sin_port=htons(portno);
//printf("ipaddr (in hex): %x\n",server_addr.sin_addr);

/*
 * 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:");

//We get 12 long words back in Network order
/*
for(i=0;i<12;i++)
    printf("%d\t%-8x\n",i,ntohl(buf[i]));
*/

/*
 * 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
//printf("tmit=%d\n",tmit);

/*
 * 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; 
//printf("tmit=%d\n",tmit);
/* 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("Time: %s",ctime(&tmit));
i=time(0);
//printf("%d-%d=%d\n",i,tmit,i-tmit);
printf("System time is %d seconds off\n",i-tmit);
}
$ ./sntp_client 
socket: Success
sending data..
sendto: Success
recvfr:: Success
Time: Sun Sep 26 16:00:32 4213
System time is -702316565 seconds off
 /* This code will query a ntp server for the local time and display
 * it.  it is intended to show how to use a NTP server as a time
 * source for a simple network connected device.
 * This is the C version.  The orignal was in Perl
 *
 * For better clock management see the offical NTP info at:
 * http://www.eecis.udel.edu/~ntp/
 *
 * written by Tim Hogard (thogard@abnormal.com)
 * Thu Sep 26 13:35:41 EAST 2002
 * Converted to C Fri Feb 21 21:42:49 EAST 2003
 * this code is in the public domain.
 * it can be found here http://www.abnormal.com/~thogard/ntp/
 *
 * ported to android 4.3 on mar/nov/5 - 2013 by abel.
 * the same day ported to a library for agpsd layer.
 */
/*
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
#include <utils/SystemClock.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <sys/time.h>
#define NTP_MODE_CLIENT 3
#define NTP_VERSION 3
#define TRANSMIT_TIME_OFFSET 40
#define REFERENCE_TIME_OFFSET 16
#define ORIGINATE_TIME_OFFSET 24
#define RECEIVE_TIME_OFFSET 32
#define OFFSET_1900_TO_1970 ((uint64_t)((365 * 70) + 17) * 24 * 60 * 60)
*/
void ntpdate(uint64_t *cachedTime, uint64_t *cachedTimeRef, uint64_t *cacheCertainty);
/*
int main() {
    uint64_t cachedTime, cachedTimeRef, cacheCertainty;
    ntpdate(&cachedTime, &cachedTimeRef, &cacheCertainty);
    printf ("%lld\n, %lld\n, %lld\n", cachedTime, cachedTimeRef, cacheCertainty);
    return 0;
}
*/
double random2 () {
    srandom(time(NULL));
    return random();
}

uint64_t currentTimeMillis(/*long int seconds, long int miliseconds*/) {
    struct timeval te; 
    gettimeofday(&te, NULL); // get current time
    uint64_t millis = (uint64_t) te.tv_sec * 1000 + floor(te.tv_usec / 1000); // caculate milliseconds
//  printf ("millis: %llu\n", millis);
    return millis;
}

uint32_t read32(char* buffer, int offset) {
    char b0 = buffer[offset];
    char b1 = buffer[offset+1];
    char b2 = buffer[offset+2];
    char b3 = buffer[offset+3];

    // convert signed bytes to unsigned values
    uint32_t i0 = ((b0 & 0x80) == 0x80 ? (b0 & 0x7F) + 0x80 : b0);
    uint32_t i1 = ((b1 & 0x80) == 0x80 ? (b1 & 0x7F) + 0x80 : b1);
    uint32_t i2 = ((b2 & 0x80) == 0x80 ? (b2 & 0x7F) + 0x80 : b2);
    uint32_t i3 = ((b3 & 0x80) == 0x80 ? (b3 & 0x7F) + 0x80 : b3);

    uint32_t v = (i0 << 24) + (i1 << 16) + (i2 << 8) + i3;
    return v;
}

uint64_t readTimeStamp(char *buffer, int offset) {
    uint32_t seconds  = read32(buffer, offset);
    uint32_t fraction = read32(buffer, offset + 4);
    uint64_t v = ((int64_t)seconds - OFFSET_1900_TO_1970) * 1000 + (int64_t) fraction * 1000 / (int64_t) 0x100000000;
//  printf ("%llu\n", v);
    return v;
}


void writeTimeStamp(char* buffer, int offset, long long int time) {
    uint64_t seconds = floor (time / 1000);
    uint64_t milliseconds = time - (uint64_t) seconds * 1000;
    seconds += OFFSET_1900_TO_1970;

    // write seconds in big endian format
    buffer[offset++] = (char)(seconds >> 24);
    buffer[offset++] = (char)(seconds >> 16);
    buffer[offset++] = (char)(seconds >> 8);
    buffer[offset++] = (char)(seconds >> 0);

    uint64_t fraction = round ((uint64_t)milliseconds * 0x100000000 / 1000);
    // write fraction in big endian format
    buffer[offset++] = (char)(fraction >> 24);
    buffer[offset++] = (char)(fraction >> 16);
    buffer[offset++] = (char)(fraction >> 8);
    // low order bits should be random data
    buffer[offset++] = (char)(random2() * 255.0);
}

void ntpdate(uint64_t *cachedTime, uint64_t *cachedTimeRef, uint64_t *cacheCertainty) {
    char hostname[]="81.184.154.182";
    int portno=123;     //NTP is port 123
    int maxlen=48;        //check our buffers
    int i;          // misc var i
    uint64_t requestTime = currentTimeMillis();
    uint64_t requestTicks = android::elapsedRealtime();
    char msg[48];
    memset (msg,0,sizeof(msg));
    msg[0]= NTP_MODE_CLIENT | (NTP_VERSION << 3);
    writeTimeStamp(msg, TRANSMIT_TIME_OFFSET, requestTime);
    char  buf[maxlen]; // the buffer we get back
    struct sockaddr_in server_addr;
    int s;  // socket
    time_t tmit;   // the time -- This is a time_t sort of

    s=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    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);

    i=sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
    struct sockaddr saddr;
    socklen_t saddr_l = sizeof (saddr);
    i=recvfrom(s,buf,sizeof(buf),0,&saddr,&saddr_l);

    uint64_t responseTicks = android::elapsedRealtime();
    uint64_t responseTime = requestTime + (responseTicks - requestTicks);
    uint64_t originateTime = readTimeStamp(buf, ORIGINATE_TIME_OFFSET);
    uint64_t receiveTime = readTimeStamp(buf, RECEIVE_TIME_OFFSET);
    uint64_t transmitTime = readTimeStamp(buf, TRANSMIT_TIME_OFFSET);
    uint64_t roundTripTime = responseTicks - requestTicks - (transmitTime - receiveTime);
    int32_t clockOffset = ((receiveTime - originateTime) + (transmitTime - responseTime))/2;
    //printf ("%lld + %lld = %ld %ld\n", (receiveTime - originateTime), (transmitTime - responseTime), (receiveTime - originateTime + transmitTime - responseTime)/2, clockOffset);
    uint64_t mNtpTime = responseTime + clockOffset;
    uint64_t mNtpTimeReference = responseTicks;
    uint64_t mRoundTripTime = roundTripTime;
    uint64_t mCachedNtpTime = mNtpTime;
    uint64_t mCachedNtpElapsedRealtime = mNtpTimeReference;
    uint64_t mCachedNtpCertainty = mRoundTripTime / 2;
    *cachedTime     = mCachedNtpTime;
    *cachedTimeRef  = mNtpTimeReference;
    *cacheCertainty = mCachedNtpCertainty;

//  uint64_t now = mNtpTime + android::elapsedRealtime() - mNtpTimeReference;
/*
    printf ("%lld\n", now);
    i=currentTimeMillis();
    printf("System time is %lu miliseconds off\n",i-now);
*/
}