本文转自:https://www.jianshu.com/p/ee381d365a29

作者: 大呀大帝国 </br>
email:drnijq@126.com

1.Epoll简介

EPOLL 的API用来执行类似poll()的任务。能够用于检测在多个文件描述符中任何IO可用的情况。Epoll API可以用于边缘触发(edge-triggered)和水平触发(level-triggered), 同时epoll可以检测更多的文件描述符。以下的系统调用函数提供了创建和管理epoll实例:

  • epoll_create() 可以创建一个epoll实例并返回相应的文件描述符(epoll_create1() 扩展了epoll_create() 的功能)。
  • 注册相关的文件描述符使用epoll_ctl()
  • epoll_wait() 可以用于等待IO事件。如果当前没有可用的事件,这个函数会阻塞调用线程。

边缘触发(edge-triggered 简称ET)和水平触发(level-triggered 简称LT):

epoll的事件派发接口可以运行在两种模式下:边缘触发(edge-triggered)和水平触发(level-triggered),两种模式的区别请看下面,我们先假设下面的情况:

  1. 一个代表管道读取的文件描述符已经注册到epoll实例上了。
  2. 在管道的写入端写入了2kb的数据。
  3. epoll_wait 返回一个可用的rfd文件描述符。
  4. 从管道读取了1kb的数据。
  5. 调用epoll_wait 完成。

如果rfd被设置了ET,在调用完第五步的epool_wait 后会被挂起,尽管在缓冲区还有可以读取的数据,同时另外一段的管道还在等待发送完毕的反馈。这是因为ET模式下只有文件描述符发生改变的时候,才会派发事件。所以第五步操作,可能会去等待已经存在缓冲区的数据。在上面的例子中,一个事件在第二步被创建,再第三步中被消耗,由于第四步中没有读取完缓冲区,第五步中的epoll_wait可能会一直被阻塞下去。

下面情况下推荐使用ET模式:

  1. 使用非阻塞的IO。
  2. epoll_wait() 只需要在read或者write返回的时候。

相比之下,当我们使用LT的时候(默认),epoll会比poll更简单更快速,而且我们可以使用在任何一个地方。

2.API介绍

先简单的看下EPOLL的API

2.1 创建epoll


#include <sys/epoll.h>

int epoll_create(int size);
int epoll_create1(int flags);

epoll_create() 可以创建一个epoll实例。在linux 内核版本大于2.6.8 后,这个size 参数就被弃用了,但是传入的值必须大于0。

在 epoll_create () 的最初实现版本时, size参数的作用是创建epoll实例时候告诉内核需要使用多少个文件描述符。内核会使用 size 的大小去申请对应的内存(如果在使用的时候超过了给定的size, 内核会申请更多的空间)。现在,这个size参数不再使用了(内核会动态的申请需要的内存)。但要注意的是,这个size必须要大于0,为了兼容旧版的linux 内核的代码。

epoll_create() 会返回新的epoll对象的文件描述符。这个文件描述符用于后续的epoll操作。如果不需要使用这个描述符,请使用close关闭。

epoll_create1() 如果flags的值是0,epoll_create1()等同于epoll_create()除了过时的size被遗弃了。当然flasg可以使用 EPOLL_CLOEXEC,请查看 open() 中的O_CLOEXEC来查看 EPOLL_CLOEXEC有什么用。

返回值: 如果执行成功,返回一个非负数(实际为文件描述符), 如果执行失败,会返回-1,具体原因请查看error.

2.2 设置epoll事件


#include <sys/epoll.h>

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

这个系统调用能够控制给定的文件描述符epfd指向的epoll实例,op是添加事件的类型,fd是目标文件描述符。

有效的op值有以下几种:

  • EPOLL_CTL_ADD 在epfd中注册指定的fd文件描述符并能把eventfd关联起来。
  • EPOLL_CTL_MOD 改变*** fdevetn***之间的联系。
  • EPOLL_CTL_DEL 从指定的epfd中删除fd文件描述符。在这种模式中event是被忽略的,并且为可以等于NULL。

