title: How to deal with EPOLLERR and EPOLLHUP?
body: <p>I read the code in libevent epoll, here is the code:</p>
  
  <pre><code>       if (what &amp; (EPOLLHUP|EPOLLERR)) {
              ev = EV_READ | EV_WRITE;
          } else {
              if (what &amp; EPOLLIN)
                  ev |= EV_READ;
              if (what &amp; EPOLLOUT)
                  ev |= EV_WRITE;
              if (what &amp; EPOLLRDHUP)
                  ev |= EV_CLOSED;
          }
  </code></pre>
  
  <p>To my understanding, when EPOLLERR or EPOLLHUP happens, the connection should be closed. But in the above code, when EPOLLHUP|EPOLLERR encounters, the event mask is set to EV_READ | EV_WRITE. So my question is :</p>
  
  <ul>
  <li>What makes EPOLLERR and EPOLLHUP happen?</li>
  <li>When EPOLLERR and EPOLLHUP happen, what should the program do in the event handle function? And please explain the reason behind it in detail. </li>
  </ul>
  
  <p>Thanks in advance!</p>