带宽测试初探(UDP发包方式)
生活随笔
收集整理的這篇文章主要介紹了
带宽测试初探(UDP发包方式)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
突然想起一個畢設(shè)的遺留問題----對實際帶寬估計。
當(dāng)時想到一個簡單的方式對網(wǎng)絡(luò)情況不復(fù)雜的帶寬進行測試,可行性還需進一步確認。論壇里無人答復(fù),默淚點擊打開鏈接
大致原理:在鏈路上沒有其他數(shù)據(jù)流量的情況下,用高于帶寬的吞吐量發(fā)送UDP包,然后在收端統(tǒng)計收包或丟包數(shù)量,對實際帶寬進行估計。
程序很簡單,定義一個包含序列號的UDP包,鏈路兩端一收一發(fā),最后統(tǒng)計收發(fā)包數(shù)量即可。
PS:當(dāng)時還統(tǒng)計了雙方時間戳,用于測量兩端時延,沒有去掉該部分直接附上來了
包定義:
#ifndef STRUCT_H #define STRUCT_Hstruct udp_packet {long int sendtime; //發(fā)送時間戳unsigned long int seq; //數(shù)據(jù)包序列號 };#endi發(fā)送端: #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/socket.h> #include<sys/types.h> #include<sys/time.h> #include<string.h> #include<time.h> #include"struct.h"#define MAX_BUF_SIZE 1500 #define INTERVAL 1000 #define SENDSIZE 48 //設(shè)置包大小和發(fā)送間隔以控制流量int main(int argc, char* argv[]) {if(argc != 3){printf("Usage: ./%s IP port_number",argv[0]);exit(1);} char sendbuf[MAX_BUF_SIZE];int sockfd;struct timeval sendtimeval,send_interval;struct sockaddr_in hostaddr;struct udp_packet udp_data;hostaddr.sin_family = AF_INET;hostaddr.sin_port = htons(atoi(argv[2]));hostaddr.sin_addr.s_addr = inet_addr(argv[1]);send_interval.tv_sec = 0;send_interval.tv_usec = INTERVAL;if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0){printf("Creat Socket Failed !\n");exit(1);}elseprintf("Create Socket Success !\n");udp_data.seq = 0;while(1){int temp = select(0, NULL, NULL, NULL, &send_interval);if(temp == -1){continue;}gettimeofday(&sendtimeval,0);printf("%u %u",sendtimeval.tv_sec,sendtimeval.tv_usec);udp_data.sendtime = 1000000 * sendtimeval.tv_sec + sendtimeval.tv_usec;memset(sendbuf,0x00,MAX_BUF_SIZE);memcpy(sendbuf,&udp_data,sizeof(struct udp_packet));//!!printf("packetsize is : %d\n",sizeof(udp_data));if ((sendto(sockfd,sendbuf,SENDSIZE,0,(struct sockaddr*)&hostaddr,sizeof(hostaddr))) < 0 ){printf("Fail to send the udp packet \n");}else{printf("seq is: %ld \n",udp_data.seq);printf("local send time is: %ld\n",udp_data.sendtime);}udp_data.seq++;}close(sockfd);return 0; }接收端:
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<errno.h> #include"struct.h"#define MAX_BUF_SIZE 1500int main(int argc,char* argv[]) {if (argc != 2){printf("./%s portnumber",argv[0]);exit(1);}int sockfd;long localtime;FILE *fp;struct sockaddr_in localaddr,remoteaddr;struct timeval recvtime;char recvbuf[MAX_BUF_SIZE];struct udp_packet udp_data;memset(&localaddr,0x00,sizeof(struct sockaddr_in));localaddr.sin_family = AF_INET;localaddr.sin_port = htons(atoi(argv[1]));localaddr.sin_addr.s_addr = htonl(INADDR_ANY);//本機IPif ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0){printf("Create socket failed !\n");exit(1);}elseprintf("Create socket success !\n");if ((bind(sockfd,(struct sockaddr*)&localaddr,sizeof(localaddr))) < 0){printf("Bind port failed !\n");exit(1);}elseprintf("Bind port success !\n");if ((fp = fopen("/home/simon/delay/recvdata.txt","w+")) == NULL ){perror("fopen error");exit(1);}elseprintf("Success to create the receive file!\n");int remoteaddr_len = sizeof(struct sockaddr);while(1){if (recvfrom(sockfd,recvbuf,MAX_BUF_SIZE,0,(struct sockaddr*)&remoteaddr,&remoteaddr_len) < 0){printf("Fail to receive data!\n");}else{printf("receiving... \n");struct udp_packet* t_data = (struct udp_packet*)recvbuf;gettimeofday(&recvtime,0);localtime = 1000000*recvtime.tv_sec + recvtime.tv_usec;fprintf(fp,"%ld %ld %ld\n", t_data -> seq, localtime, t_data -> sendtime);//輸出到txt文件方便統(tǒng)計}}fclose(fp);close(sockfd);return(0); }用TP-LINK TL-WR740N 路由器分別限制帶寬 1.5M、2M實測結(jié)果如下:
如果這個測試方法可行,路由器的端口帶寬控制也就能達標(biāo)百分之七八十
總結(jié)
以上是生活随笔為你收集整理的带宽测试初探(UDP发包方式)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ssm医院管理系统
- 下一篇: 【程序员讲婚庆】婚庆公司