资源描述:
《epoll并发连接测试》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、亲测cpoll最大并发数,通过测试并发书为30000时,服务端会容易死机。下而是服务端的程序:#include#inelude#include#ineludeftinclude#include#inelude#include#inelude^include#inel
2、ude#include#inelude^defineMAXBUF1024#defineMAXEPOLLSIZE3000/sctnonblocking-设置句柄为非阻塞方式/intsetnonblocking(intsockfd){if(fcntl(sockfd,F_SETFL,fcntl(sockfd,F_GETFD,0)0_N0NBL0CK)==-1)return-1;return0;}intmain(intargc,charargv[]){
3、int1istener,new_fd,epfd,nfds,n,ret,curfds;socklen_tlen;structsockaddr_inm5^_addr,their_addr;unsignedintmyport,lisnum;structepoll_eventev;structepoll_eventevents[MAXEPOLLSIZE];structrlimitrt;charbuffer[MAXBUF];charrsp_msg[2]=/zokz/;myport二5000;lisnum二100;/设置每个进程允许打开
4、的最大文件数/rt.rlimmax二rt.rlimcur二MAXEPOLLSIZE;if(setrlimit(RLIMIT_NOFILE,&rt)==-1){perror(/zsetr1imit/z);exit(1);}/开启socket监听/if((listener=socket(PF_INET,SOCK_STREAM,0))==-1){perror(^socket〃);exit(1);}elseprintfCsocket创建成功!〃);sctnonblocking(listcncr);my_acldr.sin_fam
5、ily=PF_INET;my_addr.sin_port=htons(myport);myaddr.sinaddr.saddr二INADDRANY;bzero(&(my_addr.sin_zero),8);if(bind(listener,(structsockaddr^)&my_addr,sizeof(structsockaddr))==-1){perror(z/bindz/);exit(1);}elseprintf(,ZIP地址和端口绑定成功〃);if(listcn(listclisnum)二二~1){perror(
6、"listen");exit(1);}elseprintfC开启服务成功!〃);/创建epoll句柄,把监听socket加入到epoll集合里/epfd=epoll_create(MAXEP0LLS1ZE);len=sizeof(structsockaddrin);ev.events二EPOLLINEPOLLET;ev.data,fd二1istener;if(cpoll_ctl(epfd,EPOLL_CTL_ADD,listener,&cv)<0)//注册{fprintf(stderr,〃epollsetinsertio
7、nerror:fd=%d/z,listener);returnT;}elseprintf(,z监听socket加入epoll成功!〃);curfds二0;while(1){/等待冇事件发生/nfds=epollwait(epfd,events,MAXEPOLLSIZE,-1);if(nfds二二-1){pcrror(,zcpoll_waitz,);break;}printf("当前并发连接数:%d〃,curfds);printfC,nfds=%d/,,nfds);for(n=0;n8、events[n]・data,fd==listener){newfd二accept(1istener,(structsockaddr)&theiraddr,&lcn);if(new_fd<0){"perror(,zaccept,z);continue;}else{printf(,z有连接来