title: Epoll events(EPOLLET) only triggered once
body: <p>I'm trying to create an epoll_client &amp;&amp; an epoll_server over TCP, they send messages to each other.</p>
  
  <p>server side steps:</p>
  
  <ol>
  <li>create sockets, bind, listen</li>
  <li>create epoll instance, add the sockfd to epoll</li>
  <li>epoll_wait</li>
  </ol>
  
  <p>client side steps:</p>
  
  <ol>
  <li>create sockets, connet to server</li>
  <li>create epoll instance, add the sockfd to epoll</li>
  <li>epoll_wait</li>
  </ol>
  
  <p>now, client can connect to server, but only the EPOLLOUT event triggered once. Where should i put my read and write functions and should i modify the <code>events[n].events</code> when connection established?</p>
  
  <p>full code is here: <a href="https://github.com/linuxaged/epoll_example/blob/master/EpollServer.cpp" rel="nofollow noreferrer">EpollServer</a>, <a href="https://github.com/linuxaged/epoll_example/blob/master/EpollClient.cpp" rel="nofollow noreferrer">EpollClient</a></p>
  
  <pre><code>// client side
  for (;;)
  {
      nfds = epoll_wait( epollfd_, events, MAX_EVENTS, -1 );
      if ( nfds == -1 )
      {
          Print("--tcp client, epoll_wait error: %s\n", strerror(errno) );
          break;
      }
  
      Print( "--tcp epoll waiting %d ...\n", nfds );
      for ( n = 0; n &lt; nfds; ++n )
      {
          if ( events[n].data.fd == sockfd_ )
          {
  
              switch ( events[n].events )
              {
                  case EPOLLIN:
                  {
                      char buffer[1024];
                      for(;;)
                      {
                          ssize_t n_read = read( events[n].data.fd, buffer, sizeof(buffer) );
                          if ( n_read == -1 )
                          {
                              if ( errno == EWOULDBLOCK || errno == EAGAIN )
                              {
                                  Print("--tcp client, read finished\n");
                                  continue;
                              }
                              else
                              {
                                  Print("--tcp client read error!\n");
                                  break;
                              }
                          }
                          else if ( n_read == 0 )
                          {
                              Print("--tcp client read EOF\n");
                              break;
                          }
                          else
                          {
                              Print("--tcp client continue reading");
                              continue;
                          }
                      }
  
                      break;
                  }
                  case EPOLLOUT:
                  {
                      if ( !connected )
                      {
                          int sock_err = 0;
                          socklen_t socklen = sizeof(int);
                          int n_sockopt = getsockopt( sockfd_, SOL_SOCKET, SO_ERROR, &amp;sock_err, &amp;socklen );
                          if ( n_sockopt == 0 )
                          {
                              if ( sock_err == 0 )
                              {
                                  connected = true;
                                  Print( "--tcp client, connected to %s success!\n", ip );
                              }
                              else
                              {
                                  Print("--tcp client, failed to connect to %s\n", ip );
                              }
                          }
                          else
                          {
                              Print("--tcp client, failed to getsockopt %s\n", strerror( errno ) );
                          }
                      }
                      else
                      {
                          Print("--tcp client, ok to write to server\n");
                          if ( !sendBuffer_.empty() )
                          {
                              ssize_t written = 0;
                              ssize_t n_write = 0;
                              for(;;)
                              {
                                  n_write = write( sockfd_, sendBuffer_.data() + written, sendBuffer_.size() );
                                  if ( -1 == n_write )
                                  {
                                      if ( errno == EAGAIN || errno == EWOULDBLOCK )
                                      {
                                          Print("--tcp client, write finished\n");
                                          break;
                                      }
                                      else
                                      {
                                          Print("--tcp client failed to write\n");
                                          break;
                                      }
                                  }
                                  else
                                  {
                                      written += n_write;
                                      continue;
                                  }
                              }
                          }
                      }
  
                      break;
                  }
                  case EPOLLERR:
                      Print("--tcp client epoll error\n");
                      break;
                  case EPOLLHUP:
                      Print("--tcp epoll hup\n");
                      break;
                  default:
                      Print("--tcp client unkown event %04x\n", events[n].events );
                      break;
              }
  
          }
  
      }
  }
  
  //----------------------------------------------------------------
  
  // server side
  for (;;)
  {
      nfds = epoll_wait( epollfd_, events, MAX_EVENTS, EPOLL_TIMEOUT_MS );
      if ( nfds == -1 )
      {
          Print("--tcp server, epoll_wait error: %s\n", strerror(errno) );
          break;
      }
  
      Print( "--tcp epoll waiting %d...\n", nfds );
      for ( n = 0; n &lt; nfds; ++n )
      {
          if ( events[n].events &amp; EPOLLERR ||
                  events[n].events &amp; EPOLLHUP ||
                  !(events[n].events &amp; EPOLLIN) )
          {
              Print("--tcp server, epoll error\n");
              continue;
          }
          else if ( events[n].data.fd == sockfd_ )
          {
              sockaddr_in addr;
              socklen_t addrlen = sizeof( sockaddr_in );
              conn_sock = accept4( sockfd_, (struct sockaddr *) &amp;addr, &amp;addrlen, SOCK_NONBLOCK );
              if ( conn_sock == -1 )
              {
                  Print("--tcp server accept4 failed\n");
                  continue;
              }
              else
              {
                  char ipStr[INET_ADDRSTRLEN];
                  inet_ntop(PF_INET, &amp;(addr.sin_addr), ipStr, INET_ADDRSTRLEN);
                  Print("--tcp server, accept connection from %s\n", ipStr);
                  // add remote fd to epoll interest list
                  epoll_event ee{};
                  ee.events = EPOLLIN | EPOLLET;
                  ee.data.fd = conn_sock;
                  if ( epoll_ctl( epollfd_, EPOLL_CTL_ADD, conn_sock, &amp;ee ) == -1 )
                  {
                      Print("--tcp server, failed to epoll_ctl\n");
                      continue;
                  }
              }
          }
          else // remote fd
          {
              Print("--tcp server, read and write\n");
              char buffer[1024];
              for (;;)
              {
                  ssize_t n_read = read(events[n].data.fd, buffer, sizeof(buffer));
                  if (n_read == -1)
                  {
                      if (errno == EWOULDBLOCK || errno == EAGAIN)
                      {
                          Print("--tcp read finish\n");
                          break;
                      }
                      else
                      {
                          Print("--tcp server read error!\n");
                          break;
                      }
                  }
                  else if (n_read == 0)
                  {
                      Print("--tcp server read EOF, client closed\n");
                      break;
                  }
                  else
                  {
                      Print("--tcp server continue reading");
                      continue;
                  }
              }
  
              /**
               * write data in send buffer back to client
               */
              if (!sendBuffer_.empty())
              {
                  int written = 0;
                  for (;;)
                  {
                      ssize_t n_write = write(events[n].data.fd, sendBuffer_.data() + written,
                                              sendBuffer_.size() - written);
                      if (n_write == -1)
                      {
                          if (errno == EWOULDBLOCK || errno == EAGAIN)
                          {
                              Print("--tcp server, write finished\n");
                              break;
                          }
                          else
                          {
                              Print("--tcp server, write error %s\n", strerror(errno));
                              break;
                          }
                      } else if (n_write == 0)
                      {
                          Print("--tcp server, write finish\n");
                          break;
                      } else
                      {
                          written += n_write;
                      }
                  }
  
                  sendBuffer_.clear();
              }
  
  
          }
      }
  }
  </code></pre>