⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sudp_linux.c

📁 linux下的网卡测速程序
💻 C
字号:
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <errno.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/timeb.h>



#define MAXIMUM_NUMBER 10000
#define PORT 100
#define KEY 1244
#define SIZE MAXIMUM_NUMBER*40


struct w_header
{
	unsigned short seq_num;
	unsigned short hour;
	unsigned short min;
	unsigned short second;
	unsigned short ms;
	unsigned short rate;
	unsigned short tx_packet;
	unsigned short last_flag;
};

struct packet_info
{
		int rxth[MAXIMUM_NUMBER];   //发送时间
		int rxtm[MAXIMUM_NUMBER];  //发送时间
		int rxts[MAXIMUM_NUMBER];  //发送时间
		int rxtms[MAXIMUM_NUMBER]; //发送时间
		int rxlen[MAXIMUM_NUMBER]; //每次发送的包的字节个数,
		int rxseq_num[MAXIMUM_NUMBER];
		int rxrate[MAXIMUM_NUMBER];
		int rxlast_flag[MAXIMUM_NUMBER];
		int tx_packet;
		int total_bytes;
		int total_packet;
};

void stat_udp(struct packet_info *pkt_info);



int main( )
{
 
 
 	int fd;
  	char buf[10240],buffer[20];
 	int sockfd,i;
  	long n;
 	struct sockaddr_in addr;
 	struct w_header *packet_header;
 	int addr_len=sizeof(struct sockaddr_in);
 	
 	sockfd=socket(AF_INET,SOCK_DGRAM,0);
  	if(sockfd<0){
    		fprintf(stderr,"socket error\n");
    		exit(1);
  	}

	bzero(&addr,sizeof(addr));
  	addr.sin_family=AF_INET;
  	addr.sin_addr.s_addr=htonl(INADDR_ANY);
  	addr.sin_port=htons(PORT);
  	
  	if(bind(sockfd,(struct sockaddr *)&addr,sizeof(addr))<0){
    		fprintf(stderr,"bind error.\n");
    		exit(1);
  	}
 
      	
   	int shmid;
	char *shmaddr;
	struct shmid_ds shbuf;
	
   	shmid = shmget(KEY,SIZE,IPC_CREAT |0600);
   	bzero(shmat(shmid,NULL,0),SIZE);
   	
   	if(fork()==0)
   	{
   	   	struct packet_info *pkt_info;
   	   	while(fgets(buffer, sizeof(buffer), stdin))
		{
   			int pid;
			//kill the parent process
			if (kill(getppid(),SIGKILL)==-1)
				printf("kill error\n");
				
			//statistical 
			pkt_info=(struct packet_info *)shmat(shmid,NULL,0);
			stat_udp(pkt_info);
			if (shmctl(shmid,IPC_RMID,&shbuf)==-1)
				perror("shmctl");
			shutdown(sockfd,2);
			exit(0);
		}
	}
	else
	{     

		printf("waiting fot receiving udp data....\n");	
   		
   		
		struct packet_info *pkt_info;
		
		struct timeb tp;
		ftime(&tp);
		
		struct tm *p;
		time_t timep;
		
		time(&timep);
		p=gmtime(&timep);
		
		
		printf("time %d:%d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec,tp.millitm);
		
		pkt_info=(struct packet_info *)shmat(shmid,NULL,0);
		
		pkt_info->total_bytes=0;
		pkt_info->total_packet=0;
	
		while(1)	// 一直循环
		{
			//receive the data
					
			pkt_info->rxlen[pkt_info->total_packet]
				 = recvfrom(sockfd,buf,sizeof(buf),0,(struct socket_in *)&addr,&addr_len);
			
			
			if ( pkt_info->rxlen[pkt_info->total_packet] > 0 )
			{
				
				
			//	printf("received data %d len %d\n",pkt_info->total_packet,pkt_info->rxlen[pkt_info->total_packet]);
								
				//time information
				ftime(&tp);
				time(&timep);
				p=gmtime(&timep);
				
				pkt_info->rxth[pkt_info->total_packet]=p->tm_hour;
				pkt_info->rxtm[pkt_info->total_packet]=p->tm_min;
				pkt_info->rxts[pkt_info->total_packet]=p->tm_sec;
				pkt_info->rxtms[pkt_info->total_packet]=tp.millitm;
				
				//packet information from trasmitter
				packet_header = (struct w_header *)buf;
				pkt_info->total_bytes +=pkt_info->rxlen[pkt_info->total_packet];
				pkt_info->rxseq_num[pkt_info->total_packet]=packet_header->seq_num;
				pkt_info->rxlast_flag[pkt_info->total_packet]=packet_header->last_flag;
				pkt_info->rxrate[pkt_info->total_packet]=packet_header->rate;
				pkt_info->tx_packet=packet_header->tx_packet;
				pkt_info->total_packet++;
					
				//judge if it is the last packet, with the flag=0x5555
				if (pkt_info->rxlast_flag[pkt_info->total_packet-1]==0x5555)
				{
					//do statistic
					stat_udp(pkt_info);
					//release the shared memory
					if (shmctl(shmid,IPC_RMID,&shbuf)==-1)
						perror("shmctl");
					shutdown(sockfd,2);
					exit(0);
						
				}
				
			}
			else
			{
				perror("recvfrom");				
			}	
		}
	}	
} 

void stat_udp(struct packet_info *pkt_info)
{
	FILE *stream1;
	int i;
	
	
	
	if (pkt_info->total_packet<1)
		{
			printf("no packet is received\n");
			exit(0);
		}
	
	//calculate the throughput 
	int total_packet=pkt_info->total_packet-1;	
	double duration = (pkt_info->rxth[total_packet] - pkt_info->rxth[0]) * 3600 + (pkt_info->rxtm[total_packet] - pkt_info->rxtm[0]) * 60 + 
			(pkt_info->rxts[total_packet]-pkt_info->rxts[0]) + (pkt_info->rxtms[total_packet] - pkt_info->rxtms[0]) * 0.001;
	double rate_measured = (double)(pkt_info->total_bytes*8)/(duration+0.001);
	double fer=(double)(pkt_info->total_packet*1.0)/pkt_info->tx_packet;  
	
	for(i=0;i<pkt_info->total_packet;i++)
		printf("\npacket %d with length %d, last_flag %x at time %d:%d:%d:%d",pkt_info->rxseq_num[i],pkt_info->rxlen[i],pkt_info->rxlast_flag[i],pkt_info->rxth[i],pkt_info->rxtm[i],pkt_info->rxts[i],pkt_info->rxtms[i]);
	printf("\nend : total packet %d total bytes %d last_flag %x\n",pkt_info->total_packet, pkt_info->total_bytes,pkt_info->rxlast_flag[pkt_info->total_packet-1]);
	printf("throughput=%0.4fM nfer=%0.4f\n", rate_measured/1000000.0,fer);
					
					
	
	if( (stream1 = fopen("record", "a+" )) == NULL )
		printf( "Can not open the file\n" );
	//			else
		//			fprintf(stream1,"\n\n*********************\nthe following is a new record\n*********************\n");
					
				/*for(i=1;i<total_packet+1;i++)
					fprintf(stream1,"\npacket %d with length %d, last_flag %x at time %d:%d:%d:%d",rxseq_num[i],rxlen[i],rxlast_flag[i],rxth[i],rxtm[i],rxts[i],rxtms[i]);
				*/
	fprintf(stream1,"\ntotal packet: %5d length: %5d rate: %3dM throughput: %0.3fM",pkt_info->total_packet, pkt_info->rxlen[1],pkt_info->rxrate[1],rate_measured/1000000.0);
	fclose( stream1 );
}
				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -