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

Sup*_*eep 3 c++ boost boost-asio

我是新手,我一直在尝试使用boost :: asio.问题是我在设置一些选项时总是会遇到"错误文件描述符"错误/异常(我需要将其设置为非阻塞).即便这样也失败了:

#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;
}
Run Code Online (Sandbox Code Playgroud)

在运行期间弹出:

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
Run Code Online (Sandbox Code Playgroud)

当我尝试了一切时,这真的令人沮丧.如果重要,操作系统是Linux x64.

Sam*_*ler 7

您调用了无法打开的套接字构造函数socket.您可以使用打开socket调用之前的其他重载之一socket::io_control(),或者socket显式打开.

boost::asio::ip::tcp::socket socket(io_service);
socket.open(boost::asio::ip::tcp::v4());
Run Code Online (Sandbox Code Playgroud)