C层实现多线程测网速

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

 

Server端:

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include<time.h>

 

#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<pthread.h>
#include<stdio.h>
#include<sys/time.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<time.h>

#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类型的时候,是不是会影响运行速度?

 

 

 


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种编程语言,也是一种集成开发环境(IDE),通过它我们可以创建多线程的应用程序。多线程试是指对使用多线程的应用程序进行试。 在Delphi中,多线程可以通过创建TThread对象来实现。TThread是Delphi中用于支持多线程开发的类。使用多线程的好处是可以让应用程序在执行某些任务时能够同时处理其他任务,提高程序的效率和响应速度。 在进行多线程试时,需要重点关注以下几个方面: 1. 正确性试:确保多线程任务的逻辑正确性。通过自定义的试用例来验证多线程任务在不同条件下的正确性,包括对共享数据的处理、线程同步和互斥等。 2. 性能试:评估多线程任务的性能。通过对任务执行时间、CPU占用率等指标的试,评估多线程任务的效率和响应速度。 3. 并发试:试多个线程同时运行时的稳定性。模拟多个并发请求,多线程任务的并发处理能力和稳定性。 4. 内存管理试:多线程任务对内存的使用情况。通过检内存占用量、内存泄漏等指标,评估多线程任务对内存的管理是否合理。 此外,可以使用Delphi自带的调试工具来辅助多线程试,如使用调试器检潜在的问题和错误。 总之,Delphi多线程试是为了确保多线程应用程序的稳定性、正确性和性能。通过综合考虑任务逻辑、性能、并发和内存管理等方面的试,可以有效地评估多线程应用程序的质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值