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

发布于 2024-10-19 03:27:49 字数 672 浏览 6 评论 0原文

我是 boost 的新手,我一直在尝试 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;
}

在运行时弹出:

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

这真的很令人沮丧,因为我已经尝试了一切。如果重要的话,操作系统是 Linux x64。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-10-26 03:27:49

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

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文