title: Socket select() blocks the second time around
body: <p>I have a simple server/client program that I'm working on. I'm using <code>select()</code> to wait for data to come in on a TCP socket before reading it in. When the data comes in, I use several <code>recv()</code> and <code>select()</code> calls to read in the chunks until I have it all. Then I loop back to the initial <code>select()</code> call and see if the client has anything else to send.</p>
  
  <pre><code>struct timeval timeoutCounter;
  fd_set readFileDescriptor;
  do {
      timeoutCounter.tv_sec = 30;
      timeoutCounter.tv_usec = 0;
      FD_ZERO(&amp;readFileDescriptor);
      FD_SET(socket, &amp;readFileDescriptor);
      cout &lt;&lt; "This line always prints, every iteration through the loop.\n";
      dataReady = select(socket+1,&amp;readFileDescriptor,NULL,NULL,&amp;timeoutCounter);
      cout &lt;&lt; "This line only prints the first time I call select()."
           &lt;&lt; "The second time it hangs before reaching this line.\n";
  
      // ... recv(), select(), recv(), select(), etc in a loop until I have all the data
      // send() a response to the client
  } while(dataReady &gt; 0);
  </code></pre>
  
  <p>I started off with all of this in a big, hard-to-read function, and it worked. Then I broke it out into a separate class from the one that <code>accept()</code>s the connection, and now its behavior is different. The first data set that the user sends comes in fine. But the client waits for a response from the server and then sends a second set of data to the socket. However, <code>select()</code> doesn't return after the client sends the second set of data; it blocks until it times out.</p>
  
  <p>I've already ruled the client out as being the problem; the packets are sent fine and at the appropriate time. I've also tried printing the socket file descriptor to prove that it does not change somewhere. Does anyone have any idea why this code might not work? What are the factors that might cause <code>select()</code> to block?</p>
  
  <p><strong>EDIT:</strong> It looks like my code runs fine on 32-bit machines, but fails on 64-bit machines. I still haven't solved the problem, but that narrows it down a good bit.</p>