IO多路复用之epoll(二)
前一篇介绍了epoll的LT模式,LT模式注意epollout事件在数据全部写成功后需要取消关注,
或者更改为EPOLLIN。
而这次epoll的ET模式,要注意的是在读和写的过程中要在循环中写完或者读完所有数据,
确保不要丢掉一些数据。
因为epoll ET模式只在两种边缘更改的时候触发,对于读事件只在内核缓冲区由空变为
非空通知一次用户,对于写事件,内核缓冲区只在由满变为非满的情况通知用户一次。
下面是代码
int main()
{
int eventsize = 20;
struct epoll_event * epoll_eventsList = (struct epoll_event *)malloc(sizeof(struct epoll_event)
*eventsize);
//打开一个空的描述符
int idlefd = open("/dev/null",O_RDONLY|O_CLOEXEC);
cout << "idlefd" <<idlefd <<endl;
//生成listen描述符
int listenfd = socket(PF_INET, SOCK_CLOEXEC | SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
if(listenfd < 0)
{
ERR_EXIT("socketfd");
}
//初始化地址信息
struct sockaddr_in servaddr;
memset(&servaddr,0 ,sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6667);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)))
ERR_EXIT("bindError");
if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");
//记录客户端连接对应的socket
std::vector<int> clients;
//创建epollfd, 用于管理epoll事件表
int epollfd;
epollfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event event;
event.data.fd = listenfd;
event.events = EPOLLIN|EPOLLET;
//将listenfd加入到epollfd管理的表里
epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);
//用于接收新连接的客户端地址
struct sockaddr_in peeraddr;
socklen_t peerlen;
int connfd;
//为了简单起见写了个很大的数组,根据文件描述符存储内容
//其实很多项目代码采epoll.data.ptr回调具体的读写
vector<string> recievebuf;
for(int i = 0 ; i < 22222; i++)
{
recievebuf.push_back("");
}
while(1)
{
int nready = epoll_wait(epollfd, epoll_eventsList, eventsize, -1);
if (nready == -1)
{
if (errno == EINTR)
continue;
ERR_EXIT("epoll_wait");
}
if (nready == 0)
continue;
//大小不够重新开辟
if ((size_t)nready == eventsize)
{
if(eventsize * 2 >= 22222)
{
ERR_EXIT("too many fds");
}
struct epoll_event * epoll_eventsList2 = (struct epoll_event *)malloc(sizeof(struct epoll_event) *
eventsize *2);
if(epoll_eventsList2)
{
memcpy(epoll_eventsList2,epoll_eventsList,sizeof(struct epoll_event) * eventsize);
eventsize = eventsize * 2;
free(epoll_eventsList);
epoll_eventsList = epoll_eventsList2;
}
}
for (int i = 0; i < nready; ++i)
{
//判断wait返回的events数组状态是否正常
if ((epoll_eventsList[i].events & EPOLLERR) ||
(epoll_eventsList[i].events & EPOLLHUP))
{
fprintf (stderr, "epoll error\n");
close (epoll_eventsList[i].data.fd);
continue;
}
if (epoll_eventsList[i].data.fd == listenfd)
{
peerlen = sizeof(peeraddr);
//ET模式accept放在while循环里
do
{
connfd = ::accept4(listenfd, (struct sockaddr*)&peeraddr,
&peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if(connfd <= 0)
break;
std::cout<<"ip="<<inet_ntoa(peeraddr.sin_addr)<<
" port="<<ntohs(peeraddr.sin_port)<<std::endl;
clients.push_back(connfd);
//将connd加入epoll表里,关注读事件
event.data.fd = connfd;
event.events = EPOLLIN |EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event);
cout << "loop" <<endl;
cout << "loop" << connfd << endl;
}while(1);
//accept失败,判断是否接收全所有的fd
cout << connfd << endl;
if (connfd == -1){
if (errno != EAGAIN && errno != ECONNABORTED
&& errno != EPROTO && errno != EINTR)
{
cout << "error" <<endl;
ERR_EXIT("accept");
}
}
//所有请求都处理完成
cout << "continue"<<endl;
continue;
}//endif
else if(epoll_eventsList[i].events & EPOLLIN)
{
connfd = epoll_eventsList[i].data.fd;
if(connfd > 22222)
{
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());
continue;
}
char buf[1024] = {0};
if(connfd < 0)
continue;
int ret = 0;
int total = 0;
std::string strtemp;
while(1)
{
cout << "begin read" <<endl;
ret = read(connfd, buf, 1024);
if(ret <= 0)
{
break;
}
strtemp += string(buf);
total += ret;
memset(buf, 0, 1024);
if(ret < 1024)
{
break;
}
}//endwhile(1)
cout << "end read" <<endl;
recievebuf[connfd] = strtemp.c_str();
cout << "buff data :" << recievebuf[connfd]<<endl;
if(ret == -1)
{
if((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
//由于内核缓冲区空了,下次有数据到来是会触发epollin
continue;
}
ERR_EXIT("read");
}//endif ret == -1
//连接断开
if(ret == 0)
{
std::cout<<"client close"<<std::endl;
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());
continue;
}
cout << "turn to write" << endl;
//更改为写模式
event.data.fd = connfd;
event.events = EPOLLOUT | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);
cout << "epoll mod change success" << endl;
}//end elif
else //写事件
{
if(epoll_eventsList[i].events & EPOLLOUT)
{
cout << "begin write" <<endl;
connfd = epoll_eventsList[i].data.fd;
int count = 0;
int totalsend = 0;
char buf[1024];
strcpy(buf, recievebuf[connfd].c_str());
cout << "write buff" <<buf<<endl;
while(1)
{
int totalcount = strlen(buf);
int pos = 0;
count = write(epoll_eventsList[i].data.fd, buf + pos, totalcount);
cout << "write count:" << count;
if(count < 0)
{
break;
}
if(count < totalcount)
{
totalcount = totalcount - count;
pos += count;
}
else
{
break;
}
}//end while
if(count == -1)
{
if((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
//由于内核缓冲区满了
//于内核缓冲区满了
continue;
}
ERR_EXIT("write");
}
if(count == 0)
{
std::cout<<"client close"<<std::endl;
close(connfd);
event = epoll_eventsList[i];
epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);
clients.erase(std::remove(clients.begin(), clients.end(), connfd),
clients.end());
continue;
}
event.data.fd = connfd;
event.events = EPOLLIN|EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);
}
}//end eles 写事件
}
}
}
源代码下载地址:http://download.csdn.net/detail/secondtonone1/9486222

