c++ - boost asio async_read_some 只读取数据片段

标签 c++ boost boost-asio

我有一个使用 boost::asio 进行读/写操作的 C++ 服务器 - 写出消息工作正常 - 但由于某种原因我无法读取工作

我从客户端发送给它的消息是 15 16 位无符号短裤 - 我的测试消息是这样的:

1, 34, 7, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0

现在在服务器上我经常看到这样的事情。读取通常被分解和/或乘以 256

这是一次发送两次

reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 3: [1024 0 0]
reading length = 3: [0 0 0]
reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 6: [1024 0 0 0 0 0]

这是第二次发送两次

reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 6: [1024 0 0 0 0 0]
reading length = 6: [1 34 7 0 0 0]
reading length = 1: [0] 
reading length = 0: []
reading length = 0: []
reading length = 2: [0 0]
reading length = 2: [0 0]
reading length = 1: [0]

这是第三次发送一次(我不知道这里发生了什么)

reading length = 14: [256 8704 1792 0 0 0 0 0 1024 0 0 0 0 0]

这里是实际的服务器读取代码

void Session::start(void) {
  msg_interface->add_msg_listener(this);
  _socket.async_read_some(
    boost::asio::buffer(_read_data_buffer, READ_DATA_BUFFER_LENGTH),
    boost::bind(&Session::handle_read, this, boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void Session::handle_read(const boost::system::error_code& error, std::size_t bytes_transferred) {
  if (error) {
    msg_interface->remove_msg_listener(this);
    delete this;
  } else {
    if (bytes_transferred != 0) {
      // print what we've received
      const int length = bytes_transferred / sizeof(uint16_t);
      std::cout << "reading length = " << length << ": [";
      for (int i = 0; i < length; i++) {
        const uint16_t data = ntohs(_read_data_buffer[i]);
        std::cout << data;
        if (i != length - 1) {
          std::cout << " ";
        }
      }
      std::cout << "]" << std::endl;
    }
    _socket.async_read_some(
      boost::asio::buffer(_read_data_buffer, READ_DATA_BUFFER_LENGTH),
      boost::bind(&Session::handle_read, this, boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
  }
}

变量如下

const int READ_DATA_BUFFER_LENGTH = 1024;

class AbstractSession {
protected:
  uint16_t _read_data_buffer[READ_DATA_BUFFER_LENGTH];
...

有一件事——我使用相同的 IO 服务在接收到消息时写入消息(服务器正在与旧接口(interface)通信并将接收到的消息发送给所有客户端)并从客户端读取消息.对两者使用相同的 io 服务会导致这种情况发生吗?

关于我应该如何解决这个问题有什么想法吗?

最佳答案

您的代码执行了它应该执行的操作 - 它读取了尽可能多的数据。这就是为什么函数 async_read_some name 以“some”结尾 - 它读取“some”字节并且不 promise 做更多的事情。文档还说明了以下内容:

Consider using the async_read function if you need to ensure that the requested amount of data is read before the asynchronous operation completes.

这看起来像你的情况,除非你重新设计你的接收器来处理部分读取。

关于c++ - boost asio async_read_some 只读取数据片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11084296/

相关文章:

C++ 最佳实践 : function to accept uint8_t, uint16_t、uint32_t、float

c++ - 读取 graphviz 的点文件而不在 boost 图中存储节点 ID

c++ - 使用 async_write 同时写入不同的套接字

c++ - 使用 Boost.Asio 进行异步解析

java - 自动选择文件 I/O 的缓冲区大小

c++ - 在特定时间运行 C++ 循环

visual-studio-2010 - 错误 LNK1104 : cannot open file 'libboost_system-vc100-mt-gd-1_55. 库

c++ - Visual Studio 中的英特尔编译器未找到 boost 库

c++ - boost 编译问题

c++ - 我有一个.pdf文件,如何将其页面呈现为RGB图像?