怎样使用epoll实现客户端UDP并发?
网络程序为了支持并发,可以采用select,多线程等技术。 但是对于select,readhat linux系统只支持最大1024个描述符。 因此要想同时并发超过1024,就无法使用select模式。 而使用多线程,并发数达到1000时将严重影响系统的性能。 而使用epoll可以避免以上的缺陷。 下面是一个使用epoll实现客户端UDP并发。是我为写压力测试程序而写的。 发送使用一个独立的线程,接收使用epoll调用。 在程序开始要先设置系统能打开的最大描述符限制。即setrlimt调用。 在linux readhat enterprise 4环境下测试通过。其它环...全部
网络程序为了支持并发,可以采用select,多线程等技术。 但是对于select,readhat linux系统只支持最大1024个描述符。 因此要想同时并发超过1024,就无法使用select模式。
而使用多线程,并发数达到1000时将严重影响系统的性能。 而使用epoll可以避免以上的缺陷。 下面是一个使用epoll实现客户端UDP并发。是我为写压力测试程序而写的。 发送使用一个独立的线程,接收使用epoll调用。
在程序开始要先设置系统能打开的最大描述符限制。即setrlimt调用。 在linux readhat enterprise 4环境下测试通过。其它环境我没测过。 g++ -o udp_epoll_c udp_epoll_c。
cpp -lpthread /*************************************************************************** file: udp_epoll_c。
cpp ------------------- begin : 2006/01/17 copyright : (C) 2005 by 张荐林 email : zhangjianlin_8 at 126。
com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version。
* * * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include
#include using namespace std; int Read(int fd,void *buffer,unsigned int length) { unsigned int nleft; int nread; char *ptr; ptr = (char *)buffer; nleft = length; while(nleft > 0) { if((nread = read(fd, ptr, nleft))< 0) { if(errno == EINTR) nread = 0; else return -1; }
以上是我对于这个问题的解答,希望能够帮到大家。
收起