bool Connection::Receive(){
boost::shared_ptr<std::string> buffer(new std::string());
socket_.async_receive(boost::asio::buffer(*buffer), boost::bind(&Connection::handler, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
//std::cout<<buffer<<std::endl;
int recvlen = buffer->length();
if (recvlen <= 0) {
return false;
}
//this->OnReceived(buffer, recvlen);
return true;
}
Error:
Error 1 error C2440: '<function-style-cast>' : cannot convert from 'const boost::asio::const_buffers_1' to 'boost::asio::mutable_buffer' e:\boost_1_46_1\boost_1_46_1\boost\asio\detail\buffer_sequence_adapter.hpp 211
The
async_receivefunction need to change the content of the buffer, but you can’t change a constant buffer.Use a
mutable_bufferinstead of a constant buffer. You can find some informations hereYou need to pass a pointer and the size to the mutable buffer:
It’s important that you declare your
content_bufferin a global scope, bacause theasync_receivefunction is accessing the buffer async. If you declare your buffer in the function, it go out of scope before data get received.