title: Winsock nonblocking send() wait buffer. What is the correct method?
body: <p>I have some questions about when it is needed to store the data in the wait buffer (waiting for the FD_WRITE event).</p>
  
  <p>This is my send function (fixed):</p>
  
  <pre><code>bool MyClass::DataSend(char *buf, int len)
  {
      if (len &lt;=0 || m_Socket == INVALID_SOCKET) return false;
  
      if (m_SendBufferLen &gt; 0)
      {
          if ((m_SendBufferLen + len) &lt; MAX_BUFF_SIZE)
          {
              memcpy((m_SendBuffer + m_SendBufferLen), buf, len);
              m_SendBufferLen += len;
              return true;
          }
          else
          {
              // close the connection and log insuficient wait buffer size
              return false;
          }
      }
  
      int iResult;
      int nPosition = 0;
      int nLeft = len;
  
      while (true)
      {
          iResult = send(m_Socket, (char*)(buf + nPosition), nLeft, 0);
  
          if (iResult != SOCKET_ERROR)
          {
              if (iResult &gt; 0)
              {
                  nPosition   += iResult;
                  nLeft       -= iResult;
  
              }
              else
              {
                  // log 0 bytes sent
                  break;
              }
          }
          else
          {
              if (WSAGetLastError() == WSAEWOULDBLOCK)
              {
                  if ((m_SendBufferLen + nLeft) &lt; MAX_BUFF_SIZE)
                  {
                      // log data copied to the wait buffer
                      memcpy((m_SendBuffer + m_SendBufferLen), (buf + nPosition), nLeft);
                      m_SendBufferLen += nLeft;
                      return true;
                  }
                  else
                  {
                      // close the connection and log insuficient wait buffer size
                      return false;
                  }
              }
              else
              {
                  // close the connection and log winsock error
                  return false;
              }
          }
  
          if (nLeft &lt;= 0) break;
      }
  
      return true;
  }
  </code></pre>
  
  <p>My send (FD_WRITE event) function (fixed):</p>
  
  <pre><code>bool MyClass::DataSendEvent()
  {
      if (m_SendBufferLen &lt; 1) return true;
  
      int iResult;
      int nPosition = 0;
      int nLeft = m_SendBufferLen;
  
      while (true)
      {
          iResult = send(m_Socket, (char*)(m_SendBuffer + nPosition), nLeft, 0);
  
          if (iResult != SOCKET_ERROR)
          {
              if (iResult &gt; 0)
              {
                  nPosition   += iResult;
                  nLeft       -= iResult;
              }
              else
              {
                  // log 0 bytes sent
                  break;
              }
          }
          else
          {
              if (WSAGetLastError() == WSAEWOULDBLOCK)
              {
                  if (nPosition &gt; 0)
                  {
                      memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
                      m_SendBufferLen -= nPosition;
                  }
  
                  break;
              }
              else
              {
                  // close the connection and log winsock error
                  return false;
              }
          }
  
          if (nLeft &lt;= 0)
          {
              if (m_SendBufferLen == nPosition)
              {
                  m_SendBufferLen = 0;
                  break;
              }
              else
              {
                  memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
                  m_SendBufferLen -= nPosition;
                  nPosition   = 0;
                  nLeft       = m_SendBufferLen;
              }
          }
      }
  
      return true;
  }
  </code></pre>
  
  <p>Do I really need the <code>if (nPosition &gt; 0)</code> or not? How do I simulate this scenario? Is there possible send() in non-blocking mode send less bytes than the requested? If not, why using the while() looping?</p>
  
  <ul>
  <li>This is the final code (thanks to @user315052)</li>
  </ul>