如何在asio :: buffer()中使用std :: string [英] How to use std::string with asio::buffer()
问题描述
当我尝试将std :: string与boost :: asio :: buffer:一起使用时,出现以下错误消息
I get the following error message when I'm trying to use std::string with boost::asio::buffer:
boost/asio/detail/consuming_buffers.hpp:
In constructor
'boost::asio::detail::consuming_buffers<
boost::asio::mutable_buffer, boost::asio::const_buffers_1
>::consuming_buffers(const boost::asio::const_buffers_1 &)':
boost/asio/impl/read.hpp:140:25:
instantiated from
'boost::asio::detail::read_op<
boost::asio::basic_stream_socket<boost::asio::ip::tcp>,
boost::asio::const_buffers_1
, boost::asio::detail::transfer_all_t
, boost::_bi::bind_t<
void, boost::_mfi::mf1<void, read_op, const
boost::system::error_code &>
, boost::_bi::list2<boost::_bi::value<read_op
*>, boost::arg<1> (*)()>
>
>::read_op(
boost::asio::basic_stream_socket<boost::asio::ip::tcp>
&, const boost::asio::const_buffers_1
&
, boost::asio::detail::transfer_all_t
, boost::_bi::bind_t<
void, boost::_mfi::mf1<void, read_op, const
boost::system::error_code &>
, boost::_bi::list2<boost::_bi::value<read_op
*>, boost::arg<1> (*)()>
>
)'
[...]
完整源代码: http://liveworkspace.org/code/eca749f6f2714b7c3c4df9f26a404d86
推荐答案
我认为问题是您正在将const缓冲区传递给async_read
而不是可变缓冲区.在第50行结尾的块中,boost::asio::buffer(_header)
返回const缓冲区.您应该执行boost::asio::async_read(s, boost::asio::buffer(data, size), handler)
之类的操作,因为boost::asio::buffer(data, size)
会创建一个可变缓冲区.
I think the problem is that you are passing a const buffer to async_read
instead of a mutable buffer. In the block ending in line 50, boost::asio::buffer(_header)
returns a const buffer. You should do something like boost::asio::async_read(s, boost::asio::buffer(data, size), handler)
, because boost::asio::buffer(data, size)
creates a mutable buffer.
您可能需要使用char数组,而不是对_header和_data使用std::string
,例如:
Instead of using std::string
s for _header and _data, you probably need to use arrays of char, such as:
char* _data;
boost::asio::buffer(_data, strlen(_data));
请参阅buffer
和async_read
的参考文档.
这篇关于如何在asio :: buffer()中使用std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!