title: Send length and data through socket c++
body: <p>So i'm currently working on a app that uses socket, for now everything is working, i just want to know if there is a more efficient way to send the length of the data and the data instead of sending twice. </p>
  
  <p>i.e: Sending everything in one string.</p>
  
  <pre><code>void SendPackage(const SOCKET sock, const std::string package)
  {
      int length = package.lenth();
  
      send(sock, std::to_string(length).c_str(), 10, 0);     //Buffer length is 10 assuming data length
      send(sock, package.c_str(), length, 0);        //will never be greater than 9,999,999,999
  }
  
  void ReceivePackage(const SOCKET sock, std::string &amp;package, int bufferLength)
  {
      std::vector&lt;char&gt; recvBuffer(10);
      int length, bytesProcessed;
  
      recv(sock, &amp;recvBuffer[0], 10, 0); //Receiving length
      length = atoi(&amp;recvBuffer[0]);
      recvBuffer.resize(length);
  
      for (int i = 0; i &lt; length; i += bytesProcessed)
      {
          bytesProcessed = recv(sock, &amp;recvBuffer[0] + i, bufferLength, 0);
          if (bytesProcessed &lt; 0) break;
      }
  
      package = &amp;recvBuffer[0];
  }
  </code></pre>