title: TCP Timeout using poll
body: <p>I am currently trying to add a timeout to a method which opens up a tcp connection. I am roughly using the guide found <a href="http://developerweb.net/viewtopic.php?pid=13486" rel="nofollow">here</a> except that I am trying to use <code>poll()</code> instead of select. However my call to <code>poll()</code> immediately returns notifying that the fd is ready for write, although the connection is not open. Here is an reduced code example I produced:</p>
  
  <pre><code>#include &lt;cstring&gt;
  #include &lt;poll.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;cerrno&gt;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  int main() {
     struct addrinfo hints;
  
     ::memset( &amp;hints, 0, sizeof( hints ) );
     hints.ai_socktype = 0;
     hints.ai_family = AF_UNSPEC;
     hints.ai_protocol = 0;
     hints.ai_flags = AI_CANONNAME;
  
  
     struct addrinfo * info;
  
     if( ::getaddrinfo( "127.0.0.1", "49999", &amp;hints, &amp;info ) != 0 ) {
        std::cerr &lt;&lt; "Error: getaddrinfo" &lt;&lt; std::endl;
        exit( 1 );
     }
  
  
     int soc;
  
     if( (soc = ::socket( info-&gt;ai_family, info-&gt;ai_socktype, info-&gt;ai_protocol ) ) &lt; 0 ) {
        std::cerr &lt;&lt; "Erorr: socket" &lt;&lt; std::endl;
     }
  
     // Set mode to non-blocking for timeout handling
     int arg;
     if( (arg = ::fcntl( soc, F_GETFL, 0 )) &lt; 0 ) {
        std::cerr &lt;&lt; "Error: fcntl" &lt;&lt; std::endl;
        exit( 1 );
     }
  
     arg |= O_NONBLOCK;
     if( ::fcntl(soc, F_SETFL, arg) &lt; 0) {
        std::cerr &lt;&lt; "Error: fcntl" &lt;&lt; std::endl;
        exit( 1 );
     }
  
     int res = ::connect(soc,info-&gt;ai_addr,info-&gt;ai_addrlen);
  
     if( (res != 0) &amp;&amp; (errno != EINPROGRESS) ) {
        std::cerr &lt;&lt; "Error: connect" &lt;&lt; std::endl;
        exit( 1 );
     }
  
     pollfd pfd;
     pfd.fd = soc;
     pfd.events = POLLOUT;
  
     res = ::poll( &amp;pfd, 1, 50000 );
  
     if( res &lt; 0 ) {
        std::cerr &lt;&lt; "Error: poll" &lt;&lt; std::endl;
        exit( 1 );
     }
     else if( res == 0 ) {
        std::cerr &lt;&lt; "Error: poll" &lt;&lt; std::endl;
        exit( 1 );
     }
  
     // Set blocking mode again
     if( (arg = ::fcntl(soc, F_GETFL, NULL)) &lt; 0) {
        std::cerr &lt;&lt; "Error: fcntl" &lt;&lt; std::endl;
        exit( 1 );
     }
     arg &amp;= (~O_NONBLOCK);
     if( ::fcntl(soc, F_SETFL, arg) &lt; 0) {
        std::cerr &lt;&lt; "Error: fcntl" &lt;&lt; std::endl;
        exit( 1 );
     }
  
     return 0;
  }
  </code></pre>
  
  <p>Since port <code>49999</code> is closed on my machine I expect an error. Instead the program finishes with a return value of <code>0</code>.</p>
  
  <p>I also tried to use the select example found in the link above. If I replace the call to <code>poll()</code> with the full example I get the following error message:</p>
  
  <p><code>Operation now in progress: Operation now in progres</code></p>
  
  <p>I tried to reduce the code using select, but when I reduce it, I get a correct <code>connection refused message</code>.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Note: The "Operation now in progress" message was due to an error in the error handling code on my side. Once I corrected this, I got the correct error message from "getsockopt()". This also explains why I was not able to reduce this example.</p>