title: read and write to same socket (TCP) using select
body: <p>We're writing a client and a server to do (what I thought was) pretty simple network communications.  Mulitple clients connect to the server which then is supposed to send the data back to all other clients.</p>
  
  <p>The server just sits in a blocking <code>select</code> loop waiting for traffic, and when it comes, sends the data to the other clients.  This seems to work just fine.</p>
  
  <p>The problem is the client.  In response to a read, it will sometimes want to do a write.</p>
  
  <p>However, I've found that if I use:</p>
  
  <pre><code> rv = select(fdmax + 1, &amp;master_list, NULL, NULL, NULL);
  </code></pre>
  
  <p>My code will block until there is new data to read.  But sometimes (asynchronously, from another thread) I'll have new data to write on the network communication thread. So, I want my select to wake up periodically and let me check if there are data to write, like:</p>
  
  <pre><code>if (select(....) != -1)
  {
    if (FD_SET(sockfd, &amp;master_list))
       // handle data or disconnect
    else
       // look for data to write and write() / send() those.
  }
  </code></pre>
  
  <p>I tried setting the select to poll mode (or ridiculously short timeouts) with:</p>
  
  <pre><code>// master list contains the sockfd from the getaddrinfo/socket/connect seq
  struct timeval t;
  memset(&amp;t, 0, sizeof t);
  rv = select(fdmax + 1, &amp;master_list, NULL, NULL, &amp;t);
  </code></pre>
  
  <p>but have found that then then client never gets any incoming data.</p>
  
  <p>I also tried setting the socket fd to be non-blocking, like:</p>
  
  <pre><code>fcntl(sockfd, F_SETFL, O_NONBLOCK);
  </code></pre>
  
  <p>but that doesn't solve the problem:</p>
  
  <ol>
  <li>if my client <code>select()</code> has no <code>struct timeval</code>, reading data works, but it never unblocks to let me look for writable data.</li>
  <li>if my client <code>select()</code> has a <code>timeval</code> to get it to poll, then it never signals that there are incoming data to read, and my app freezes thinking there is no network connection made (despite the fact that all other function calls have succeeded)</li>
  </ol>
  
  <p>Any pointers at all as to what I could be doing wrong? Is it not possible to do read-write on the same socket (I can't believe that to be true).  </p>
  
  <p>(EDIT: The correct answer, and thing that I remembered on the server but not on the client, is to have a second fd_set, and copy master_list before each call to select():</p>
  
  <pre><code>// declare and FD_ZERO read_fds:
  // put sockfd in master_list
  
  while (1)
  {
     read_fds = master_list;
     select(...);
  
     if (FD_ISSET(read_fds))
       ....
     else
       // sleep or otherwise don't hog cpu resources
  }
  </code></pre>
  
  <p>)</p>