简体   繁体   English

在boost :: asio :: ip :: tcp :: socket上设置非阻塞时出现错误文件描述符错误

[英]Bad File Descriptor error when setting non-blocking on a boost::asio::ip::tcp::socket

I'm new to boost and I've been trying out boost::asio. 我是新手,我一直在尝试使用boost :: asio。 The problem is I always get an "Bad File Descriptor" error/exception when setting some options (I need to make it non-blocking). 问题是我在设置一些选项时总是会遇到“错误文件描述符”错误/异常(我需要将其设置为非阻塞)。 Even this here fails: 即便这样也失败了:

#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main( )
{
  boost::asio::io_service io_service;

  tcp::socket socket( io_service );
  boost::asio::socket_base::non_blocking_io option(true);

  socket.io_control( option );

  return 0;
}

During run-time this pops up: 在运行期间弹出:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  Bad file descriptor

Which is getting really frustrating as I've tried everything. 当我尝试了一切时,这真的令人沮丧。 OS is Linux x64 if it matters. 如果重要,操作系统是Linux x64。

You invoked the socket constructor that does not open the socket . 您调用了不打开 socket的套接字构造函数。 You could use one of the other overloads that open the socket prior to invoking socket::io_control() , or open the socket explicitly. 您可以在调用socket::io_control()之前使用打开socket的其他重载之一,或者显式打开socket

boost::asio::ip::tcp::socket socket(io_service);
socket.open(boost::asio::ip::tcp::v4());

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

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