title: Socket c++: recv function returns -1
body: <p>I'm trying to code a simple android client communicating with a windows server.
  I'm using sockets to achieve that and up to now, I was testing everything with a Linux machine (android client and linux server). So far, everything was working. Now i'm trying to set it up on windows (the server) and I have of course noticed that I had to change my code a little (if not just the include ^^').
  Anyway, my problem is that my recv functions returns -1 while it should read something but it doesn't for some reason.</p>
  
  <p>Below is how I use my recv function (EDIT: the whole server code now):</p>
  
  <pre><code>int tcp_server::start_listening()
  {
  
      WORD wVersionRequested;
      WSADATA wsaData;
      int wsaerr;
      wVersionRequested = MAKEWORD(2, 2);
      wsaerr = WSAStartup(wVersionRequested, &amp;wsaData);
      if (wsaerr != 0)
      {
          printf("Server: The Winsock dll not found!\n");
          WSACleanup();
          return 0;
      }
  
     else
      {
          printf("Server: The Winsock2 dll found \n");
      }
  
      sSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      if(sSock == INVALID_SOCKET)
      {
          printf("Server: Error initializing socket!n");
          WSACleanup();
          return 0;
      }
  
  
      sockaddr_in service,client ;
      service.sin_family = AF_INET;
      service.sin_port = htons(port);
      service.sin_addr.s_addr = INADDR_ANY;
  
  
     if(bind(sSock,(SOCKADDR*)&amp;service,sizeof(service))==SOCKET_ERROR)
     {
         printf("Server: Error binding socket to portn");
         WSACleanup();
         return 0;
     }
  
     /* wait for incoming connections */
     if(listen(sSock,10)==SOCKET_ERROR)
         printf("listen(): Error listening on socket %d\n", WSAGetLastError());
     else
     {
        printf("Server: Waiting for connections...\n");
     }
  
     /* accept connections */
     AcceptSocket;
     printf("Server: Waiting for a client to connect...\n");
     AcceptSocket = accept(sSock, NULL, NULL);
     if (AcceptSocket == INVALID_SOCKET) {
         wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
         closesocket(sSock);
         WSACleanup();
         return 1;
     } else {
         wprintf(L"Client connected.\n");
         acceptConns();
     }
  
     closesocket(sSock);
     return 0;
  }
  int tcp_server::acceptConns()
  {
      sockaddr_in from;
      bool infinite = true ;
      int fromlen=sizeof(from);
      int readsize;
      char* message;
      char* clientmessage = "\0";
      string smatrix ;
      int ind ;
      string tok;
      int i = 0 ;
      int bytesSent ;
  
      float matrix[16] ;
      while( (readsize = recv(AcceptSocket, clientmessage, 2000, 0))!= 0 ){
          message = "ack";
          cout &lt;&lt; "Writing ACK" &lt;&lt; endl ;
          smatrix = clientmessage ;
          std::stringstream ss(smatrix);
  
          cout &lt;&lt; smatrix &lt;&lt; endl ;
          while(getline(ss, tok, ',') &amp;&amp; i &lt; 16 ){
              matrix[i] = static_cast&lt;float&gt;(::atof(tok.c_str()));
              i++ ;
          }
          i = 0 ;
          Sleep(1000);
      }
      if(readsize == 0 ){
          cerr &lt;&lt; "client disconnected" &lt;&lt; endl ;
      }
      cout &lt;&lt; "Socket closed" &lt;&lt; endl ;
      closesocket(sSock);
      WSACleanup();
  }
  </code></pre>
  
  <p>As for my android code: here it is:</p>
  
  <pre><code> while(closeConnection == false) {
          if (connected == true &amp;&amp; matrixupdated == true &amp;&amp; this.hasMatrixChanged()){
              try {
                  //= new BufferedReader(new InputStreamReader(java.lang.System.in));
                  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
                  String sentence = this.getStringFromMatrix();
                  outToServer.writeBytes(sentence + "\n");
                  outToServer.flush();
                  inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                  System.arraycopy(matrix, 0, previousMatrix, 0, 16);
                  inFromServer.read(serverMsg);
                  if(serverMsg.equals("ack") == true){
                      this.serverHasInput = false ;
                      Log.e("ACK", "ACK");
                      inFromServer.read(serverMsg);       //Wait for the "ok" message from server
                      if(serverMsg.equals("ok") == false){
                          Log.e("ERROR IN COM W/ SERV"," DID NOT RECEIVE OK");
                      }
                  }
                  else{
                      this.serverHasInput = true ;
                  }
                  //Log.d("SERVERMSG", "DONE WAITING");
  
              }catch (Exception e) {
                  Log.e("ClientActivity", "S: Error", e);
              }
              this.matrixupdated = false ;
          }
  
      }
      try {
          outToServer.flush();
          outToServer.close();
          inFromServer.close();
          clientSocket.close();
      }
  </code></pre>
  
  <p>Finally the output I get in the console is unfortunately </p>
  
  <blockquote>
    <p>client disconnected</p>
  </blockquote>
  
  <p>I'm guessing this is probably some really dumb stuff, but i've been trying to see on MS website and the documentation of recv why it would give me -1 but I can't get my hands on a explanation.</p>
  
  <p>Thanks in advance for the help provided :).</p>