title: C++ Socket recv mixed messages
body: <p>Hello I am having a problem with a socket server and client.</p>
  
  <p>The problem is that  the messages get mixed up when I send them really fast. When I send them lets say 1 message per second everything runs good, but when I send them 1 message per 40ms they get mixed up.</p>
  
  <p>here is my code for receiving:</p>
  
  <pre><code>    std::string* AteneaClient::readSocket () {
  
      std::string finalString = std::string("");
      int size = MSG_SIZE;
  
      bool receiving = true;
      int timesBufferInc=0;
      while (receiving) {
          std::string temporalString;
  
          //create an empty buffer
          char* RCV_BUFFER = (char*) malloc (size* sizeof(char));
          for(int i=0;i&lt;size;i++){
              RCV_BUFFER[i]=' ';
          }
          RCV_BUFFER[size-1]='\0';
  
          int result =  recv(sock,RCV_BUFFER,size-1,NULL);
          if ( result== SOCKET_ERROR ) {
              free(RCV_BUFFER);
              return NULL;
          }
          else if(result&lt;size-1){
              receiving=false;
          }
  
          temporalString = std::string(RCV_BUFFER);
          finalString+=temporalString;
      }
      return new std::string(finalString);
  }
  </code></pre>
  
  <p>and here is my code for sending:</p>
  
  <pre><code>    int sendThread(void* data){
      SND_THREAD_DATA* parameters =(SND_THREAD_DATA*)data;
      SOCKET* individualSocket = parameters-&gt;individualSocket;
      std::string * message = parameters-&gt;message;
  
      char RCV_BUFFER[MSG_SIZE];
      std::string converter;
      std::cout &lt;&lt;"(!)Thread: Iniciando sendThread Individual.."&lt;&lt;std::endl;
      SOCKET default_socket = *individualSocket;
  
      bool running=true;
  
      while(running){
  
  
          int length=message-&gt;length();
          char *cstr = new char[length + 1];
          strcpy(cstr, message-&gt;c_str());
          if(::send(*individualSocket,cstr,length + 1,NULL)==SOCKET_ERROR){
              logSendError();
              running=false;
          }
          delete cstr;
          Sleep(SLEEPTIME);
      }
  
  }
  </code></pre>
  
  <p>and here is the code when I set up the socket:</p>
  
  <pre><code>    void AteneaClient::startUp(){
      int iResult = 0;
      iResult = WSAStartup(MAKEWORD(2, 2), &amp;WinSockData);
      if (iResult != NO_ERROR) {
          wprintf(L"(!)Main:WSAStartup() failed with error: %d\n", iResult);
          return;
      }
  
      ADDR.sin_addr.s_addr= inet_addr(IP);
      ADDR.sin_family = AF_INET;
      ADDR.sin_port = htons(PORT);
      sock = socket(AF_INET,SOCK_STREAM,0);
      running=true;
  }
  </code></pre>
  
  <p>Anyone has any idea why socket messages get mixed up?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>this is my current receive method with the improvements from Maxim comments:</p>
  
  <pre><code>    std::string* AteneaClient::readSocket () {
  
      int HEADER_SIZE=4;
      std::string finalString = std::string("");
      int sizeFirstBuffer = HEADER_SIZE*sizeof(char);
      char* RCV_BUFFER=(char*) malloc(sizeFirstBuffer+1);
  
      //clean new buffer
      for(int i=0;i&lt;HEADER_SIZE;i++){
          RCV_BUFFER[i]=' ';
          }
      RCV_BUFFER[sizeFirstBuffer]='\0';
  
  
  
      int result =  recv(sock,RCV_BUFFER,sizeFirstBuffer,NULL);
      //cout &lt;&lt; "The Size to read is:" &lt;&lt;RCV_BUFFER &lt;&lt; endl;
  
  
      //now i create a buffer with that size
      int sizeThatIHaveToRead= atoi(RCV_BUFFER);
      int sizeSecondBuffer = sizeThatIHaveToRead*sizeof(char);
      char* RCV_BUFFER_SECOND=(char*) malloc(sizeSecondBuffer+1);
  
      //clean new buffer
      for(int i=0;i&lt;sizeSecondBuffer;i++){
          RCV_BUFFER_SECOND[i]=' ';
          }
      RCV_BUFFER_SECOND[sizeSecondBuffer]='\0';
  
  
  
      result =  recv(sock,RCV_BUFFER_SECOND,sizeSecondBuffer,NULL);
      //cout &lt;&lt; "RCV_BUFFER_SECOND:" &lt;&lt;RCV_BUFFER_SECOND &lt;&lt; endl;
      finalString+=RCV_BUFFER_SECOND;
      return new std::string(finalString);
  }
  </code></pre>