Bad file descriptor error when setting up non-blocking on boost :: asio :: ip :: tcp :: socket

I am new to boost and I have tried boost :: asio. The problem is that when I configure some parameters, I always get the "Bad File Descriptor" error / exception message (I need to make it non-blocking). Even here it 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;
}

At runtime, 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 disappointing since I tried everything. OS is Linux x64, if that matters.

+3
source share
1 answer

You called a socket constructor that does not open socket . You can use one of the other overloads that open socketbefore the call socket::io_control(), or explicitly open socket.

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

Source: https://habr.com/ru/post/1794002/


All Articles