title: C++ non blocking socket select send too slow?
body: <p>I have a program that maintains a list of "streaming" sockets. These sockets are configured to be non-blocking sockets. </p>
  
  <p>Currently, I have used a list to store these streaming sockets. I have some data that I need to send to all these streaming sockets hence I used the iterator to loop through this list of streaming sockets and calling the send_TCP_NB function below:</p>
  
  <p>The issue is that my own program buffer that stores the data before sending to this send_TCP_NB function slowly decreases in free size indicating that the send is slower than the rate at which data is put into the program buffer. The rate at which the program buffer is about 1000 data per second. Each data is quite small, about 100 bytes.</p>
  
  <p>Hence, i am not sure if my send_TCP_NB function is working efficiently or correct?</p>
  
  <pre><code>int send_TCP_NB(int cs, char data[], int data_length) {
  
      bool sent = false;
      FD_ZERO(&amp;write_flags);      // initialize the writer socket set
      FD_SET(cs, &amp;write_flags);   // set the write notification for the socket based on the current state of the buffer
      int status;
      int err;
  
      struct timeval waitd;       // set the time limit for waiting
      waitd.tv_sec = 0;
      waitd.tv_usec = 1000;
  
      err = select(cs+1, NULL, &amp;write_flags, NULL, &amp;waitd);
      if(err==0)
      {
          // time limit expired
          printf("Time limit expired!\n");
          return 0;   // send failed
      }
      else
      {
          while(!sent)
          {
                  if(FD_ISSET(cs, &amp;write_flags))
                  {
                       FD_CLR(cs, &amp;write_flags);
                       status = send(cs, data, data_length, 0);
                       sent = true;
                  }
           }
  
           int nError = WSAGetLastError();
           if(nError != WSAEWOULDBLOCK &amp;&amp; nError != 0)
           {      
                printf("Error sending non blocking data\n");
                return 0;
           }
           else
           {
                if(nError == WSAEWOULDBLOCK)
                {
                      printf("%d\n", nError);
                }
                return 1;
            }
         }
  }
  </code></pre>