升压read_also()函数与升压::数组工作,不能使用std ::数组 [英] Boost read_also() function not working with boost::array, cannot use std::array

查看:151
本文介绍了升压read_also()函数与升压::数组工作,不能使用std ::数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code打开一个升压插座,写命令,通过socket发送命令,得到的结果:

I have this code to open a boost socket, write a command, send the command via the socket, and get the results:

#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));
}

这code编译和运行,但冻结在调用 read_some()行了,我想不通为什么。如果任何人有任何想法的解决方案,我会非常AP preciate它。

This code compiles and runs, but freezes on the line calling read_some(), and I can't figure out why. If anyone has any ideas for solutions, I'd greatly appreciate it.

推荐答案

您写了 command.size()字节。

您再要求读 MAXSIZE 字节。

如果 command.size()&LT; MAXSIZE ,为什么不是等待更多的字节下来管?

If command.size() < MAXSIZE, why wouldn't it wait for more bytes to come down the pipe?

尝试第一写入分组的大小,则该分组的字节。

Try first writing the size of the packet, then the bytes of the packet.

在读出侧,首先读取分组的大小,然后读该分组的多个字节。

On the reading side, first read the size of the packet, then read that many bytes of the packet.

read_some 可以随意读取不到你的要求,但我没有看到说,它必须返回它读取你的要求尽可能多的字节之前的任何文件。

read_some is free to read less than you requested, but I don't see any documentation that says it must return before it reads as many bytes as you requested.

这篇关于升压read_also()函数与升压::数组工作,不能使用std ::数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