title: Getting this "10038 wsa socket" error in my code
body: <p>I'm currently trying to implement a class to create and use TCP connections.</p>
  <p>(I'm quite new in c++ so it's possible that I forgot something obvious, even though I've been seriously checking my code)</p>
  <p>Note that later I'll be implementing a second class to handle new connections so there are some parts of the code that are incomplete/arranged and I hope that this won't disturb you while trying to understand it.</p>
  <p>Here's my problem:</p>
  <p>The server and the client both seem to work fine (I'm always checking on my listen, bind, connect functions: no error) and I'm apparently able to send data with the client but when I try to read it from the server, I get WSA error 10038 (which means that I'm executing the recv() on an invalid socket). I've been looking it up for a few days now and can't seem to find my mistake. Besides the tests in the code, I've used &quot;netstat -an -p tcp&quot; to check the state of the connection and everything appears fine but the server just won't receive the data.</p>
  <p><em>NB: I'm using Visual Studio 6 (Yup, it's old, but it's mandatory so...) and winsock, not winsock2.</em></p>
  <p>If I forgot something just ask, I'm trying to be as complete as possible but it's not impossible to forget something.</p>
  <p>Here are my source codes, I tried to cut it to the essential but I'm afraid that the mistake can be pretty much anywhere:</p>
  <p><strong>CmTcpTransport.cpp</strong></p>
  <pre><code>/**
  Initializes the TCP connection (socket) in CLIENT or SERVER mode.
  It does not connect yet.
  
  @param strIPAddress: IP Address to reach
         nPort: Port to connect to
         hSocket: client Socket if SERVER mode
         nTcpMode: mode for this connexion SERVER(1) or CLIENT(2)      
         
  @return an int to get the exit point of the function
  */
  int CmTcpTransport::initialize(std::string strIPAddress, unsigned int nPort,         SOCKET hSocket, int nTcpMode)
  {
  m_nTcpMode = nTcpMode; 
  
  cout &lt;&lt; &quot;Creating TCP connexion...&quot; &lt;&lt; endl &lt;&lt; endl;
  
  if (nTcpMode == 1) // SERVER
  {
  
      serveraddr.sin_family = AF_INET; // address family Internet
      serveraddr.sin_port = htons (nPort); // Port to connect on
      serveraddr.sin_addr.s_addr = inet_addr (strIPAddress.c_str()); // Target IP
  
      cout &lt;&lt; &quot;SERVER: Retrieving socket information...&quot; &lt;&lt; endl; 
              
      if (hSocket == INVALID_SOCKET)
      {
          return 102; // The received socket is invalid
          cout &lt;&lt; &quot;SERVER: Socket creation failed&quot; &lt;&lt; endl;
      }  
  
      serverSocket = hSocket;
  
      cout &lt;&lt; &quot;SERVER: Listening socket received&quot; &lt;&lt; endl;
  
      if (bind(serverSocket, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)
      {
          return 103; // Couldn't bind
          cout &lt;&lt; &quot;SERVER: Socket binding failed&quot; &lt;&lt; endl;
      }   
      else {
          cout &lt;&lt; &quot;SERVER: Binding successful&quot; &lt;&lt; endl;
      }
  
      if (listen(serverSocket, 1) == SOCKET_ERROR)
      {
          return 103; // Couldn't listen
          cout &lt;&lt; &quot;SERVER: Socket listening failed&quot; &lt;&lt; endl;
      }
      else {
          cout &lt;&lt; &quot;SERVER: Listening to socket&quot; &lt;&lt; endl;
      }
  
  }
  else // CLIENT
  {
      serveraddr.sin_family = AF_INET; // address family Internet
      serveraddr.sin_port = htons (nPort); // Port to connect on
      serveraddr.sin_addr.s_addr = inet_addr (strIPAddress.c_str()); // Target IP
  
      cout &lt;&lt; &quot;CLIENT: Creating client socket...&quot; &lt;&lt; endl;
  
      clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // CREATE CLIENT SOCKET
  
      if (clientSocket == INVALID_SOCKET)
      {
          return 104; // Couldn't create the socket
          cout &lt;&lt; &quot;CLIENT: Socket creation failed&quot; &lt;&lt; endl;
      }  
  
      cout &lt;&lt; &quot;CLIENT: Client socket created&quot; &lt;&lt; endl;
  
  
  }
  
  cout &lt;&lt; &quot;Initialization succeeded.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  return 105;
  }
  
  /**
  Creates the connexion to TCP
  
  @param none        
  @return an int to get the exit point of the function
  */
  int CmTcpTransport::connectTcp()
  {
  
  
  if (m_nTcpMode == 1) // SERVER
  {
      cout &lt;&lt; &quot;Waiting for connexion...&quot; &lt;&lt; endl;
      int clientLen(0);
      clientLen = sizeof(clientaddr);
      
  
      if ((clientSocket = accept(serverSocket, (SOCKADDR *)&amp;clientaddr, &amp;clientLen) == INVALID_SOCKET))
      {
          cout &lt;&lt; &quot;SERVER: Connexion failed.&quot; &lt;&lt; endl;
          return 202;; // Couldn't listen
      }
      else {
          cout &lt;&lt; &quot;SERVER: Connexion established.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
          if (clientSocket == SOCKET_ERROR) 
          {
              printf(&quot;socket became invalid after connexion:: %d\n&quot;, WSAGetLastError());
          }
          else {
                  cout &lt;&lt; &quot;SOCKET STILL VALID AND WORKING AFTER CONNEXION&quot; &lt;&lt; endl;
          }
          return 201;
      }
      
  
  }
  else // CLIENT
  {
      cout &lt;&lt; &quot;Connecting...&quot; &lt;&lt; endl;
      if (connect(clientSocket, (SOCKADDR *)&amp;serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)
      {
          cout &lt;&lt; &quot;CLIENT: Connexion failed.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
          return 202;; // Couldn't connect
      }
      else 
      {
          cout &lt;&lt; &quot;CLIENT: Connexion to server succeeded.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt;         endl;
      }
      return 203;
  }
  }
  
  /**
  Receives a buffer of bytes from the TCP
  
  @param buffer: buffer to store the data
         bufLen: size of the buffer
         
  @return nbChar: the result of the recv function
  if no error occurs, recv returns the number of bytes received
  else, it returns a value of SOCKET_ERROR
  */
  int CmTcpTransport::recvTcp(char *buffer, int bufLen)
  {
  if (clientSocket == SOCKET_ERROR) 
          {
              printf(&quot;socket became invalid before receive:: %d\n&quot;,  WSAGetLastError());
          }
          else {
                  cout &lt;&lt; &quot;SOCKET STILL VALID AND WORKING BEFORE RECEIVE&quot; &lt;&lt;         endl;
          }
          
  int nbChar = recv(clientSocket, buffer, bufLen, 0);
  if (nbChar == SOCKET_ERROR)
  {
      printf(&quot;recv failed: %d\n&quot;, WSAGetLastError());
      cout &lt;&lt; &quot;No data received.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
      return nbChar;
  }
  else {
      buffer[nbChar]=0; // Gestion de la taille du buffer
      cout &lt;&lt; &quot;Data received: &quot; &lt;&lt; buffer &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
      return nbChar;
  }
  }
  /**
  Sends a buffer of bytes to the TCP
  
  @param buffer: buffer containing the data
         bufLen: size of the buffer
         
  @return nbChar: the result of the send function
  if no error occurs, recv returns the number of bytes sent
  else, it returns a value of SOCKET_ERROR
  */
  int CmTcpTransport::sendTcp(char *buffer, int bufLen)
  {
  
  int nbChar = send(clientSocket, buffer, bufLen,0);
  if (nbChar == SOCKET_ERROR)
  {
      
      cout &lt;&lt; &quot;No data sent.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
      return nbChar;
  }
  else {
      cout &lt;&lt; &quot;Data sent: &quot; &lt;&lt; buffer &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
      return nbChar;
  }
  }
  </code></pre>
  <p><strong>server main.cpp</strong></p>
  <pre><code>int main(int argc, char* argv[])
  {
  // ----------------------- WINSOCK ----------------------------
  
  cout &lt;&lt; &quot;Initializing winsock library...&quot; &lt;&lt; endl;
  
  // Start up Winsock
  WSADATA wsadata;
  
  int error = WSAStartup(0x101, &amp;wsadata);
  
  // Error ?
  if (error) 0;
  
  // Try Winsock Version ?
  if (wsadata.wVersion != 0x101)
  {
      WSACleanup(); // Clean up Winsock
      return 101;
  }
  
  cout &lt;&lt; &quot;Initialization successful&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  
  
  // ----------------------- SERVER ---------------------------
  
  char buffer[3000];
  
  int testRecv; // return of the recv function
  
  cout &lt;&lt; &quot;SERVER&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  
  // Simulate the activity of the TcpListener transfering a listening Socket for the client connexion
  SOCKET testSocket;
  testSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); 
  
  // Creating a new TCP transport
  CmTcpTransport server;
  
  // Initialize and connect the TCP transport
  server.initialize(&quot;127.0.0.1&quot;, 10000, testSocket, TTCP_MODE_SERVER);
  
  server.connectTcp();
  
  // Try to receive data
  testRecv = server.recvTcp(buffer, sizeof(buffer));
  
  cout &lt;&lt; testRecv &lt;&lt; endl;
  
  // Disconnect
  server.disconnectTcp();
  
  
  return 0;
  }
  </code></pre>
  <p><strong>client main.cpp</strong></p>
  <pre><code>int main(int argc, char* argv[])
  {
  
  // ----------------------- WINSOCK ----------------------------
  
  cout &lt;&lt; &quot;Initializing winsock library...&quot; &lt;&lt; endl;
  
  // Start up Winsock
  WSADATA wsadata;
  
  int error = WSAStartup(0x0202, &amp;wsadata);
  
  // Error ?
  if (error) 0;
  
  // Try Winsock Version ?
  if (wsadata.wVersion != 0x0202)
  {
      WSACleanup(); // Clean up Winsock
      return 101;
  }
  
  cout &lt;&lt; &quot;Initialization successful&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  
  
  // ----------------------- CLIENT ---------------------------
  
  char buffer[10000];
  
  cout &lt;&lt; &quot;CLIENT&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
  
  // this Socket won't be used as the TCP transport is in client mode
  SOCKET test;
  test = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  
  // Create, initialize and connect the client TCP transport
  CmTcpTransport client;
  
  client.initialize(&quot;127.0.0.1&quot;, 10000, test, TTCP_MODE_CLIENT);
  
  client.connectTcp();
  
  // Copy the data in the buffer and send it
  strcpy(buffer, &quot;Test donnees&quot;);
  
  client.sendTcp(buffer, sizeof(buffer));
  
  client.disconnectTcp();
  
  return 0;
  }
  </code></pre>
  <p>And here are screenshots of my consoles when I run both mains:</p>
  <p><a href="http://www.hostingpics.net/viewer.php?id=892752Consoles.jpg" rel="nofollow noreferrer">http://www.hostingpics.net/viewer.php?id=892752Consoles.jpg</a></p>
  <p>Thank you for your help, I realize this is a lot to read/process and I appreciate the effort !</p>