title: How to detect a timed out client with poll()?
body: <p>I'm writing a non forking server, using poll() for multiple simultaneous connections. It works properly, except I have a problem with how to detect timeout the right way.</p>
  
  <p>Let's say I have the following code:</p>
  
  <pre><code>#define POLL_SIZE 512
  
  struct pollfd poll_set[POLL_SIZE];
  timeout = 60000; // 60 secs
  
  // setup server_sockfd with socket(), bind(), listen(), ...
  
  poll_set[0].fd = server_sockfd;
  poll_set[0].events = POLLIN;
  numfds = 1;
  
  while(1) {
     rc = poll(poll_set, numfds, timeout);
     if(rc == 0){
        // handle timeout
     }
  
     for(fd_index = 0; fd_index &lt; numfds; fd_index++) {
        if(poll_set[fd_index].revents &amp; POLLIN) {
           // accept new connection or handle established connections
        }
     }
  }
  </code></pre>
  
  <p>Let's assume, I have 15 clients connected, 14 clients are sending and receiving data, however one client is silent, no data to or from, ie. just occupying a socket on the server.</p>
  
  <p>Now, the problem is that poll() can't spot this one specific client, because all the other 14 clients are providing data, so poll() says, it's ok.</p>
  
  <p>How would you solve this problem by detecting this silent client, and close its connection?</p>
  
  <p>Currently, I have nothing better, then create a time_t lastseen[POLL_SIZE] array, and keep track of the timestamp of the given connection when either data is read from the client or sent to client.</p>
  
  <p>Then I use an alarm signal in every 60 seconds, and run through the lastseen array, compare their timestamp with the current timestamp, and tear down every connection being idle > 60 seconds.</p>
  
  <p>Or perhaps a thread could do the same to avoid signaling. What do you suggest to solve the problem?</p>
  
  <p>(Note that I experimented with libevent, and it's very nice. However, I had to abandon it, because I couldn't find support to add SSL/TLS to an already connected socket. Think of STARTTLS)</p>