📄 talker.c,v
字号:
head 1.1;access;symbols;locks; strict;comment @ * @;1.1date 2002.07.02.19.30.29; author rbraud; state Exp;branches;next ;desc@@1.1log@Initial revision@text@/* talker.c -- a datagram "client" demo*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/time.h>#include <unistd.h>#include <netinet/in.h>#include <netdb.h>#include <sys/socket.h>#include <sys/wait.h>#include <arpa/inet.h>#define MAXBUF 1024*1024#define MINIMUM_WAIT_TIME 150000#define MAXIMUM_WAIT_TIME 700000int confidence(long *, int, float, float *, float *);double estimate_rtt(struct sockaddr_in their_addr) { // host is IP addr. int sockfd, i=0; // struct sockaddr_in my_addr; /* my address information */ // struct sockaddr_in their_addr,their_addr1; /* connector's address information */ // struct hostent *he; struct timeval start_time, end_time, tv; long total_time_in_musecs = 0; int cnt = 0, numbytes, selectRet; long confArr[100] = { 0 }; float avg, delta; char message[]="hello"; char buf[MAXBUF]; fd_set rset; // int MYPORT; /* if (argc != 3) { fprintf(stderr,"usage: talker hostname portnumber\n"); exit(1); } */ avg = 0; FD_ZERO(&rset); // MYPORT=portnumber; gettimeofday(&start_time,NULL); // if ((he=gethostbyname(host)) == NULL) { /* get the host info */ // perror("gethostbyname"); // exit(1); // } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } FD_SET(sockfd,&rset); // their_addr.sin_family = AF_INET; /* host byte order */ // their_addr.sin_port = htons(MYPORT); /* short, network byte order */ // their_addr.sin_addr = *((struct in_addr *)he->h_addr); // bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ // printf ("familty was %d, should be %d\n", their_addr.sin_family, AF_INET); their_addr.sin_family = AF_INET; /* host byte order */ // printf ("sending to inet: %s", inet_ntoa((struct in_addr )(their_addr.sin_addr))); while ((i++<10) && (total_time_in_musecs < MAXIMUM_WAIT_TIME)) { sprintf(message,"%d",i); gettimeofday(&start_time,NULL); if ((numbytes=sendto(sockfd,message,strlen(message),0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { perror("sending problem"); printf( "Continuing.. "); continue; } if(FD_ISSET(sockfd,&rset)) { tv.tv_sec = 0; tv.tv_usec = MINIMUM_WAIT_TIME; do { sprintf(buf,"%d",i); //printf("ENTER - select\n"); fflush(0); // do { if((selectRet = select(sockfd+1,&rset,NULL,NULL,&tv)) > 0 ) { numbytes = recvfrom(sockfd,buf,MAXBUF,0,NULL,NULL); if (numbytes<0) { perror("Problem in recvfrom"); continue; } //printf("DONE - recvfrom\n"); fflush(0); if(atoi(buf) == i) { gettimeofday(&end_time,NULL); confArr[cnt++] = 1000000L*(end_time.tv_sec-start_time.tv_sec)+ (end_time.tv_usec -start_time.tv_usec); } } gettimeofday(&end_time,NULL); total_time_in_musecs += 1000000L*(end_time.tv_sec-start_time.tv_sec)+ (end_time.tv_usec -start_time.tv_usec); //printf("CHECK2 total in usec = %ld SELECT_RET %d ERR_NO %d\n", // total_time_in_musecs, selectRet, errno); fflush(0); //} while ((selectRet == -1 && errno == EINTR) && // total_time_in_musecs < MAXIMUM_WAIT_TIME); } while (atoi(buf)<i && total_time_in_musecs < MAXIMUM_WAIT_TIME); FD_SET(sockfd,&rset); } if(cnt >= 3) { confidence(confArr, cnt, 0.95, &avg, &delta); if(delta <= 0.1 * avg) break; } }//end of while close(sockfd); // printf("CNT = %d AVG = %.0f DELTA = %.0f\n NUMPACKETS LOST = %d", cnt, avg, delta, i - cnt); if(avg == 0) return 200000; return avg;}double estimate_rtt_wrapper(struct sockaddr_in their_addr) { double dist = estimate_rtt(their_addr); if (dist < 1000.0) return 500.0; if (dist < 5000.0) return 3000.0; if (dist < 10000.0) return 7500.0; if (dist < 20000.0) return 15000.0; if (dist < 40000.0) return 30000.0; if (dist < 80000.0) return 60000.0; if (dist < 160000.0) return 120000.0; return 200000.0;}/*int main(int argc, char ** argv) { struct hostent *he; struct sockaddr_in their_addr; if (argc != 3) { fprintf(stderr,"usage: %s hostname portnumber\n", argv[0]); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) { perror("gethostbyname"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(atoi(argv[2])); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); printf(" %8.4f is the delay \n", estimate_rtt(their_addr)); return 0; }*/@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -