title: check on socket with select() returns 0 when data is present
body: <p>I've had my socket class working for a while now, but I wanted to add a timeout using select().  Seems pretty straight forward but I always have 0 returned from select().  I've even removed the select() check so it reads data regardless of select() and the data gets read, but select() still reports that data is not present.  Any clue on how to get select() to stop lying to me?  I've also set the socket to non-blocking.  Thanks.</p>
  
  <p>Code:</p>
  
  <pre><code>char buf [ MAXRECV + 1 ];
  
  s = "";
  
  memset ( buf, 0, MAXRECV + 1 );
  
  
  struct timeval tv;
  int retval;
  
  fd_set Sockets;
  FD_ZERO(&amp;Sockets);
  FD_SET(m_sock,&amp;Sockets);
  
  // Print sock int for sainity
  std::cout &lt;&lt; "\nm_sock:" &lt;&lt; m_sock &lt;&lt; "\n";
  
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  
  retval = select(1, &amp;Sockets, NULL, NULL, &amp;tv);
  std::cout &lt;&lt; "\nretval is :[" &lt;&lt; retval &lt;&lt; "]\n\n";
  
  // Check
  if (FD_ISSET(m_sock,&amp;Sockets))
    std::cout &lt;&lt; "\nFD_ISSET(m_sock,&amp;Sockets) is true\n\n";
  else
    std::cout &lt;&lt; "\nFD_ISSET(m_sock,&amp;Sockets) is false\n\n";
  
  // If error occurs
  if (retval == -1)
  {
    perror("select()");
    std::cout &lt;&lt; "\nERROR IN SELECT()\n";
  }
  // If data present
  else if (retval)
  {
    std::cout &lt;&lt; "\nDATA IS READY TO BE READ\n";
    std::cout &lt;&lt; "recv ( m_sock, buf, MAXRECV, 0)... m_sock is " &lt;&lt; m_sock &lt;&lt; "\n";
    int status = recv ( m_sock, buf, MAXRECV, 0 );
  
    if ( status == -1 )
    {
        std::cout &lt;&lt; "status == -1   errno == " &lt;&lt; errno &lt;&lt; "  in Socket::recv\n";
        return 0;
    }
    else if ( status == 0 )
    {
        return 0;
    }
    else
    {
        s = buf;
        return status;
    }
  }
  // If data not present
  else
  {
  std::cout &lt;&lt; "\nDATA WAS NOT READY, TIMEOUT\n";
  return 0;
  }
  </code></pre>