博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP服务器初步,仅仅实现两端通信
阅读量:5139 次
发布时间:2019-06-13

本文共 3454 字,大约阅读时间需要 11 分钟。

客户端:

#include 
#include
#include
#include
#include
#include
#include
#include
#define MAXDATASIZE 100 int connect_server(int argc, char *argv[]){ int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in their_addr; unsigned int myport; if(argv[2]) myport = atoi(argv[2]); else myport = 80; if (argc != 3) { fprintf(stderr,"usage: %s hostname port\n", argv[0]); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { herror("gethostbyname"); exit(1); } if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family=PF_INET; their_addr.sin_port=htons(myport); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero),0); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = 0; printf("Received: %s\n",buf); close(sockfd); return 0;}void* thread_process(void* none){ int argc = 3; char* argv[]={ "user","192.168.101.99","8080"}; int i = 0; while(i++<1000){ connect_server(argc,argv); }}int main(int argc, char *argv[]){ int result; int i = 0; struct timeval tpstart,tpend; float timeuse; pthread_t pthid[10]; gettimeofday(&tpstart,NULL); for(i=0;i<10;i++) pthread_create(&pthid[i],NULL,thread_process,NULL); for(i=0;i<10;i++) pthread_join(pthid[i],NULL); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; timeuse/=1000000; printf("Used Time:%f\n",timeuse); return 0;}

 

服务器端:

#include
#include
#include
#include
#include
#include
#include
#include
#include
/************************************* *file:socketc *data time:2013-5.1 *autho:bluesoybottle * * ***************************************/void* newfd_process(void* argv){ int new_fd = (*(int*)argv); if(send(new_fd,"HTTP 200 OK\n",14,0) == -1){ perror("send ok"); } close(new_fd);}int main_socket(int arvg_port,int listen_num){ int sockfd,new_fd; struct sockaddr_in local_addr; struct sockaddr_in user_addr; unsigned int sin_size,local_port,lisnum; if(arvg_port>0) local_port = arvg_port; else local_port = 8080; if(listen_num > 0 ) lisnum = listen_num; else lisnum = 511; if((sockfd = socket(PF_INET,SOCK_STREAM,0)) == -1){ perror("socket failed\n"); return -1; } local_addr.sin_family = PF_INET; local_addr.sin_port = htons(local_port); local_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(local_addr.sin_zero),0); if(bind(sockfd,(struct sockaddr *)&local_addr,sizeof(struct sockaddr)) == -1){ perror("bind failed\n"); return -1; } if(listen(sockfd,lisnum) == -1){ perror("listen failed\n"); return -1; } sin_size = sizeof(struct sockaddr_in); int total = 0; while(1){ if((new_fd = accept(sockfd,(struct sockaddr*)&user_addr,&sin_size)) == -1 ){ perror("accpet"); continue; } total++; printf("N:%d.server:get connection from%s\n",total,inet_ntoa(user_addr.sin_addr)); pthread_t thread_id; pthread_create(&thread_id,NULL,newfd_process,&new_fd); pthread_join(thread_id,NULL); }}int main(){ int result = main_socket(8080,-1); printf("reulst = %d\n",result); return 0;}

十个客户端并发1000连接,时间为2.8s

转载于:https://www.cnblogs.com/gogly/archive/2013/05/01/3053610.html

你可能感兴趣的文章
打地鼠游戏(7)
查看>>
redis实现分页技术
查看>>
第八章 SpringCloud之Feign、Hystrix结合使用
查看>>
程序员是怎样炼成的-读书笔记序
查看>>
C++ main函数的参数
查看>>
Oracle下rman备份和还原到数据库任意一个时间点
查看>>
yeild 理解
查看>>
【转载】详细解读C#中的 .NET 弱事件模式
查看>>
oracle中如何修改process
查看>>
装饰器模式
查看>>
Saltstack入门
查看>>
Supervisor安装和配置
查看>>
ubuntu查看系统桌面的环境
查看>>
Quartz定时器
查看>>
COMP3055 Machine Learning Coursework
查看>>
字符串截取的函数自定义
查看>>
IntelliJ IDEA maven 构建简单springmvc项目
查看>>
Mysql临时文件目录控制
查看>>
python BeautifulSoup html解析
查看>>
关于为什么不推荐使用用户定义表类型的说明
查看>>