Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're using the local variable <code>buf</code> as the read buffer, which is dangerous and won't work. Also, you're just sending the original contents of that buffer to the handler. So instead, you need to use a buffer with a longer lifetime. Something like this:</p> <pre><code>class Server { ..... boost::array&lt;char, 1&gt; buf; void Server::accepted() { std::cout &lt;&lt; "Accepted!" &lt;&lt; std::endl; boost::asio::async_read(socket, boost::asio::buffer(buf), boost::bind(&amp;Server::handleRead, this, boost::asio::placeholders::error)); } void Server::handleRead(const boost::system::error_code&amp; error) { if(!error) { std::cout &lt;&lt; "Message: " &lt;&lt; buf.data() &lt;&lt; std::endl; } else { std::cout &lt;&lt; "Error occurred." &lt;&lt; std::endl; } } ..... } </code></pre> <p><strong>edit</strong>: or alternatively, using a heap allocated buffer (not sure if the code is right, but you'll get the idea):</p> <pre><code>void Server::accepted() { std::cout &lt;&lt; "Accepted!" &lt;&lt; std::endl; boost::shared_ptr&lt;boost::array&lt;char, 1&gt;&gt; buf(new boost::array&lt;char, 1&gt;); boost::asio::async_read(socket, boost::asio::buffer(*buf), boost::bind(&amp;Server::handleRead, this, buf, boost::asio::placeholders::error)); } void Server::handleRead(boost::shared_ptr&lt;boost::array&lt;char, 1&gt;&gt; buf, const boost::system::error_code&amp; error) { if(!error) { std::cout &lt;&lt; "Message: " &lt;&lt; buf-&gt;data() &lt;&lt; std::endl; } else { std::cout &lt;&lt; "Error occurred." &lt;&lt; std::endl; } } </code></pre>
 

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