title: Sending a long String over a Socket C++
body: <p>I'm having a strange issue with some basic client &lt;-> server communication using sockets. </p>
  
  <p>The server packages an array of structs containing player information by dumping the data into one long string using an <code>ostringstream</code> object and some some simple formatting with newlines and spaces, then sends that string via socket to the client to be displayed to the player.</p>
  
  <p>The issue arises when the "name" field in the one or more of the structs being packaged is longer than 4 characters; if this is the case every character after the 4th from each "name" field containing 5+ characters is duplicated on a single line at the end of the string AFTER* being sent to the client. </p>
  
  <p>*The packaged string always displays properly on the server side.</p>
  
  <p>Below is the relevant code and two screenshots of server and client terminal, one displaying the error, the other without.</p>
  
  <p><strong>SERVER CODE:</strong></p>
  
  <pre><code>struct leader_board_pos
  {
      string name;
      int score;
  };
  
  leader_board_pos leader_board[num_players]; 
  
  ....
  
  // Package and send the current leaderboard to the client 
  int i = 0;
  string leader_board_package;
  string dubspace = "  ";
  ostringstream oss;
  
  cout &lt;&lt; "Package leader board &gt; "; cin.ignore();
  
  leader_board_package = "Leader Board: \n";
  while(i &lt; num_leaders)
  {
      oss &lt;&lt; leader_board[i].name &lt;&lt; dubspace &lt;&lt; leader_board[i].score &lt;&lt; endl;
      leader_board_package += oss.str();
      oss.str(string());
      i++;
  }
  
  cout &lt;&lt; leader_board_package; cin.ignore();
  
  bytes_sent = send(clientSock, leader_board_package.c_str(), leader_board_package.length(), 0);
  
  if (bytes_sent != leader_board_package.length())
  {
      cout &lt;&lt; "Server Message: Communication error..." &lt;&lt; endl;
      return;
  }
  </code></pre>
  
  <p><strong>CLIENT CODE:</strong></p>
  
  <pre><code>const int MAX_BUFF_LENGTH = 4096;
  
  ...
  
  //Get and display Leaderboard
  int bytes_recv = 0;
  vector&lt;char&gt; leader_board_buffer(MAX_BUFF_LENGTH);
  string leader_board_package;
  
  do {
  
      bytes_recv = recv(sock, leader_board_buffer.data(), MAX_BUFF_LENGTH, 0);
  
      if (bytes_recv == -1)
      {
          cout &lt;&lt; "Communication error...";
          return 0;
      }
      else
      {
          leader_board_package.append(leader_board_buffer.begin(), leader_board_buffer.end());
      }
  
  } while (bytes_recv == MAX_BUFF_LENGTH);
  
  cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; leader_board_package; cin.ignore(); 
  </code></pre>
  
  <p><strong>The Screenshots, Relevant sections highlighted:</strong></p>
  
  <p><strong><a href="https://i.imgur.com/xjYbxos.png?1" rel="nofollow">Screenshot with Error</a></strong></p>
  
  <p><strong><a href="https://i.imgur.com/wOR2HT5.png?1" rel="nofollow">Screenshot without Error</a></strong></p>
  
  <p>Unless I'm missing something else I'm 99% sure the error is in the do-while receive loop on the client-side since the string displays appropriately when printed on the server-side. I can't for the love of me imagine what would cause such a specific error since it's just one long string being sent.</p>