title: Asio: Is there an automatically resizable buffer for receiving input?
body: <blockquote>
    <p>Asio: Is there an automatically resizable buffer for receiving input?</p>
  </blockquote>
  
  <p>I don't know in advance the size to be received, so I send this quantity in a header.</p>
  
  <p>I've looked at <a href="http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/example/cpp03/chat/chat_message.hpp" rel="noreferrer">http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/example/cpp03/chat/chat_message.hpp</a> for an example using a header, but this example assumes the specification of a maximum body size. </p>
  
  <p>Looking at the asio::buffer class, I must provide some underlying buffer, thus not flexible. Instead I've looked towards the asio::streambuf class, but using it as below gives segmentation/memory errors.</p>
  
  <p>I try to give a maximum size to read only <code>HEADER_LEN</code> bytes i.e. the header.</p>
  
  <p>Is this approach wrong?</p>
  
  <pre><code>void do_recv_header() 
  {
      asio::streambuf buf(HEADER_LEN);
      asio::async_read(*g_selected_conn, buf, [this, &amp;buf](const system::error_code&amp; ec, std::size_t bytes_transferred)
      {
          if (ec != 0) {
              std::cout &lt;&lt; "async_read() error: " &lt;&lt; ec.message() &lt;&lt; " (" &lt;&lt; ec.value() &lt;&lt; ") " &lt;&lt; std::endl;
              remove_closed_conn(g_selected_conn);
              SetEvent(g_wait_event);
          }
          else {
              std::istream is(&amp;buf);
              int body_len;
              is &gt;&gt; body_len;
              std::cout &lt;&lt; body_len &lt;&lt; std::endl;
              do_recv_body(body_len);
          }
      });
  }
  </code></pre>