title: winsock recv gives 10014 error
body: <p>I'll start with the code:</p>
  
  <pre><code>typedef std::vector&lt;unsigned char&gt; CharBuf;   
  static const int RCV_BUF_SIZE = 1024; 
  SOCKET m_socket = a connected and working socket;
  
  // ...
  
  CharBuf buf; // Declare buffer
  buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
  char* p_buf = reinterpret_cast&lt;char*&gt;(&amp;buf[0]); // change from unsigned char to char
  //char p_buf[RCV_BUF_SIZE];
  
  int ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0); // Does not work
  
  for (int i=0; i&lt;RCV_BUF_SIZE; ++i) // Works (does not crash, so the buffer is ok)
      char c = p_buf[i];
  
  //...
  </code></pre>
  
  <p>Now when I run this code ret becomes -1 and WSAGetLastError() returns 10014 which means the pointer is bad.<br>
  However I can't see why this shouldn't work? If I comment out the reinterpret_cast line and use the line below it works!<br>
  It could be argued that reinterpret_cast is risky, but I think it should be ok as both unsigned char and signed char has the exact same size.<br>
  std::vectors should be safe to address directly in memory as far as I know as well.</p>
  
  <p>The funny part is that when I do the same thing with the same vector-type in send() it works! Send function:</p>
  
  <pre><code>void SendData(const CharBuf&amp; buf)
  {
      buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
      const char* p_buf = reinterpret_cast&lt;const char*&gt;(&amp;buf[0]); // change from unsigned char to char
  
      int ret = send(m_socket, p_buf, (int)buf.size(), 0); // Works
  }
  </code></pre>
  
  <p>As we see, no difference except CharBuf being const in this case, can that change anything?</p>
  
  <p>Why is recv() more sensitive than send()? How can recv() even know the pointer is invalid (which it obviously isn't)?? all it should see is a char array!</p>
  
  <p>As per request my whole receive function (bear in mind that I can't spell out every function in it, but I think they should be fairly self-explanatory.</p>
  
  <pre><code>bool TcpSocket::ReceiveData(CharBuf* pData)
  {
      if (!CheckInitialized("ReceiveData"))
          return false;
  
  
      if (m_status != CONNECTED_STAT)
      {
          AddToErrLog("Socket not connected", 1, "ReceiveData");
          return false;
      }
  
      int ret;
      pData-&gt;resize(RCV_BUF_SIZE);
      char* p_buf = reinterpret_cast&lt;char*&gt;(&amp;pData[0]);
  
      ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0);
  
      switch (ret)
      {
      case 0: // Gracefully closed
          AddToLog("Connection gracefully closed", 2);
          Shutdown(); // The connection is closed, no idea to keep running
  
          return true;
  
      case SOCKET_ERROR: // Error
          ret = WSAGetLastError();
          if (ret == 10004) // This indicates the socket was closed while we were waiting
              AddToLog("Socket was shut down while waiting for data", 1, "ReceiveData(1)");
          else
              AddToErrLog("Receive data failed with code: " + CStr(ret));
  
          AddToLog("Connection ended with error", 2);
  
          Shutdown();
          return false;
  
      default: // Normal operation
          pData-&gt;resize(ret); // Remove unused space
  
          return true;
      }
  }
  </code></pre>
  
  <p>Never mind. I found it while I was pasting the function. Like always, you find your error when you try to explain it for someone else :)
  I leave it up to the reader to figure out what was wrong, but I'll give &amp;pData[0] as a hint.
  Thanks for your help :D</p>