所以我創建了一個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