c++ - 如何在 boost::asio 中读取数据包

标签 c++ boost boost-asio

我如何在 boost::asio 中读取数据包,例如 0x01,当前代码我只读取文本:

   m_socket.async_read_some(boost::asio::buffer(buffer),
       strand.wrap(boost::bind(&Client::handleRead, shared_from_this(),
       boost::asio::placeholders::error,
       boost::asio::placeholders::bytes_transferred)));

谢谢

最佳答案

您好,您可以使用 int 变量分配缓冲区,而不是使用从套接字读取的值

int m_hdr_size = 0; // this var must be not local

boost::asio::async_read_some(
boost::asio::buffer(reinterpret_cast<char*>(&m_hdr_size), size_t(4))...

如果我理解你的问题。只需设置变量并将其大小设置为缓冲区

http://think-async.com/Asio/boost_asio_1_3_1/doc/html/boost_asio/reference/buffer.html

关于c++ - 如何在 boost::asio 中读取数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4354273/

相关文章:

c++ - 警告 : 'virtual void wxWindowBase::SetInitialBaseSize(const wxSize&)' is deprecated

c# - IShellItem 的 GUID

c++ - 二叉搜索树上的范围查询(递归)

c++ - 删除存储在数组中的特定类对象

c++ - 当顶点具有属性时,write_graphviz 不起作用

c++ - Visual Studio 2010,有什么方法可以添加前缀,例如(C :\) to #include a massive amount of <boost/asio. hpp> 标题?

c++ - 带 Boost Spirit X3 的最小计算器

c++ - 构建带有 .v2 扩展名的 Boost 日期/时间 Jamfile

c++ - boost::iostream readline 在 4096 字节后停止

c++ - std::bind 是否应该与 boost::asio 兼容?