Boost read_also()函数不能与Boost::array一起工作,不能使用std::array

Boost read_also() function not working with boost::array, cannot use std::array

本文关键字:array 不能 Boost 一起 std 工作 read also 函数      更新时间:2023-10-16

我有这个代码打开一个boost套接字,写一个命令,通过套接字发送命令,并获得结果:

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <array>
#include <string>
#define MAXSIZE 1000000
//...
void MyClass::processCommand(std::string command)
{
  boost::asio::io_service io;
  boost::asio::ip::tcp::socket socket(io);
  boost::asio::ip::tcp::endpoint e(boost::asio::ip::address::from_string("127.0.0.1"), 60151);  //Info for the connection I need to make...
  this->socket.open(boost::asio::ip::tcp::v4());
  this->socket.connect(e);
  this->socket.write_some(boost::asio::buffer(command, command.size());
  this->socket.send(boost::asio::buffer(command, command.size());
  boost::array<char, MAXSIZE> buffer;
  this->socket.read_some(boost::asio::buffer(buffer));
}

这段代码编译并运行,但是在调用read_some()的行上卡住了,我不知道为什么。如果谁有什么解决办法,我将不胜感激。

您写了command.size()字节

然后请求读取MAXSIZE字节。

如果command.size() <MAXSIZE,为什么它不等待更多的字节从管道下来呢?>

先写数据包的大小,再写数据包的字节数。

在读取端,首先读取数据包的大小,然后读取数据包的多少字节。

read_some可以自由地读取比您请求的少的字节,但是我没有看到任何文档说它必须在读取您请求的那么多字节之前返回。

相关文章: