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;
}
Logo

更多推荐