linux下rto的設定及rto測試
阿新 • • 發佈:2019-01-07
rto修改命令
sudo ip route change 172.16.100.0/24 dev eth0 rto_min 5 (單位預設ms)
rto測試
1、編寫socket網路程式測試,客戶端一直向服務端傳送資料(測試客戶端的rto),在客戶端與伺服器端通訊過程中,將服務端向客戶端通訊的路由項刪掉。
2、客戶端通過wireshark抓包,即可分析rto的變化規律(指數退避)。
3、測試程式碼如下:
客戶端
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
int main(){
char buf[100];
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
struct sockaddr_in dst;
if(fd == -1){
printf("Create socket error\n");
return 0;
}
dst.sin_family = AF_INET;
dst.sin_port = htons(8000 );
dst.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(fd, (struct sockaddr*)&dst, sizeof(struct sockaddr)) == -1){
printf("Connet error\n");
return 0;
}
while(1){
if(send(fd, "hello,world!\n", 13, 0) != 13){
printf("Send error\n");
return 0;
}
sleep(1);
}
return 0;
}
伺服器端
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
int fd_all[100];
void *server(void *fd_addr){
char buf[101];
int fd = *((int *)fd_addr);
while(1){
if(recv(fd, buf, 100, 0) <= 0){
printf("no data\n");
break;
}
printf("%s\n", buf);
}
close(fd);
return NULL;
}
int main(){
int fd, new_fd;
struct sockaddr_in my_addr, new_addr;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if(fd == -1){
printf("crate socket error\n");
return 0;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(8000);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) < 0){
printf("bind error\n");
return 0;
}
listen(fd, 10);
while(1){
int size = sizeof(struct sockaddr);
new_fd = accept(fd, (struct sockaddr*)&new_addr, &size);
if(new_fd == -1){
printf("accept error\n");
return 0;
}else{
pthread_t ntid;
if(pthread_create(&ntid, NULL, server, &new_fd) < 0){
printf("create thread error\n");
return 0;
}
}
}
return 0;
}