event这个参数是用于关联制定的fd文件描述符的。它的定义如下:

typedef union epoll_data {
    void        *ptr;
    int          fd;
    uint32_t     u32;
    uint64_t     u64;
} epoll_data_t;

struct epoll_event {
uint32_t events; /* Epoll events /
epoll_data_t data; /
User data variable */
};

events这个参数是一个字节的掩码构成的。下面是可以用的事件:

  • EPOLLIN - 当关联的文件可以执行 read ()操作时。
  • EPOLLOUT - 当关联的文件可以执行 write ()操作时。
  • EPOLLRDHUP - (从 linux 2.6.17 开始)当socket关闭的时候,或者半关闭写段的(当使用边缘触发的时候,这个标识在写一些测试代码去检测关闭的时候特别好用)
  • EPOLLPRI - 当 read ()能够读取紧急数据的时候。
  • EPOLLERR - 当关联的文件发生错误的时候,epoll_wait() 总是会等待这个事件,并不是需要必须设置的标识。
  • EPOLLHUP - 当指定的文件描述符被挂起的时候。epoll_wait() 总是会等待这个事件,并不是需要必须设置的标识。当socket从某一个地方读取数据的时候(管道或者socket),这个事件只是标识出这个已经读取到最后了(EOF)。所有的有效数据已经被读取完毕了,之后任何的读取都会返回0(EOF)。
  • EPOLLET - 设置指定的文件描述符模式为边缘触发,默认的模式是水平触发。
  • EPOLLONESHOT - (从 linux 2.6.17 开始)设置指定文件描述符为单次模式。这意味着,在设置后只会有一次从epoll_wait() 中捕获到事件,之后你必须要重新调用 epoll_ctl() 重新设置。

返回值:如果成功,返回0。如果失败,会返回-1, errno将会被设置

有以下几种错误:

  • EBADF - epfd 或者 fd 是无效的文件描述符。
  • EEXIST - op是EPOLL_CTL_ADD,同时 fd 在之前,已经被注册到epoll中了。
  • EINVAL - epfd不是一个epoll描述符。或者fdepfd相同,或者op参数非法。
  • ENOENT - op是EPOLL_CTL_MOD或者EPOLL_CTL_DEL,但是fd还没有被注册到epoll上。
  • ENOMEM - 内存不足。
  • EPERM - 目标的fd不支持epoll。

2.3 等待epoll事件

#include <sys/epoll.h>

int epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout);

int epoll_pwait(int epfd, struct epoll_event events,
int maxevents, int timeout,
const sigset_t
sigmask);

epoll_wait 这个系统调用是用来等待epfd中的事件。events指向调用者可以使用的事件的内存区域。maxevents告知内核有多少个events,必须要大于0.

timeout这个参数是用来制定epoll_wait 会阻塞多少毫秒,会一直阻塞到下面几种情况:

  1. 一个文件描述符触发了事件。
  2. 被一个信号处理函数打断,或者timeout超时。

timeout等于-1的时候这个函数会无限期的阻塞下去,当timeout等于0的时候,就算没有任何事件,也会立刻返回。

struct epoll_event 如下定义:

typedef union epoll_data {
    void    *ptr;
    int      fd;
    uint32_t u32;
    uint64_t u64;
} epoll_data_t;

struct epoll_event {
uint32_t events; /* Epoll events /
epoll_data_t data; /
User data variable */
};

每次epoll_wait() 返回的时候,会包含用户在epoll_ctl中设置的events。

还有一个系统调用epoll_pwait ()。epoll_pwait()和epoll_wait ()的关系就像select()和 pselect()的关系。和pselect()一样,epoll_pwait()可以让应用程序安全的等待知道某一个文件描述符就绪或者捕捉到信号。

下面的 epoll_pwait () 调用:

ready = epoll_pwait(epfd, &events, maxevents, timeout, &sigmask);

