所以我创建了一个sntp客户端,我试图在一个可信的ntp服务器上测试它。我的客户正在发送一个请求,然后服务器应该用它自己的时间戳进行响应,然后客户端计算往返延迟等。但是,当我的客户端发送请求时,它没有任何回应,我真的不知道为什么它与sntp一起工作我创建的服务器。我看过线鲨看看发生了什么,客户端的请求看起来很好,服务器的响应也是如此,所以我不确定发生了什么事情!继承人的客户代码:为什么ntp服务器不会响应我的客户端请求?
/* talker.c
* run the program and enter hostname on command line
* e.g ./talker localhost
* Author: Adam Gloyne (14012913)
* Purpose: Sends request to server and calculates received time stamps
* Date Edited: 01/12/15 - added comments
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h> //for time calcs
/* server port the client connects to */
#define PORT 123
#define MAXBUFLEN 100
int main(int argc, char * argv[]) {
/**Variables **********************************/
/* for gethostbyname() */
struct hostent *he;
struct sockaddr_in their_addr;
int sockfd, numbytes, addr_len;
char buf[ MAXBUFLEN];
//variables for time processing ***
struct tm * timer;
char message [64];
char format [64];
long long t1; //originate time stamp
long long t2; //transmit time stamp
long long t3; //receive time stamp
long long t4; //final time stamp
long long roundDelay;
long long offset;
time_t secs; //microseconds for calcs
//***********************************
/***********************************************/
/* server address info */
if(argc != 2) {
fprintf(stderr, "usage: talker hostname message\n");
exit(1);
}
/* resolve server host name or IP address */
if((he = gethostbyname(argv[1])) == NULL) {
perror("Talker gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Talker socket");
exit(1);
}
memset(&their_addr,0, sizeof(their_addr));
/* zero struct */
their_addr.sin_family = AF_INET;
/* host byte order .. */
their_addr.sin_port = htons(PORT);
/* .. short, netwk byte order */
their_addr.sin_addr = *((struct in_addr *)he -> h_addr);
//get system time *********************************************
time (&secs);
timer = localtime (&secs);
//format time and place into buffer ready to send to server
strftime (format,64,"Client Originate Timestamp %y-%m-%d %H:%M:%S.\n",timer);
snprintf (message,64,format,(unsigned long)secs); //casting secs
printf (message);
t1 = ntohl((time_t)buf[10]); //get the originate time stamp, needed for final calculation at the end
t1 -= 2208988800U; //converting from ntp to unix time, difference in seconds from ntp time 0h 1st Jan 1900 and unix time 0h 1st Jan 1970 (from RFC)
//********************************************************************
//send the message and check for errors
if((numbytes = sendto(sockfd, message, strlen(message) + 1, 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("Talker sendto");
exit(1);
}
printf("Sent %d bytes to %s\n", numbytes,
inet_ntoa(their_addr.sin_addr));
//receive response from server and check for errors
//will be receiving one time stamp from the server but will be the value for both receive and transmit
addr_len = sizeof (struct sockaddr);
if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN - 1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1); {
perror("Talker recvfrom");
//exit(1);
}
//print out received message from server
printf("Packet contains \"%s\"\n", buf);
t3 = ntohl((time_t)buf[10]); //get the server receive time time
t3 -= 2208988800U; //convert from ntp to unix time
//assigning transmit stamp from server the same as receive (as specified in RFC)
t2 = t3;
//get new system time for final time stamp
secs = 0;
time (&secs);
timer = localtime (&secs);
strftime (format,64,"\nClient Recieve Timestamp %y-%m-%d %H:%M:%S.%%06lu %z\n",timer);
snprintf (message,64,format,(unsigned long)secs); //casting secs
printf(message);
t4 = ntohl((time_t)message[10]); //get the client receieve time
t4 -= 2208988800U; //convert to unix time
//now we have all time stamps can work out round trip delay and offset
//round trip first d = (T4 - T1) - (T3 - T2)
roundDelay = ((t4-t1) - (t3 - t2));
printf("\nHere is the round trip delay: %d microseconds", roundDelay);
//now we can calculate the system clock offset.
//t = ((T2 -T1) + (T3 -T4))/2
offset = ((t2 - t1) + (t3 - t4))/2;
printf("\nHere is the system clock offset: %d microseconds\n", offset);
//all is done here so close socket and kill client
close(sockfd);
return 0;
}
任何帮助将不胜感激。
源代码中姓名旁边的数字是什么? – dfc