开发者

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

开发者 https://www.devze.com 2023-02-12 13:42 出处:网络
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 he

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消