在内部等同于:

pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
ready = epoll_wait(epfd, &events, maxevents, timeout);
pthread_sigmask(SIG_SETMASK, &origmask, NULL);

如果 sigmask为NULL, epoll_pwait()等同于epoll_wait()。

返回值:有多少个IO事件已经准备就绪。如果返回0说明没有IO事件就绪,而是timeout超时。遇到错误的时候,会返回-1,并设置 errno。

有以下几种错误:

  • EBADF - epfd是无效的文件描述符
  • EFAULT - 指针events指向的内存没有访问权限
  • EINTR - 这个调用被信号打断。
  • EINVAL - epfd不是一个epoll的文件描述符,或者maxevents小于等于0

3. 官方demo

#define MAX_EVENTS 10
struct epoll_event  ev, events[MAX_EVENTS];
int         listen_sock, conn_sock, nfds, epollfd;


/* Code to set up listening socket, ‘listen_sock’,

  • (socket(), bind(), listen()) omitted */

epollfd = epoll_create1( 0 );
if ( epollfd == -1 )
{
perror( “epoll_create1” );
exit( EXIT_FAILURE );
}

ev.events = EPOLLIN;
ev.data.fd = listen_sock;
if ( epoll_ctl( epollfd, EPOLL_CTL_ADD, listen_sock, &ev ) == -1 )
{
perror( “epoll_ctl: listen_sock” );
exit( EXIT_FAILURE );
}

