epoll_create, epoll_ctl和epoll_wait
参考代码
#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h> using namespace std; #define MAXLINE 5
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 5000
#define INFTIM 1000 void setnonblocking(int sock)
{
int opts;
opts=fcntl(sock,F_GETFL);
if(opts<)
{
perror("fcntl(sock,GETFL)");
exit();
}
opts = opts|O_NONBLOCK;
if(fcntl(sock,F_SETFL,opts)<)
{
perror("fcntl(sock,SETFL,opts)");
exit();
}
} int main(int argc, char* argv[])
{
int i, maxi, listenfd, connfd, sockfd,epfd,nfds, portnumber;
ssize_t n;
char line[MAXLINE];
socklen_t clilen; if ( == argc )
{
if( (portnumber = atoi(argv[])) < )
{
fprintf(stderr,"Usage:%s portnumber/a/n",argv[]);
return ;
}
}
else
{
fprintf(stderr,"Usage:%s portnumber/a/n",argv[]);
return ;
} //声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件 struct epoll_event ev,events[];
//生成用于处理accept的epoll专用的文件描述符 epfd=epoll_create();
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
listenfd = socket(AF_INET, SOCK_STREAM, );
//把socket设置为非阻塞方式 //setnonblocking(listenfd); //设置与要处理的事件相关的文件描述符 ev.data.fd=listenfd;
//设置要处理的事件类型 ev.events=EPOLLIN|EPOLLET;
//ev.events=EPOLLIN; //注册epoll事件 epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
char *local_addr="127.0.0.1";
inet_aton(local_addr,&(serveraddr.sin_addr));//htons(portnumber); serveraddr.sin_port=htons(portnumber);
bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, LISTENQ);
maxi = ;
for ( ; ; ) {
//等待epoll事件的发生 nfds=epoll_wait(epfd,events,,);
//处理所发生的所有事件 for(i=;i<nfds;++i)
{
if(events[i].data.fd==listenfd)//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。 {
connfd = accept(listenfd,(sockaddr *)&clientaddr, &clilen);
if(connfd<){
perror("connfd<0");
exit();
}
//setnonblocking(connfd); char *str = inet_ntoa(clientaddr.sin_addr);
cout << "accapt a connection from " << str << endl;
//设置用于读操作的文件描述符 ev.data.fd=connfd;
//设置用于注测的读操作事件 ev.events=EPOLLIN|EPOLLET;
//ev.events=EPOLLIN; //注册ev epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
}
else if(events[i].events&EPOLLIN)//如果是已经连接的用户,并且收到数据,那么进行读入。 {
cout << "EPOLLIN" << endl;
if ( (sockfd = events[i].data.fd) < )
continue;
if ( (n = read(sockfd, line, MAXLINE)) < ) {
if (errno == ECONNRESET) {
close(sockfd);
events[i].data.fd = -;
} else
std::cout<<"readline error"<<std::endl;
} else if (n == ) {
close(sockfd);
events[i].data.fd = -;
}
line[n] = '/0';
cout << "read " << line << endl;
//设置用于写操作的文件描述符 ev.data.fd=sockfd;
//设置用于注测的写操作事件 ev.events=EPOLLOUT|EPOLLET;
//修改sockfd上要处理的事件为EPOLLOUT //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev); }
else if(events[i].events&EPOLLOUT) // 如果有数据发送 {
sockfd = events[i].data.fd;
write(sockfd, line, n);
//设置用于读操作的文件描述符 ev.data.fd=sockfd;
//设置用于注测的读操作事件 ev.events=EPOLLIN|EPOLLET;
//修改sockfd上要处理的事件为EPOLIN epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
}
}
return ;
}
//
// a simple echo server using epoll in linux
//
// 2009-11-05
// 2013-03-22:修改了几个问题,1是/n格式问题,2是去掉了原代码不小心加上的ET模式;
// 本来只是简单的示意程序,决定还是加上 recv/send时的buffer偏移
// by sparkling
//
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
using namespace std;
#define MAX_EVENTS 500
struct myevent_s
{
int fd;
void (*call_back)(int fd, int events, void *arg);
int events;
void *arg;
int status; // 1: in epoll wait list, 0 not in
char buff[]; // recv data buffer
int len, s_offset;
long last_active; // last active time
};
// set event
void EventSet(myevent_s *ev, int fd, void (*call_back)(int, int, void*), void *arg)
{
ev->fd = fd;
ev->call_back = call_back;
ev->events = ;
ev->arg = arg;
ev->status = ;
bzero(ev->buff, sizeof(ev->buff));
ev->s_offset = ;
ev->len = ;
ev->last_active = time(NULL);
}
// add/mod an event to epoll
void EventAdd(int epollFd, int events, myevent_s *ev)
{
struct epoll_event epv = {, {}};
int op;
epv.data.ptr = ev;
epv.events = ev->events = events;
if(ev->status == ){ op = EPOLL_CTL_MOD;
}
else{
op = EPOLL_CTL_ADD;
ev->status = ;
}
if(epoll_ctl(epollFd, op, ev->fd, &epv) < )
printf("Event Add failed[fd=%d], evnets[%d]\n", ev->fd, events);
else
printf("Event Add OK[fd=%d], op=%d, evnets[%0X]\n", ev->fd, op, events);
}
// delete an event from epoll
void EventDel(int epollFd, myevent_s *ev)
{
struct epoll_event epv = {, {}};
if(ev->status != ) return;
epv.data.ptr = ev;
ev->status = ;
epoll_ctl(epollFd, EPOLL_CTL_DEL, ev->fd, &epv);
}
int g_epollFd;
myevent_s g_Events[MAX_EVENTS+]; // g_Events[MAX_EVENTS] is used by listen fd
void RecvData(int fd, int events, void *arg);
void SendData(int fd, int events, void *arg);
// accept new connections from clients
void AcceptConn(int fd, int events, void *arg)
{
struct sockaddr_in sin;
socklen_t len = sizeof(struct sockaddr_in);
int nfd, i;
// accept
if((nfd = accept(fd, (struct sockaddr*)&sin, &len)) == -)
{
if(errno != EAGAIN && errno != EINTR)
{
}
printf("%s: accept, %d", __func__, errno);
return;
}
do
{
for(i = ; i < MAX_EVENTS; i++)
{
if(g_Events[i].status == )
{
break;
}
}
if(i == MAX_EVENTS)
{
printf("%s:max connection limit[%d].", __func__, MAX_EVENTS);
break;
}
// set nonblocking
int iret = ;
if((iret = fcntl(nfd, F_SETFL, O_NONBLOCK)) < )
{
printf("%s: fcntl nonblocking failed:%d", __func__, iret);
break;
}
// add a read event for receive data
EventSet(&g_Events[i], nfd, RecvData, &g_Events[i]);
EventAdd(g_epollFd, EPOLLIN, &g_Events[i]);
}while();
printf("new conn[%s:%d][time:%d], pos[%d]\n", inet_ntoa(sin.sin_addr),
ntohs(sin.sin_port), g_Events[i].last_active, i);
}
// receive data
void RecvData(int fd, int events, void *arg)
{
struct myevent_s *ev = (struct myevent_s*)arg;
int len;
// receive data
len = recv(fd, ev->buff+ev->len, sizeof(ev->buff)--ev->len, );
EventDel(g_epollFd, ev);
if(len > )
{
ev->len += len;
ev->buff[len] = '\0';
printf("C[%d]:%s\n", fd, ev->buff);
// change to send event
EventSet(ev, fd, SendData, ev);
EventAdd(g_epollFd, EPOLLOUT, ev);
}
else if(len == )
{
close(ev->fd);
printf("[fd=%d] pos[%d], closed gracefully.\n", fd, ev-g_Events);
}
else
{
close(ev->fd);
printf("recv[fd=%d] error[%d]:%s\n", fd, errno, strerror(errno));
}
}
// send data
void SendData(int fd, int events, void *arg)
{
struct myevent_s *ev = (struct myevent_s*)arg;
int len;
// send data
len = send(fd, ev->buff + ev->s_offset, ev->len - ev->s_offset, );
if(len > )
{
printf("send[fd=%d], [%d<->%d]%s\n", fd, len, ev->len, ev->buff);
ev->s_offset += len;
if(ev->s_offset == ev->len)
{
// change to receive event
EventDel(g_epollFd, ev);
EventSet(ev, fd, RecvData, ev);
EventAdd(g_epollFd, EPOLLIN, ev);
}
}
else
{
close(ev->fd);
EventDel(g_epollFd, ev);
printf("send[fd=%d] error[%d]\n", fd, errno);
}
}
void InitListenSocket(int epollFd, short port)
{
int listenFd = socket(AF_INET, SOCK_STREAM, );
fcntl(listenFd, F_SETFL, O_NONBLOCK); // set non-blocking
printf("server listen fd=%d\n", listenFd);
EventSet(&g_Events[MAX_EVENTS], listenFd, AcceptConn, &g_Events[MAX_EVENTS]);
// add listen socket
EventAdd(epollFd, EPOLLIN, &g_Events[MAX_EVENTS]);
// bind & listen
sockaddr_in sin;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
bind(listenFd, (const sockaddr*)&sin, sizeof(sin));
listen(listenFd, );
}
int main(int argc, char **argv)
{
unsigned short port = ; // default port
if(argc == ){
port = atoi(argv[]);
}
// create epoll
g_epollFd = epoll_create(MAX_EVENTS);
if(g_epollFd <= ) printf("create epoll failed.%d\n", g_epollFd);
// create & bind listen socket, and add to epoll, set non-blocking
InitListenSocket(g_epollFd, port);
// event loop
struct epoll_event events[MAX_EVENTS];
printf("server running:port[%d]\n", port);
int checkPos = ;
while(){
// a simple timeout check here, every time 100, better to use a mini-heap, and add timer event
long now = time(NULL);
for(int i = ; i < ; i++, checkPos++) // doesn't check listen fd
{
if(checkPos == MAX_EVENTS) checkPos = ; // recycle
if(g_Events[checkPos].status != ) continue;
long duration = now - g_Events[checkPos].last_active;
if(duration >= ) // 60s timeout
{
close(g_Events[checkPos].fd);
printf("[fd=%d] timeout[%d--%d].\n", g_Events[checkPos].fd, g_Events[checkPos].last_active, now);
EventDel(g_epollFd, &g_Events[checkPos]);
}
}
// wait for events to happen
int fds = epoll_wait(g_epollFd, events, MAX_EVENTS, );
if(fds < ){
printf("epoll_wait error, exit\n");
break;
}
for(int i = ; i < fds; i++){
myevent_s *ev = (struct myevent_s*)events[i].data.ptr;
if((events[i].events&EPOLLIN)&&(ev->events&EPOLLIN)) // read event
{
ev->call_back(ev->fd, events[i].events, ev->arg);
}
if((events[i].events&EPOLLOUT)&&(ev->events&EPOLLOUT)) // write event
{
ev->call_back(ev->fd, events[i].events, ev->arg);
}
}
}
// free resource
return ;
}
参考资料
epoll_create函数实现源码分析
epoll_create epoll_ctl epoll_wait close epoll和select的简单比较
epoll使用详解(精髓)
epoll源码分析
epoll_create函数实现源码分析
epoll_create, epoll_ctl和epoll_wait的更多相关文章
- epoll机制:epoll_create、epoll_ctl、epoll_wait、close
在Linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随着监听fd数 ...
- epoll使用详解:epoll_create、epoll_ctl、epoll_wait、close
epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...
- select、poll、epoll之间的区别总结
select.poll.epoll之间的区别总结 05/05. 2014 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪 ...
- select、poll、epoll之间的区别总结[整理]
select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...
- linux I/O复用
转载自:哈维.dpkirin url:http://blog.csdn.NET/zhang_shuai_2011/article/details/7675797 http://blog.csdn.Ne ...
- select、poll、epoll区别总结
1 本质上都是同步I/O 三者都是I/O复用,本质上都属于同步I/O.因为三者只是负责通知应用程序什么时候数据准备好了,实际的I/O操作还是在由应用程序处理:如果是异步I/O的话,实际I/O由内核处理 ...
- select、poll、epoll之间的区别
select.poll.epoll之间的区别总结[整理] select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就 ...
- epoll 知识总结
poll/select/epoll 对比 http://www.cnblogs.com/apprentice89/p/3234677.html ---有待继续学习 http://blog.chi ...
- IO多路复用的几种实现机制的分析
http://blog.csdn.net/zhang_shuai_2011/article/details/7675797 select,poll,epoll都是IO多路复用的机制.所谓I/O多路复用 ...
随机推荐
- Frame Stacking 框架堆叠
/* [题目来源]http://poj.org/problem?id=1128 [题目分析] 几张图片叠在一起,给出堆叠后的情况,要求出所有可能的从下到上的堆叠顺序. [思路分析] 1.题目已经很明确 ...
- 前端必会的js知识总结整理
1.晨曦. 2.js是一门什么样的语言及特点? js是一种基于对象和事件驱动的并具有相对安全性的客户端脚本语言.也是一种广泛用于web客户端开发的脚本语言,常用来给html网页添加动态 ...
- 如何用C表示排列组合?
问题来自<Linux C一站式编程>,是个挺有意思的题目. 2.定义一个数组,编程打印它的全排列.比如定义: #define N 3 int a[N] = { 1, 2, 3 }; 则运行 ...
- poj 1848 树形dp
思路:表示我很弱,这个想不出dp方程,参考网上代码 #include<iostream> #include<algorithm> #include<cstring> ...
- Colossal Fibonacci Numbers(巨大的斐波那契数)UVA 11582
评测地址:http://acm.hust.edu.cn/vjudge/problem/41990 The i'th Fibonacci number f (i) is recursively de n ...
- 【转】创业C2C(Copy To China):停车位共享APP,用户、市政能够买账?
如果周六中午想开车到旧金山的Mission吃顿早午餐,笔者劝您还是省省吧.因为不光是到了吃饭的地儿排队得耗上一个小时,就是满大街的兜圈子找停车位都能折腾死人.那个时候您或许就明白了,其实最苦的并不是买 ...
- 纯jq编写增删改,弹出框
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- webkit,HTML5头部标签
大家都知道在移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码,更好地将移动web前端页面表现出来.本文整理一些HTML5头部<meta>标签常用的 ...
- white-space:nowrap 的妙用
对于多个元素同在同一行的布局,如比较常见的是轮播.下面我将探讨这这一布局的做法: 首先约定html结果如下: div.row div.col div.col div.col ... 做法一: 设定di ...
- 一个简单的XML与数组之间的转换
xml是网络使用最多的数据交换格式,所以,不掌握怎么操作它,又有蛋疼的了. php中可以操作xml的类/函数很多,个人认为最简单的是SimpleXMLElement这个类,它的使用就跟其名字一样:简单 ...