title: Only first few multithreaded clients can read response from socket
body: <p>I am building a client-server program in C using sockets.  Both my client and my server use a fixed number of threads to operate.  I tested with very few client and server threads at first (5 and 3) and everything seemed to work fine.  But now I tried to up the number of client threads to 500 (while the number of server thread stays at 3), but everything breaks.  The first hundred or so client can send their request and receive a response, but the others don't receive anything from the server.  </p>
  
  <p>I'm working on a Debian Windows Subsystem if that changes anything.</p>
  
  <p>I have also tried upping the number of server thread to 300 but the problem still happens.</p>
  
  <p>Here is my (very) simplified code.</p>
  
  <h2>Client thread</h2>
  
  <pre><code>  int client_socket= socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(2018);
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    memset(&amp;addr.sin_zero, 0, sizeof(addr.sin_zero));
    connect(client_socket, (struct sockaddr *) &amp;addr, sizeof(addr));
  
    int response;
    int cid = 1;
  
    send(client_socket, &amp;cid, sizeof(int), 0);
  
    int len = read_socket(socket, &amp;response, sizeof(int), 1000);
    if (len == 0) {
        printf("No response");
    } else {
        printf("Response");
    }
    close(client_socket);
  </code></pre>
  
  <h2>Server thread</h2>
  
  <pre><code>int socket_fd, cid, len;
  while (1)
    {
      socket_fd =
          accept(socket_fd, (struct sockaddr *)&amp;thread_addr, &amp;socket_len);
      if (socket_fd&gt; 0) {
          int cid;
          int len = read_socket(socket_fd, &amp;cid, sizeof(cid), 1000);
          if (len == 0) {
            printf("Nothing");
          }
          send(socket_fd, &amp;cid, sizeof(int),0);
          close(socket_fd);
      }
    }
  </code></pre>
  
  <p>And here is my helper function <code>read_socket()</code></p>
  
  <pre class="lang-c prettyprint-override"><code>ssize_t read_socket(int sockfd, void *buf, size_t obj_sz, int timeout) {
    int ret;
    int len = 0;
  
    struct pollfd fds[1];
    fds-&gt;fd = sockfd;
    fds-&gt;events = POLLIN;
    fds-&gt;revents = 0;
  
    do {
      // wait for data or timeout
      ret = poll(fds, 1, timeout);
  
      if (ret &gt; 0) {
        if (fds-&gt;revents &amp; POLLIN) {
            ret = recv(sockfd, (char*)buf + len, obj_sz - len, 0);
            if (ret &lt; 0) {
              // abort connection
              perror("recv()");
              return -1;
            }
            len += ret;
        }
      } else {
        // TCP error or timeout
        if (ret &lt; 0) {
          perror("poll()");
        }
        break;
      }
    } while (ret != 0 &amp;&amp; len &lt; obj_sz);
    return ret;
  }
  </code></pre>
  
  <p>Like I said, some client can complete their execution with no problem, but a lot of them don't receive a response from the server.</p>