- static int s_epfd;//epoll描述字
- {//初始化epoll
- struct epoll_event ev;
- //设置epoll
- s_epfd = epoll_create(65535);
- {//这个过程可以循环以便加入多个LISTEN套接字进入epoll事件集合
- //<a href="http://www.doserver.net/tag.php?tag=%E6%9C%8D%E5%8A%A1%E5%99%A8" target="_blank" title="http://www.doserver.net/tag.php?tag=%E6%9C%8D%E5%8A%A1%E5%99%A8" class="mykeyword">服务器</a>监听创建
- rc = listen();//listen参数这里省略
- //加入epoll事件集合
- ev.events = EPOLLIN;
- ev.data.fd = rc;
- if (epoll_ctl(s_epfd, EPOLL_CTL_ADD, rc, &ev) < 0) {
- fprintf(stderr, "epoll set insertion error: fd=%d", rc);
- return(-1);
- }
- }
- }
- {//epoll事件处理
- int i, nfds, sock_new;
- struct epoll_event events[16384];
- for( ; ; ) {
- //等待epoll事件
- nfds = epoll_wait(s_epfd, events, 16384, -1);
- //处理epoll事件
- for(i = 0; i < nfds; i++) {
- //events[i].data.fd是epoll事件中弹出的套接字
- //接收连接
- sock_new = accept(events[i].data.fd);//accept其它参数这里省略了
- if(0 > sock_new) {
- fprintf(stderr, "接收客户端连接失败\n");
- continue;
- }
- }
- }
- }
Epoll模型代码
最新推荐文章于 2024-05-08 16:36:46 发布