无法从“const boost::asio::const_buffers_1”转换到“boost::asio::mutable_buffer”

发布于 2024-11-08 23:21:55 字数 787 浏览 0 评论 0原文

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   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
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-11-15 23:21:55

async_receive 函数需要更改缓冲区的内容,但不能更改常量缓冲区。

使用 mutable_buffer 而不是常量缓冲区。您可以在此处

您需要将指针和大小传递给可变缓冲区:

std::vector<char> content_buffer(max_buffer_size);

... boost::asio::mutable_buffer(content.data(), content.size()) ...

在全局范围内声明 content_buffer 非常重要,因为async_receive 函数正在异步访问缓冲区。如果您在函数中声明缓冲区,则它会在接收数据之前超出范围。

The async_receive function need to change the content of the buffer, but you can't change a constant buffer.

Use a mutable_buffer instead of a constant buffer. You can find some informations here

You need to pass a pointer and the size to the mutable buffer:

std::vector<char> content_buffer(max_buffer_size);

... boost::asio::mutable_buffer(content.data(), content.size()) ...

It's important that you declare your content_buffer in a global scope, bacause the async_receive function is accessing the buffer async. If you declare your buffer in the function, it go out of scope before data get received.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文