title: Sending a string over a network using sockets in C++
body: <p>I'm encountering issues sending a string over a TCP network where the string is sending additional characters that were not input.</p>
  
  <p>For example, when I input the following string...
  <a href="https://imgur.com/FmM7J7y" rel="nofollow noreferrer"><img src="https://i.imgur.com/FmM7J7y.jpg" title="source: imgur.com" /></a></p>
  
  <p>I get the following result on the receiving end.
  <a href="https://imgur.com/E2sGvmQ" rel="nofollow noreferrer"><img src="https://i.imgur.com/E2sGvmQ.jpg" title="source: imgur.com" /></a></p>
  
  <p>Below is the code that I'm using to send the string.</p>
  
  <pre><code>string input;
  printf("Please input what you want to send...\n");
  printf("&gt;");
  cin &gt;&gt; input;
  
  const char* ch = (const char*)&amp;input;
  int lengthOfBytes = sizeof(input);
  
  for (int i = 0; i &lt; lengthOfBytes; i++)
  {
     n = send(socket_d, &amp;*ch, 10, 0);
  }
  
  //Reading in characters.
  if (ch == (const char*)'\r')
  {
     cout &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>And here is the code used for recieving the string.</p>
  
  <pre><code>int n;
  int count = 0;
  char byte;
  n = recv(socket_d, &amp;byte, 1, 0);
  if (n &lt;= 0)
  {
      if (WSAGetLastError() != WSAEWOULDBLOCK) 
      {
          cout &lt;&lt; "Terminated " &lt;&lt; WSAGetLastError() &lt;&lt; "\n";
          return;
      }
  }
  else
  {
      cout &lt;&lt; (char) byte;
      if ((char) byte == '\r')
          cout &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>What am I doing wrong when sending the string across the network?</p>