电脑测网速c语言,C层实现多线程测网速

可能这个程序有很多问题,所以请各位大婶批评指正,勿喷!

Server端:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define BUFSIZE 1024

pthread_t thread[4];

double sock(int port){

int server_sockfd;

int client_sockfd;

struct sockaddr_in server_addr;

struct sockaddr_in client_addr;

int sin_size;

char buf[BUFSIZE];

memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family=AF_INET;

server_addr.sin_addr.s_addr=INADDR_ANY;

server_addr.sin_port=htons(port);

if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0){

perror("socket");

return 1;

}

if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0){

perror("bind");

return 1;

}

listen(server_sockfd,5);

sin_size=sizeof(struct sockaddr_in);

if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_addr,&sin_size))<0){

perror("accept");

return 1;

}

printf("accept client %s   port%d\n",inet_ntoa(client_addr.sin_addr),port);

int buflen=128*1024;

setsockopt(client_sockfd,SOL_SOCKET,SO_RCVBUF,(const char *)&buflen,sizeof(int));

unsigned long count=0;

unsigned int length;

clock_t start,finish;

start=clock();

while((length=recv(client_sockfd,buf,BUFSIZE,0))>0){

if(length<0){

printf("receive failed!");

break;

}

count+=length;

}

finish=clock();

printf("The total count of byte is %ld bytes\n",count);

double time=(double)(finish-start)/CLOCKS_PER_SEC;

double v=(double)(count)/1024/time;

printf("v = %lf KB/s \n",v);

return v;

}

double v1,v2,v3,v4;

void *thread1()

{

v1=sock(8000);

pthread_exit(NULL);

}

void *thread2()

{

v2=sock(8001);

pthread_exit(NULL);

}

void *thread3()

{

v3=sock(8002);

pthread_exit(NULL);

}

void *thread4()

{

v4=sock(8003);

pthread_exit(NULL);

}

void thread_create(void)

{

int temp;

memset(&thread,0,sizeof(thread));

if((temp=pthread_create(&thread[0],NULL,thread1,NULL))!=0)

printf("creating thread1 failed!\n");

else

printf("creating thread1 success!\n");

if((temp=pthread_create(&thread[1],NULL,thread2,NULL))!=0)

printf("creating thread2 failed!\n");

else

printf("creating thread2 success!\n");

if((temp=pthread_create(&thread[2],NULL,thread3,NULL))!=0)

printf("creating thread3 failed!\n");

else

printf("creating thread3 success!\n");

if((temp=pthread_create(&thread[3],NULL,thread4,NULL))!=0)

printf("creating thread4 failed!\n");

else

printf("creating thread4 success!\n");

}

void thread_wait(void)

{

if(thread[0]!=0)

{

pthread_join(thread[0],NULL);

printf("thread1 is finished!\n");

}

if(thread[1]!=0)

{

pthread_join(thread[1],NULL);

printf("thread2 is finished!\n");

}

if(thread[3]!=0)

{

pthread_join(thread[3],NULL);

printf("thread3 is finished!\n");

}

if(thread[4]!=0)

{

pthread_join(thread[4],NULL);

printf("thread4 is finished!\n");

}

}

int main()

{

printf("I'm main function.I'm creating thread!\n");

thread_create();

printf("I'm main function.I'm waiting for thread to finish task!\n");

thread_wait();

printf("The total of vate is %lf KB/s\n",v1+v2+v3+v4);

return 0;

}

Client端:

#include

#include

#include

#include

#include

#include

#include

#define filename "/sdcard/ssss.mp3"

#define BUFSIZE 1024

#define MAX 10

#define TIMES 2500

pthread_t thread[4];

int port1=0,port2=0,port3=0,port4=0;

char* addr;

double sock(char* addr,char* port){

int i=TIMES;

int client_sockfd;

struct sockaddr_in server_addr;

char buf[BUFSIZE];

memset(&server_addr,0,sizeof(server_addr));

server_addr.sin_family=AF_INET;

server_addr.sin_addr.s_addr=inet_addr(addr);

server_addr.sin_port=htons(atoi(port));

if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)

{

perror("socket");

return 1;

}

printf("create client socket\n");

int buflen=128*1024;

setsockopt(client_sockfd,SOL_SOCKET,SO_SNDBUF,(const char *)&buflen,sizeof(int));

if(connect(client_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0)

{

perror("connect");

return 1;

}

printf("connected to server\n");

FILE* from_fd;

if((from_fd=fopen(filename,"r"))==-1)

{

printf("open or create file failed!");

exit(0);

}

int len=fread(buf,sizeof(char),BUFSIZE,from_fd);

clock_t start,finish;

start=clock();

while(i--){

if(send(client_sockfd,buf,len,0)<0){

printf("length<0\n");

break;

}

}

finish=clock();

double time=(double)(finish-start)/CLOCKS_PER_SEC;

double v=TIMES/time;

printf("v = %lf KB/s \n",v);

return v;

}

double v1,v2,v3,v4;

void *thread1()

{

v1=sock(addr,port1);

pthread_exit(NULL);

}

void *thread2()

{

v2=sock(addr,port2);

pthread_exit(NULL);

}

void *thread3()

{

v3=sock(addr,port3);

pthread_exit(NULL);

}

void *thread4()

{

v4=sock(addr,port4);

pthread_exit(NULL);

}

void thread_create(void)

{

int temp;

memset(&thread,0,sizeof(thread));

if((temp=pthread_create(&thread[0],NULL,thread1,NULL))!=0)

printf("creating thread1 failed!\n");

else

printf("creating thread1 success!\n");

if((temp=pthread_create(&thread[1],NULL,thread2,NULL))!=0)

printf("creating thread2 failed!\n");

else

printf("creating thread2 success!\n");

if((temp=pthread_create(&thread[2],NULL,thread3,NULL))!=0)

printf("creating thread3 failed!\n");

else

printf("creating thread3 success!\n");

if((temp=pthread_create(&thread[3],NULL,thread4,NULL))!=0)

printf("creating thread4 failed!\n");

else

printf("creating thread4 success!\n");

}

void thread_wait(void)

{

if(thread[0]!=0)

{

pthread_join(thread[0],NULL);

printf("thread1 is finished!\n");

}

if(thread[1]!=0)

{

pthread_join(thread[1],NULL);

printf("thread2 is finished!\n");

}

if(thread[2]!=0)

{

pthread_join(thread[2],NULL);

printf("thread3 is finished!\n");

}

if(thread[3]!=0)

{

pthread_join(thread[3],NULL);

printf("thread4 is finished!\n");

}

}

int main(int argc,char *argv[])

{

addr=argv[1];

port1=argv[2];

port2=argv[3];

port3=argv[4];

port4=argv[5];

printf("I'm main function.I'm creating thread!\n");

thread_create();

printf("I'm main function.I'm waiting for thread to finish task!\n");

thread_wait();

printf("The total of vate is %lf KB/s\n",v1+v2+v3+v4);

exit(0);

}

另外有个小问题,sock函数返回double类型的时候,是不是会影响运行速度?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值