title: Boost receive data from the tcp socket
body: <p>I am trying to read specific number of bytes from the socket. My server is sending:</p>
  
  <p>1) byte[0] - length  of the message
  2) byte[1:N] - the actual message</p>
  
  <p>How do I read the first byte and then read the remaining bytes using boost::asio::ip::tcp::socket::read ? Here is the code snippet:</p>
  
  <pre><code> // receive data through the socket
      void TCPTestClient::ReceiveData( )
      {
          try
          {
              boost::system::error_code error;
  
              boost::asio::streambuf receivedStreamBuffer;
  
              // reserve 512 bytes in output sequence
              boost::asio::streambuf::mutable_buffers_type bufs =receivedStreamBuffer.prepare( 512 );
              boost::asio::read( m_socket,
                                     bufs,
                                     error );
  
              // transfer the buffer contents to string
              std::istream is( &amp;receivedStreamBuffer );
              is &gt;&gt; m_receivedMessageStr;
  
  
              // throw exception if error occurred
              if ( error )
              {
                  throw NetworkTestFailedException( error.message() );
              }
          }
          catch(...)
          {
  
          }
      }
  </code></pre>