for (;; )
{
nfds = epoll_wait( epollfd, events, MAX_EVENTS, -1 );
if ( nfds == -1 )
{
perror( “epoll_wait” );
exit( EXIT_FAILURE );
}

<span class="token keyword">for</span> <span class="token punctuation">(</span> n <span
            class="token operator">=</span> <span class="token number">0</span><span
            class="token punctuation">;</span> n <span class="token operator">&lt;</span> nfds<span
            class="token punctuation">;</span> <span class="token operator">++</span>n <span
            class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span> events<span
            class="token punctuation">[</span>n<span class="token punctuation">]</span><span
            class="token punctuation">.</span>data<span class="token punctuation">.</span>fd <span
            class="token operator">==</span> listen_sock <span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        conn_sock <span class="token operator">=</span> <span class="token function">accept</span><span
            class="token punctuation">(</span> listen_sock<span class="token punctuation">,</span>
                    <span class="token punctuation">(</span><span class="token keyword">struct</span> <span
            class="token class-name">sockaddr</span> <span class="token operator">*</span><span
            class="token punctuation">)</span> <span class="token operator">&amp;</span>local<span
            class="token punctuation">,</span> <span class="token operator">&amp;</span>addrlen <span
            class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> conn_sock <span
            class="token operator">==</span> <span class="token operator">-</span><span
            class="token number">1</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span> <span
            class="token string">"accept"</span> <span class="token punctuation">)</span><span
            class="token punctuation">;</span>
            <span class="token function">exit</span><span class="token punctuation">(</span> EXIT_FAILURE <span
            class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">setnonblocking</span><span class="token punctuation">(</span> conn_sock <span
            class="token punctuation">)</span><span class="token punctuation">;</span>
        ev<span class="token punctuation">.</span>events   <span class="token operator">=</span> EPOLLIN <span
            class="token operator">|</span> EPOLLET<span class="token punctuation">;</span>
        ev<span class="token punctuation">.</span>data<span class="token punctuation">.</span>fd  <span
            class="token operator">=</span> conn_sock<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">epoll_ctl</span><span
            class="token punctuation">(</span> epollfd<span
            class="token punctuation">,</span> EPOLL_CTL_ADD<span
            class="token punctuation">,</span> conn_sock<span class="token punctuation">,</span>
                <span class="token operator">&amp;</span>ev <span class="token punctuation">)</span> <span
            class="token operator">==</span> <span class="token operator">-</span><span
            class="token number">1</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">perror</span><span class="token punctuation">(</span> <span
            class="token string">"epoll_ctl: conn_sock"</span> <span class="token punctuation">)</span><span
            class="token punctuation">;</span>
            <span class="token function">exit</span><span class="token punctuation">(</span> EXIT_FAILURE <span
            class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span
            class="token punctuation">{</span>
        <span class="token function">do_use_fd</span><span class="token punctuation">(</span> events<span
            class="token punctuation">[</span>n<span class="token punctuation">]</span><span
            class="token punctuation">.</span>data<span class="token punctuation">.</span>fd <span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

}

4.完整可运行的DEMO

#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>

#define MAX_EVENT 20
#define READ_BUF_LEN 256

/**

  • 设置 file describe 为非阻塞模式

  • @param fd 文件描述

  • @return 返回0成功,返回-1失败

  • /
    static int make_socket_non_blocking <span

              class="token punctuation">(</span><span class="token keyword">int</span> fd<span
              class="token punctuation">)</span> <span class="token punctuation">{</span>
    

    int flags, s<span

              class="token punctuation">;</span>
    

    // 获取当前flag
    flags = fcntl<span

              class="token punctuation">(</span>fd<span class="token punctuation">,</span> F_GETFL<span
              class="token punctuation">,</span> <span class="token number">0</span><span
              class="token punctuation">)</span><span class="token punctuation">;</span>
    

    if (<span

              class="token operator">-</span><span class="token number">1</span> <span
              class="token operator">==</span> flags<span class="token punctuation">)</span> <span
              class="token punctuation">{</span>
      <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Get fd status"</span><span
              class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span
              class="token number">1</span><span class="token punctuation">;</span>
    

    }

    flags |= O_NONBLOCK;

    // 设置flag
    s = fcntl<span

              class="token punctuation">(</span>fd<span class="token punctuation">,</span> F_SETFL<span
              class="token punctuation">,</span> flags<span class="token punctuation">)</span><span
              class="token punctuation">;</span>
    

    if (<span

              class="token operator">-</span><span class="token number">1</span> <span
              class="token operator">==</span> s<span class="token punctuation">)</span> <span
              class="token punctuation">{</span>
      <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Set fd status"</span><span
              class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span
              class="token number">1</span><span class="token punctuation">;</span>
    

    }
    return 0<span

              class="token punctuation">;</span>
    

    }

int main() {
// epoll 实例 file describe
int epfd = 0;
int listenfd = 0;
int result = 0;
struct epoll_event ev, event[MAX_EVENT];
// 绑定的地址
const char * const local_addr = “192.168.0.45”;
struct sockaddr_in server_addr = { 0 };

listenfd <span class="token operator">=</span> <span class="token function">socket</span><span
            class="token punctuation">(</span>AF_INET<span class="token punctuation">,</span> SOCK_STREAM<span
            class="token punctuation">,</span> <span class="token number">0</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> listenfd<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Open listen socket"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span
            class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/* Enable address reuse */</span>
<span class="token keyword">int</span> on <span class="token operator">=</span> <span
            class="token number">1</span><span class="token punctuation">;</span>
<span class="token comment">// 打开 socket 端口复用, 防止测试的时候出现 Address already in use</span>
result <span class="token operator">=</span> <span class="token function">setsockopt</span><span
            class="token punctuation">(</span> listenfd<span class="token punctuation">,</span> SOL_SOCKET<span
            class="token punctuation">,</span> SO_REUSEADDR<span class="token punctuation">,</span> <span
            class="token operator">&amp;</span>on<span class="token punctuation">,</span> <span
            class="token keyword">sizeof</span><span class="token punctuation">(</span>on<span
            class="token punctuation">)</span> <span class="token punctuation">)</span><span
            class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    perror <span class="token punctuation">(</span><span class="token string">"Set socket"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>

server_addr<span class="token punctuation">.</span>sin_family <span class="token operator">=</span> AF_INET<span
            class="token punctuation">;</span>
inet_aton <span class="token punctuation">(</span>local_addr<span class="token punctuation">,</span> <span
            class="token operator">&amp;</span><span class="token punctuation">(</span>server_addr<span
            class="token punctuation">.</span>sin_addr<span class="token punctuation">)</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
server_addr<span class="token punctuation">.</span>sin_port <span class="token operator">=</span> <span
            class="token function">htons</span><span class="token punctuation">(</span><span
            class="token number">8080</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
result <span class="token operator">=</span> <span class="token function">bind</span><span
            class="token punctuation">(</span>listenfd<span class="token punctuation">,</span> <span
            class="token punctuation">(</span><span class="token keyword">const</span> <span
            class="token keyword">struct</span> <span class="token class-name">sockaddr</span> <span
            class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>server_addr<span
            class="token punctuation">,</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>server_addr<span class="token punctuation">)</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Bind port"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>
result <span class="token operator">=</span> <span class="token function">make_socket_non_blocking</span><span
            class="token punctuation">(</span>listenfd<span class="token punctuation">)</span><span
            class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>

result <span class="token operator">=</span> <span class="token function">listen</span><span
            class="token punctuation">(</span>listenfd<span class="token punctuation">,</span> <span
            class="token number">200</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Start listen"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 创建epoll实例</span>
epfd <span class="token operator">=</span> <span class="token function">epoll_create1</span><span
            class="token punctuation">(</span><span class="token number">0</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token number">1</span> <span class="token operator">==</span> epfd<span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Create epoll instance"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>

ev<span class="token punctuation">.</span>data<span class="token punctuation">.</span>fd <span
            class="token operator">=</span> listenfd<span class="token punctuation">;</span>
ev<span class="token punctuation">.</span>events <span class="token operator">=</span> EPOLLIN <span
            class="token operator">|</span> EPOLLET <span class="token comment">/* 边缘触发选项。 */</span><span
            class="token punctuation">;</span>
<span class="token comment">// 设置epoll的事件</span>
result <span class="token operator">=</span> <span class="token function">epoll_ctl</span><span
            class="token punctuation">(</span>epfd<span class="token punctuation">,</span> EPOLL_CTL_ADD<span
            class="token punctuation">,</span> listenfd<span class="token punctuation">,</span> <span
            class="token operator">&amp;</span>ev<span class="token punctuation">)</span><span
            class="token punctuation">;</span>

<span class="token keyword">if</span><span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
    <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"Set epoll_ctl"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span> <span
            class="token punctuation">;</span> <span class="token punctuation">;</span> <span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> wait_count<span class="token punctuation">;</span>
    <span class="token comment">// 等待事件</span>
    wait_count <span class="token operator">=</span> <span class="token function">epoll_wait</span><span
            class="token punctuation">(</span>epfd<span class="token punctuation">,</span> event<span
            class="token punctuation">,</span> MAX_EVENT<span class="token punctuation">,</span> <span
            class="token operator">-</span><span class="token number">1</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span
            class="token keyword">int</span> i <span class="token operator">=</span> <span
            class="token number">0</span> <span
            class="token punctuation">;</span> i <span class="token operator">&lt;</span> wait_count<span
            class="token punctuation">;</span> i<span class="token operator">++</span><span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">uint32_t</span> events <span class="token operator">=</span> event<span
            class="token punctuation">[</span>i<span class="token punctuation">]</span><span
            class="token punctuation">.</span>events<span class="token punctuation">;</span>
        <span class="token comment">// IP地址缓存</span>
        <span class="token keyword">char</span> host_buf<span class="token punctuation">[</span>NI_MAXHOST<span
            class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">// PORT缓存</span>
        <span class="token keyword">char</span> port_buf<span class="token punctuation">[</span>NI_MAXSERV<span
            class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token keyword">int</span> __result<span class="token punctuation">;</span>
        <span class="token comment">// 判断epoll是否发生错误</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span> events <span
            class="token operator">&amp;</span> EPOLLERR <span class="token operator">||</span> events <span
            class="token operator">&amp;</span> EPOLLHUP <span class="token operator">||</span> <span
            class="token punctuation">(</span><span class="token operator">!</span> events <span
            class="token operator">&amp;</span> EPOLLIN<span class="token punctuation">)</span><span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span
            class="token string">"Epoll has error\n"</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
            close <span class="token punctuation">(</span>event<span class="token punctuation">[</span>i<span
            class="token punctuation">]</span><span class="token punctuation">.</span>data<span
            class="token punctuation">.</span>fd<span class="token punctuation">)</span><span
            class="token punctuation">;</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span
            class="token keyword">if</span> <span class="token punctuation">(</span>listenfd <span
            class="token operator">==</span> event<span class="token punctuation">[</span>i<span
            class="token punctuation">]</span><span class="token punctuation">.</span>data<span
            class="token punctuation">.</span>fd<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
            <span class="token comment">// listen的 file describe 事件触发, accpet事件</span>

            <span class="token keyword">for</span> <span class="token punctuation">(</span> <span
            class="token punctuation">;</span> <span class="token punctuation">;</span> <span
            class="token punctuation">)</span> <span class="token punctuation">{</span> <span
            class="token comment">// 由于采用了边缘触发模式,这里需要使用循环</span>
                <span class="token keyword">struct</span> <span
            class="token class-name">sockaddr</span> in_addr <span class="token operator">=</span> <span
            class="token punctuation">{</span> <span class="token number">0</span> <span
            class="token punctuation">}</span><span class="token punctuation">;</span>
                socklen_t in_addr_len <span class="token operator">=</span> <span
            class="token keyword">sizeof</span> <span class="token punctuation">(</span>in_addr<span
            class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">int</span> accp_fd <span class="token operator">=</span> <span
            class="token function">accept</span><span class="token punctuation">(</span>listenfd<span
            class="token punctuation">,</span> <span class="token operator">&amp;</span>in_addr<span
            class="token punctuation">,</span> <span class="token operator">&amp;</span>in_addr_len<span
            class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> accp_fd<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    <span class="token function">perror</span><span class="token punctuation">(</span><span
            class="token string">"Accept"</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
                    <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                __result <span class="token operator">=</span> <span class="token function">getnameinfo</span><span
            class="token punctuation">(</span><span class="token operator">&amp;</span>in_addr<span
            class="token punctuation">,</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>in_addr<span class="token punctuation">)</span><span
            class="token punctuation">,</span>
                                       host_buf<span class="token punctuation">,</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>host_buf<span class="token punctuation">)</span> <span
            class="token operator">/</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>host_buf<span class="token punctuation">[</span><span
            class="token number">0</span><span class="token punctuation">]</span><span
            class="token punctuation">)</span><span class="token punctuation">,</span>
                                       port_buf<span class="token punctuation">,</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>port_buf<span class="token punctuation">)</span> <span
            class="token operator">/</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>port_buf<span class="token punctuation">[</span><span
            class="token number">0</span><span class="token punctuation">]</span><span
            class="token punctuation">)</span><span class="token punctuation">,</span>
                                       NI_NUMERICHOST <span class="token operator">|</span> NI_NUMERICSERV<span
            class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">!</span> __result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span
            class="token string">"New connection: host = %s, port = %s\n"</span><span
            class="token punctuation">,</span> host_buf<span
            class="token punctuation">,</span> port_buf<span class="token punctuation">)</span><span
            class="token punctuation">;</span>
                <span class="token punctuation">}</span>

                __result <span class="token operator">=</span> <span
            class="token function">make_socket_non_blocking</span><span
            class="token punctuation">(</span>accp_fd<span
            class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> __result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
                <span class="token punctuation">}</span>

                ev<span class="token punctuation">.</span>data<span class="token punctuation">.</span>fd <span
            class="token operator">=</span> accp_fd<span class="token punctuation">;</span>
                ev<span class="token punctuation">.</span>events <span class="token operator">=</span> EPOLLIN <span
            class="token operator">|</span> EPOLLET<span class="token punctuation">;</span>
                <span class="token comment">// 为新accept的 file describe 设置epoll事件</span>
                __result <span class="token operator">=</span> <span class="token function">epoll_ctl</span><span
            class="token punctuation">(</span>epfd<span class="token punctuation">,</span> EPOLL_CTL_ADD<span
            class="token punctuation">,</span> accp_fd<span class="token punctuation">,</span> <span
            class="token operator">&amp;</span>ev<span class="token punctuation">)</span><span
            class="token punctuation">;</span>

                <span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> __result<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    <span class="token function">perror</span><span class="token punctuation">(</span><span
            class="token string">"epoll_ctl"</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
                    <span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span
            class="token punctuation">{</span>
            <span class="token comment">// 其余事件为 file describe 可以读取</span>
            <span class="token keyword">int</span> done <span class="token operator">=</span> <span
            class="token number">0</span><span class="token punctuation">;</span>
            <span class="token comment">// 因为采用边缘触发,所以这里需要使用循环。如果不使用循环,程序并不能完全读取到缓存区里面的数据。</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span> <span
            class="token punctuation">;</span> <span class="token punctuation">;</span><span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
                ssize_t result_len <span class="token operator">=</span> <span class="token number">0</span><span
            class="token punctuation">;</span>
                <span class="token keyword">char</span> buf<span class="token punctuation">[</span>READ_BUF_LEN<span
            class="token punctuation">]</span> <span class="token operator">=</span> <span
            class="token punctuation">{</span> <span class="token number">0</span> <span
            class="token punctuation">}</span><span class="token punctuation">;</span>

                result_len <span class="token operator">=</span> <span class="token function">read</span><span
            class="token punctuation">(</span>event<span class="token punctuation">[</span>i<span
            class="token punctuation">]</span><span class="token punctuation">.</span>data<span
            class="token punctuation">.</span>fd<span class="token punctuation">,</span> buf<span
            class="token punctuation">,</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>buf<span class="token punctuation">)</span> <span
            class="token operator">/</span> <span class="token keyword">sizeof</span> <span
            class="token punctuation">(</span>buf<span class="token punctuation">[</span><span
            class="token number">0</span><span class="token punctuation">]</span><span
            class="token punctuation">)</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>

                <span class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">-</span><span class="token number">1</span> <span
            class="token operator">==</span> result_len<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>EAGAIN <span
            class="token operator">!=</span> errno<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                        perror <span class="token punctuation">(</span><span class="token string">"Read data"</span><span
            class="token punctuation">)</span><span class="token punctuation">;</span>
                        done <span class="token operator">=</span> <span class="token number">1</span><span
            class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">else</span> <span
            class="token keyword">if</span> <span class="token punctuation">(</span><span
            class="token operator">!</span> result_len<span class="token punctuation">)</span> <span
            class="token punctuation">{</span>
                    done <span class="token operator">=</span> <span class="token number">1</span><span
            class="token punctuation">;</span>
                    <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>

                <span class="token function">write</span><span class="token punctuation">(</span>STDOUT_FILENO<span
            class="token punctuation">,</span> buf<span class="token punctuation">,</span> result_len<span
            class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span
            class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">printf</span><span class="token punctuation">(</span><span
            class="token string">"Closed connection\n"</span><span class="token punctuation">)</span><span
            class="token punctuation">;</span>
                close <span class="token punctuation">(</span>event<span class="token punctuation">[</span>i<span
            class="token punctuation">]</span><span class="token punctuation">.</span>data<span
            class="token punctuation">.</span>fd<span class="token punctuation">)</span><span
            class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
close <span class="token punctuation">(</span>epfd<span class="token punctuation">)</span><span
            class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token number">0</span><span
            class="token punctuation">;</span>

}