IO多路复用之epoll(二)的更多相关文章
- python 网络编程 IO多路复用之epoll
python网络编程——IO多路复用之epoll 1.内核EPOLL模型讲解 此部分参考http://blog.csdn.net/mango_song/article/details/4264 ...
- IO多路复用之epoll总结
1.基本知识 epoll是在2.6内核中提出的,是之前的select和poll的增强版本.相对于select和poll来说,epoll更加灵活,没有描述符限制.epoll使用一个文件描述符管理多个描述 ...
- IO多路复用之epoll
1.基本知识 epoll是在2.6内核中提出的,是之前的select和poll的增强版本.相对于select和poll来说,epoll更加灵活,没有描述符限制.epoll使用一个文件描述符管理多个描述 ...
- IO多路复用与epoll机制浅析
epoll是Linux中用于IO多路复用的机制,在nginx和redis等软件中都有应用,redis的性能好的原因之一也就是使用了epoll进行IO多路复用,同时epoll也是各大公司面试的热点问题. ...
- linux网络编程 IO多路复用 select epoll
本文以我的小型聊天室为例,对于服务器端的代码,做了三次改进,我将分别介绍阻塞式IO,select,epoll . 一:阻塞式IO 对于聊天室这种程序,我们最容易想到的是在服务器端accept之后,然后 ...
- python网络编程——IO多路复用之epoll
1.内核EPOLL模型讲解 此部分参考http://blog.csdn.net/mango_song/article/details/42643971博文并整理 首先我们来定义流的概念,一个流 ...
- Linux IO多路复用之epoll网络编程及源码(转)
原文: 前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网 ...
- IO多路复用之epoll(一)讲解
网络通信中socket有自己的内核发送缓冲区和内核接受缓冲区,好比是一个水池, 当用户发送数据的时候会从用户缓冲区拷贝到socket的内核发送缓冲区,然后从 socket发送缓冲区发出去, 当用户要读 ...
- Linux IO多路复用之epoll网络编程(含源码)
前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网络读取一 ...
随机推荐
- WCF传送大数据时的错误“ 超出最大字符串内容长度配额”
格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult.InnerException 消息是“反序 ...
- 团队项目NABCD
团队成员及项目简介 团队名:伍陸柒 团队成员: 李 俏(20132912 信1301-2) 郝 颖(20132919 信1301-2)http://www.cnblogs.com/haoying1 ...
- HDU 4055 Number String dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...
- 人生的第一篇blog
开始写博客了,人生第一篇博客啊,要写些什么呢?想想也没有什么头绪,随便写写吧. 这学期要使用代码管理工具了,要写团队项目了.一直以来都是自己一个人在默默编程,没有过合作经历.对于代码的管理也只是一直在 ...
- Mac10.11.2 Apache 服务配置
系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...
- MySQL 基于mysqldump备份工具实战演练
前言: 细节提示:先执行 show global variables like 'log_bin';看看log_bin的值,如果服务器变量log_bin的值为OFF,需要修改my.cnf配置文件,将l ...
- 第四周PSP &进度条
团队项目PSP 一:表格 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论开发环境.工具以及技术 8:37 10:42 25 10 ...
- php关于static和self的一点理解
在使用和学习laravel的过程中,总会看到类似与static::methods或者static::variable的使用方式,对此感觉到疑惑和不解,后来查阅了相关的资料才知道他是php5.3之后新加 ...
- 【Nginx】优化配置
nginx优化 突破十万并发 一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它 ...
- windows下的coreseek安装及PHP调用入门
转载:http://zhan.renren.com/longmensoft?gid=3602888498043096197&checked=true 把我的运行环境简单说一下:windows ...