【问题标题】:How to deal with EPOLLERR and EPOLLHUP?如何处理 EPOLLERR 和 EPOLLHUP?
【发布时间】:2014-06-09 11:10:46
【问题描述】:

我阅读了libevent epoll中的代码,代码如下:

       if (what & (EPOLLHUP|EPOLLERR)) {
            ev = EV_READ | EV_WRITE;
        } else {
            if (what & EPOLLIN)
                ev |= EV_READ;
            if (what & EPOLLOUT)
                ev |= EV_WRITE;
            if (what & EPOLLRDHUP)
                ev |= EV_CLOSED;
        }

据我了解,当 EPOLLERR 或 EPOLLHUP 发生时,应该关闭连接。但是在上面的代码中,当遇到 EPOLLHUP|EPOLLERR 时,事件掩码设置为 EV_READ | EV_WRITE。所以我的问题是:

  • 是什么导致 EPOLLERR 和 EPOLLHUP 发生?
  • 当EPOLLERR和EPOLLHUP发生时,程序在事件句柄函数中应该做什么?并请详细解释其背后的原因。

提前致谢!

【问题讨论】:

    标签: linux network-programming


    【解决方案1】:

    是什么让 EPOLLERR 和 EPOLLHUP 发生?

    man epoll_ctl

       EPOLLERR
              Error condition happened on the associated file descriptor.
              epoll_wait(2) will always wait for this event; it is not
              necessary to set it in events.
    
       EPOLLHUP
              Hang up happened on the associated file descriptor.
              epoll_wait(2) will always wait for this event; it is not
              necessary to set it in events.
    

    当 EPOLLERR 和 EPOLLHUP 发生时,程序在 事件句柄函数?

    在这种情况下,当 libevent 传递事件 EV_READ | EV_WRITE 时,回调函数调用 e。 G。 recv(),当对等方执行有序关闭 (EPOLLHUP) 时可能返回 0,如果发生错误 (EPOLLERR),则返回 -1;然后程序可能会清理连接,如果它是客户端,可能会重新建立它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2016-07-28
      • 1970-01-01
      相关资源
      最近更新 更多