我有一个本地posix套接字,我试图从使用boost :: asio异步读取: :async_read。但是当我这样做时:使用boost :: asio :: async_read失败,但boost :: asio :: read工程(正在使用io_stream.run_one())
// io_stream and fd are passed by reference to this function
asio::posix::stream_descriptor stream(io_stream);
stream.assign(fd);
boost::system::error_code ec;
asio::async_read(stream, buffer,
asio::transfer_at_least(1),
boost::bind(doRead,
asio::placeholders::error,
asio::placeholders::bytes_transferred));
size_t count = io_service.run_one(ec);
计数变量设置为0并且doRead处理程序从不调用,ec不包含错误。但是,如果我有一个同步读取代异步读取:
size_t count = asio::read(stream, buffer,
asio::transfer_at_least(1));
这工作,我得到的数据的预期量回到计数变量。我使用由“int pipe(int pipefd [2]);”创建的fd的测试用例和传递数据通过做工精细与ASIO :: async_read所以我想它可能是几个选项:
- 一些我传递本地描述符是如何以某种方式不兼容async_read所以它是默默地失败。我试图通过阻断和关闭来创建它,但无济于事。
- io_service和posix :: stream_descriptor是一些如何不连接,所以io_service从不尝试运行读取。这可能是由于我通过引用传递io_service吗? (并在一些线程以及这一问题?)
'stream_descriptor'可以很好地处理'pipe'的读端的描述符,参见[this](http://stackoverflow.com/questions/5210796/boost-asio-how-to-write- console-server/5211349)#5211349)回答。我怀疑你的第二颗子弹是你看到的行为的原因。请编辑您的问题,并附上一个[简短,自包含,正确的示例](http://sscce.org/)供我们分析。 –
周一上班时我会一起上班,因为我家里没有代码,谢谢! –