Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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