Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost ASIO async_read_some
    primarykey
    data
    text
    <p>I am having difficulties in implementing a simple TCP server. The following code is taken from <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html" rel="nofollow">boost::asio examples</a>, "Http Server 1" to be precise.</p> <pre><code>void connection::start() { socket_.async_read_some( boost::asio::buffer(buffer_), boost::bind( &amp;connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); } void connection::handle_read(const boost::system::error_code&amp; e, std::size_t bytes_transferred) { if (!e &amp;&amp; bytes_transferred) { std::cout &lt;&lt; " " &lt;&lt; bytes_transferred &lt;&lt;"b" &lt;&lt; std::endl; data_.append(buffer_.data(), buffer_.data()+bytes_transferred); //(1) what here? socket_.async_read_some( boost::asio::buffer(buffer_), boost::bind( &amp;connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); } else// if (e != boost::asio::error::operation_aborted) { std::cout &lt;&lt; data_ &lt;&lt; std::endl; connection_manager_.stop(shared_from_this()); } } </code></pre> <p>In the original code the <code>buffer_</code> is big enough to keep the entire request. It's not what I need. I've changed the size to 32bytes. </p> <p>The server compiles and listens at port 80 of localhost, so I try to connect to it via my web browser.</p> <p>Now if the statement (1) is commented-out, then only the first 32bytes of the request are read and the connection hangs. Web browser keeps waiting for the response, the server does.. I dont know what.</p> <p>If (1) is uncommented, then the entire request is read (and appeded to <code>data_</code>), but it never stops - I have to cancel the request in my browser and only then does the <code>else { }</code> part run - I see my request on stdout.</p> <p><strong>Question 1:</strong> How should I handle a large request?<br/> <strong>Question 2:</strong> How should I cache the request (currently I append the buffer to a string)?<br/> <strong>Question 3:</strong> How can I tell that the request is over? In HTTP there always is a response, so my web-browser keeps waiting for it and doesnt close the connection, but how can my server know that the request is over (and perhaps close it or reply some "200 OK")?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload