简体   繁体   中英

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. 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.

You invoked the socket constructor that does not open the socket . You could use one of the other overloads that open the socket prior to invoking socket::io_control() , or open the socket explicitly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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