Note that there are some explanatory texts on larger screens.

plurals
  1. POasync_read_until finishing before delimiter read
    primarykey
    data
    text
    <p>I am having a problem using async_read_until. I was originally using async_receive but the response from my server grew to be more than one packet, and I ran into a problem where async_receive would stop reading after the first packet was received. I then looked into async_read_until and tried to read until a delimiter was reached. This however seems to be a problem, whenever I use async_read_until, my streambuf object only seems to go until the first whitespace. Here is my code I am using:</p> <pre><code>bool request_handler::get_recommendation(std::string&amp; content, std::string &amp;returnJson) { //Connect to recommendation engine boost::asio::io_service io_service; boost::asio::ip::tcp::socket socket(io_service); boost::asio::ip::tcp::resolver resolver(io_service); boost::asio::ip::tcp::resolver::query query(Settings::get_recommendation_ip(), Settings::get_recommendation_port(), boost::asio::ip::resolver_query_base::numeric_service); boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query); boost::asio::ip::tcp::endpoint endpoint = *iterator; boost::system::error_code ec; socket.connect(endpoint, ec); if(ec) { Log::error("Couldn't connect: " + ec.message()); return false; } //Create post Message std::string request_information = "POST / HTTP/1.1\r\n"; request_information += "Host: " + Settings::get_recommendation_ip() + "\r\n"; request_information += "Accept: */*\r\n"; request_information += "Content-Type:text/plain\r\n"; request_information += "Content-Length:" + boost::lexical_cast&lt;std::string&gt;(content.size()) + "\r\n"; request_information += "Connection: close\r\n\r\n"; request_information += content; try { boost::shared_ptr&lt;bool&gt; timer1_result(new bool(false)); boost::shared_ptr&lt;bool&gt; timer2_result(new bool(false)); boost::shared_ptr&lt;bool&gt; write_result(new bool(false)); boost::shared_ptr&lt;bool&gt; read_result(new bool(false)); // boost::array&lt;char,8192&gt; buf; boost::asio::streambuf buf; // buf.assign(0); boost::asio::deadline_timer dt(io_service); //Create write timer dt.expires_from_now(boost::posix_time::milliseconds(Settings::get_write_timeout())); dt.async_wait(boost::bind(&amp;request_handler::set_result, this, timer1_result, _1)); //Call async write boost::asio::async_write(socket, boost::asio::buffer(request_information, request_information.size()), boost::bind(&amp;request_handler::set_result, this, write_result, _1)); io_service.reset(); //Run until either the timer finishes or the write completes while(io_service.run_one()) { //Write completes if(*write_result) { //Stop the timer dt.cancel(); break; } //Timer completes else if(*timer1_result) { //Stop the write socket.cancel(); return false; } } boost::asio::deadline_timer dt2(io_service); //Create read timer dt2.expires_from_now(boost::posix_time::milliseconds(Settings::get_read_timeout())); dt2.async_wait(boost::bind(&amp;request_handler::set_result, this, timer2_result, _1)); //Call async_receive // socket.async_receive(boost::asio::buffer(buf), boost::bind(&amp;request_handler::set_result, this, read_result, _1)); boost::asio::async_read_until(socket, buf, "]", boost::bind(&amp;request_handler::set_result, this, read_result, _1)); io_service.reset(); //Run until either the timer finishes or the receive completes while(io_service.run_one()) { //Receive completes if(*read_result) { //Stop timer // dt2.cancel(); // break; } else if(*timer2_result) { //Stop the write socket.cancel(); break; } } //Put result in return string // returnJson = buf.data(); std::istream is(&amp;buf); is &gt;&gt; returnJson; std::cout &lt;&lt; "***" &lt;&lt; returnJson &lt;&lt; std::endl; return true; }catch(std::exception&amp; e) { return false; } } void request_handler::set_result(boost::shared_ptr&lt;bool&gt; a, boost::system::error_code ec) { std::cout &lt;&lt; ec.message() &lt;&lt; std::endl; *a = true; } </code></pre> <p>I thought the problem might have been that I need more calls to read_until and the whole packet wasn't being read in at once, but I am unable to get any more data than "HTTP/1.1" of my response from streambuf I read into. Any clue what the problem might be?</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.
 

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