奇怪的异常抛出 - 分配:不允许操作 [英] Strange exception throw - assign: Operation not permitted

查看:234
本文介绍了奇怪的异常抛出 - 分配:不允许操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从做异步读CIN所以我有一块code的

client.h

  ...
提高:: ASIO :: POSIX :: stream_descriptor输入;
提高:: ASIO ::流缓冲input_buffer

client.cpp

 客户端::(INT ARGC,字符** argv的,提高:: ASIO :: io_service对象和放大器; io_service对象)
    :TCP_SOCKET(io_service对象)
    ,UDP_SOCKET(io_service对象)
    ,输入(io_service对象,:: DUP(STDIN_FILENO))
{
    ...
    read_std_input();
}无效的客户:: read_std_input(){
    async_read_until(输入,input_buffer,'\\ n',
                     提高::绑定(安培;客户:: handle_std_read,为此,
                                 提高:: ASIO ::占位符::错误
                                 提高:: ASIO ::占位符:: bytes_transferred));
}

问题是:当我运行我的客户正常的方式[./client],然后通过命令输入类似,它就像魅力。
然而,当我通过运行[./client<测试]它抛出:


  

抛出的一个实例后终止叫
  提振:: exception_detail :: clone_impl


  
  

    

是什么():分配:不允许操作中止


  

你有什么样的问题可能是一个想法?
谢谢!


解决方案

Boost.Asio的的POSIX面向流的描述明确不支持普通文件。因此,如果测试是一个普通的文件,然后 ./客户&LT;测试将导致<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/posix__basic_stream_descriptor/assign.html\"><$c$c>posix::stream_descriptor::assign()尝试分配 STDIN_FILENO stream_descriptor 时失败。在<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost_asio/overview/posix/stream_descriptor.html\">documentation声明:


  

Boost.Asio的包括添加到允许同步和异步读取和写入操作要在POSIX文件描述符执行类,如管道,标准输入和输出,以及各种设备(但不会的常规文件)。


考虑通过了测试文件的内容通过管道客户端

$猫测试| 。/客户


下面是一个完整的示例程序和演示:

的#include&LT;&iostream的GT;
#包括LT&;升压/ asio.hpp&GT;无效handle_read(
  常量的boost ::系统::错误_ code&放;错误,
  的std ::为size_t bytes_transferred

{
  性病::法院LT&;&LT; 读&LT;&LT; bytes_transferred&LT;&LT; 用字节
            &LT;&LT;返回Error.message()&所述;&下;的std :: ENDL;
}诠释的main()
{
  提高:: ASIO :: io_service对象io_service对象;
  提高:: ASIO :: POSIX :: stream_descriptor输入(io_service对象);  //分配STDIN_FILENO到stream_descriptor。它将支持
  //管道,标准输入和输出,以及各种装置,但不
  //常规文件。
  提高::系统::错误_ code错误;
  input.assign(STDIN_FILENO,误差);
  如果(错误)
  {
    的std :: CERR&LT;&LT;返回Error.message()&所述;&下;的std :: ENDL;
    返回-1;
  }  提高:: ASIO ::流缓冲input_buffer;
  async_read_until(输入,input_buffer,'\\ n',&安培; handle_read);
  io_service.run();
}

示范


$ ./client
测试标准输入<大骨节病>输入
读23个字节成功
$回声这是一个测试&GT;测试
$ ./client&LT;测试
不允许操作
$猫测试| 。/客户
读15个字节成功

I want to do asynchronous read from cin therefore I have a piece of code

client.h

...
boost::asio::posix::stream_descriptor input;
boost::asio::streambuf input_buffer

client.cpp

Client::Client(int argc, char **argv, boost::asio::io_service &io_service)
    : tcp_socket(io_service)
    , udp_socket(io_service)
    , input(io_service, ::dup(STDIN_FILENO))
{
    ...
    read_std_input();
}

void Client::read_std_input() {
    async_read_until(input, input_buffer, '\n',
                     boost::bind(&Client::handle_std_read, this,
                                 boost::asio::placeholders::error,
                                 boost::asio::placeholders::bytes_transferred));
}

The problem is: when I run my client the normal way [ ./client ] and then input something via command like, it works like charm. However, when I run it via [ ./client < test ] it throws :

terminate called after throwing an instance of 'boost::exception_detail::clone_impl

' what(): assign: Operation not permitted Aborted

Do you have an idea of what the problem might be? Thanks!

解决方案

Boost.Asio's POSIX stream-oriented descriptors explicitly do not support regular files. Hence, if test is a regular file, then ./client < test will result in posix::stream_descriptor::assign() failing when attempting to assign STDIN_FILENO to the stream_descriptor. The documentation states:

Boost.Asio includes classes added to permit synchronous and asynchronous read and write operations to be performed on POSIX file descriptors, such as pipes, standard input and output, and various devices (but not regular files).

Consider passing the contents of the test file to client through a pipe.

$ cat test | ./client


Here is a complete example program and demonstration:

#include <iostream>
#include <boost/asio.hpp>

void handle_read(
  const boost::system::error_code& error,
  std::size_t bytes_transferred
)
{
  std::cout << "read " << bytes_transferred << " bytes with "
            << error.message() << std::endl;
}

int main()
{
  boost::asio::io_service io_service;
  boost::asio::posix::stream_descriptor input(io_service);

  // Assign STDIN_FILENO to the stream_descriptor.  It will support
  // pipes, standard input and output, and various devices, but NOT
  // regular files.
  boost::system::error_code error;
  input.assign(STDIN_FILENO, error);
  if (error)
  {
    std::cerr << error.message() << std::endl;
    return -1;
  }

  boost::asio::streambuf input_buffer;
  async_read_until(input, input_buffer, '\n', &handle_read);
  io_service.run();
}

Demonstration

$ ./client
testing standard inputenter
read 23 bytes with Success
$ echo "this is a test" > test
$ ./client < test
Operation not permitted
$ cat test | ./client
read 15 bytes with Success

这篇关于奇怪的异常抛出 - 分配:不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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