繁体   English   中英

在使用boost :: asio read_some()函数时替代std :: array,在read_some()函数调用时boost :: array崩溃

[英]Substitute for std::array when using boost::asio read_some() function, boost::array crashes at read_some() function call

我正在研究使用boost的套接字和char数组从套接字读取结果。 根据我在网上看到的示例(包括Boost文档),到目前为止,我有以下内容:

#include <boost/asio.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());

  std::array<char, MAXSIZE> buffer;
  this->socket.read_some(boost::asio::buffer(buffer));
}

根据我所看到的示例,看来这应该可行。 我有一个问题。 出于某种原因,我的编译器(我在Qt 5.2中工作)似乎拒绝将std :: array识别为有效。 即使使用#include <array >行,该行定义缓冲区也会给出一个错误,指出array不是std的成员。 我的项目负责人和我工作了一个小时左右,尝试了多种解决方法,包括完全重新安装了std库,但无济于事。 代码仍然会识别#include <array >行是有效的,但是然后让我不要使用std :: array。 我试图通过将buffer定义为char buffer[MAXSIZE] = ""来解决此问题,但是编译时,当它到达下一行的read_some命令时,它会崩溃。 诚然,我对使用Boost真的很陌生,而且我不确定是否还有其他方法可以定义缓冲区以使其成为传递给read_some函数的有效参数。 如果有人知道如何解决此问题(如何将缓冲区传递给read_some而不会崩溃,或者如何使std :: array工作),我将非常感激。 谢谢!

注意 :我正在处理的项目使用VS2008。

UPDATE :到目前为止,除了上面的示例代码中的代码之外,我还尝试了以下操作:

  • 声明buffer为char buffer[MAXSIZE]; 这样可以编译,但是程序在调用socket.read_some()的行处冻结。 使用char buffer[MAXSIZE] = "";我得到相同的结果char buffer[MAXSIZE] = "";

  • 声明缓冲区为boost::array <char, MAXSIZE > buffer; ,无论是否添加指令行都包含boost/array.hpp 这冻结在同一位置。

  • 根据我在这里找到的内容,尝试将缓冲区声明为std::tr1::array <char, MAXSIZE > buffer; 同样,按照相同的逻辑,我尝试using namespace std::tr1;添加using namespace std::tr1; 然后将缓冲区定义为array <char, MAXSIZE > buffer; 这些都没有汇编; 两者都因参数类型不匹配而在调用read_some()的行中给出了错误。

这确实发生在我身上,这是有可能的,因为似乎应该工作的对象倾向于在read_some()处冻结,这需要很长时间才能读取,并且只是在超时? 如果是这样的话,我可以解决此问题,我只是不知道如何判断这是怎么回事,或者只是崩溃了。

应该没问题:请在Coliru上观看

#include <boost/asio.hpp>
#include <array>
#include <string>
#define MAXSIZE 1000000
//...
void 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...

    socket.open(boost::asio::ip::tcp::v4());
    socket.connect(e);
    socket.write_some(boost::asio::buffer(command));
    socket.send(boost::asio::buffer(command));

    std::array<char, MAXSIZE> buf;
    socket.read_some(boost::asio::buffer(buf));
}

int main()
{
    processCommand("EHLO");
}

如果您没有C ++ 11,请使用boost::array (带有boost/array.hpp )。

在所有其他情况下,请在Boost问题跟踪器中查看您的编译器和不兼容的潜在报告

您可以按照此处的建议使用std::array boost::array std::vectorstd::string

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM