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

问题描述

限时删除!!

我想从做异步读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<测试]它抛出:

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:

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 input
read 23 bytes with Success
$ echo "this is a test" > test
$ ./client < test
Operation not permitted
$ cat test | ./client
read 15 bytes with Success

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

1403页,肝出来的..

09-06 12:32