title: WSAEWOULDBLOCK handling
body: <p>I have written a socket for a server in C++ CLI that is using winsock. The sockets are using async methods for sending, receiving and accepting connections. After implementing my socket in the production environment, the send function stops working giving me the error WSAEWOULDBLOCK. Out from my research on the net, this means the network buffer for socket IO is full or the networking is too busy to do my operation at this moment. However, I have not seen any specific solution which can address this problem. My temporary solution was to create a do-while loop around the WSASend function, making the thread sleep for X amount of MS and then try again. This resulted in far higher latency than the previous socket (.NET socket class) and large lag spikes. </p>
  
  <p>My code for sending data is as following:</p>
  
  <pre><code>void Connectivity::ConnectionInformation::SendData(unsigned char data[], const int length)
  {
      if (isClosed || sendError)
          return;
  
      Monitor::Enter(this-&gt;syncRoot);
  
      try
      {
          sendInfo-&gt;buf = (char*)data;
          sendInfo-&gt;len = length;
  
          do
          {
              state = 0;
              if (WSASend(connection, sendInfo, 1, bytesSent, 0, NULL, NULL) == SOCKET_ERROR)
              {
                  state = WSAGetLastError();
                  if (state == WSAEWOULDBLOCK)
                  {
                      Thread::Sleep(SleepTime);
                      //Means the networking is busy and we need to wait a bit for data to be sent
                      //Might wanna decrease the value since this could potentially lead to lagg
                  }
                  else if (state != WSA_IO_PENDING)
                  {
                      this-&gt;sendError = true;
                      //The send error bool makes sure that the close function doesn't get called
                      //during packet processing which could cause a lot of null reffernce exceptions.
                  }
              }
          }
          while (state == WSAEWOULDBLOCK);
      }
      finally
      {
          Monitor::Exit(this-&gt;syncRoot);
      }
  }
  </code></pre>
  
  <p>Is there a way to use for example the WSAEventSelect method in order to get a callback when I am able to send data? Out from the documentation on MSDN, the wait for data method could also get stuck in this error. Anyone got any solutions for getting around this?</p>