How to use std::string with asio::buffer()

十年热恋 提交于 2019-11-30 17:41:45
rturrado

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.

Instead of using std::strings for _header and _data, you probably need to use arrays of char, such as:

char* _data;
boost::asio::buffer(_data, strlen(_data));

See references for buffer and async_read.

You must pass a pointer as the first parameter:

#include <string>
#include <boost/asio.hpp>

std::string request, reply;
auto rsize = boost::asio::buffer(&reply[0], request.size());

http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/buffer.html

It seems that std::string could only be passed into an asio::buffer as a const reference.

std::vector should be a better alternative:

std::vector<char> d2(128);
bytes_transferred = sock.receive(boost::asio::buffer(d2));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!