Boost.Asio 文档不存在。这些错误是什么意思?

发布于 2024-08-11 11:20:44 字数 972 浏览 7 评论 0原文

我在使用 Boost.Asio 时遇到两个错误。

第一个发生在我尝试在套接字上接收数据时:

char reply[1024];
boost::system::error_code error;
size_t reply_length = s.receive(boost::asio::buffer(reply, 1024), 0, error);
if (error) cout << error.message() << endl; //outputs "End of file"

第二个发生在我尝试从(有效!)本机套接字创建 ip::tcp::socket 时:

boost::asio::io_service ioserv;
boost::asio::ip::tcp::socket s(ioserv);

boost::system::error_code error;
s.assign(boost::asio::ip::tcp::v4(), nativeSocket, error);
if (error) cout << error.message() << endl; //outputs "The parameter is incorrect"

由于所有这些麻烦且没有文档可供参考,我我很想回到 BSD 套接字,但我在那里遇到了自己的问题......所以如果有人可以提供帮助,我将非常感激。

编辑:关于数字2,nativeSocket是这样声明的:

SOCKET nativeSocket = INVALID_SOCKET;
nativeSocket = accept(svr_sock, (struct sockaddr*)&sin, &size);

之后,对套接字做了一些其他的事情——即,使用ioctlsocket将其设置为非阻塞,并对SO_LINGER和SO_OOBINLINE使用setsockopt。

I'm struggling with two errors with Boost.Asio.

The first occurs when I try to receive data on a socket:

char reply[1024];
boost::system::error_code error;
size_t reply_length = s.receive(boost::asio::buffer(reply, 1024), 0, error);
if (error) cout << error.message() << endl; //outputs "End of file"

The second occurs when I try to create an ip::tcp::socket from a (valid!) native socket:

boost::asio::io_service ioserv;
boost::asio::ip::tcp::socket s(ioserv);

boost::system::error_code error;
s.assign(boost::asio::ip::tcp::v4(), nativeSocket, error);
if (error) cout << error.message() << endl; //outputs "The parameter is incorrect"

With all these troubles an no documentation to turn to, I am tempted to go back to BSD sockets, but I'm having my own problems there...so if anyone can help, I'd really appreciate it.

EDIT: Regarding number 2, nativeSocket is declared thusly:

SOCKET nativeSocket = INVALID_SOCKET;
nativeSocket = accept(svr_sock, (struct sockaddr*)&sin, &size);

After that, a few other things are done to the socket -- namely, setting it as non-blocking using ioctlsocket, and using setsockopt for SO_LINGER and SO_OOBINLINE.

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

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

发布评论

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

评论(1

绮筵 2024-08-18 11:20:44

无论如何,这都不是解决您的第二个问题的完整解决方案。它生成的任何错误都应该映射到 boost::system::error_code 中,但我在 boost/system/error_code.hpp 中没有找到类似的内容,所以我不知道它到底是什么意思。

但是,在查看了 boost 1.39 的代码后,assign 最终被移交给detail::reactive_socket_servicedetail::reactive_socket_servicedetail::reactive_socket_servicedetail::reactive_socket_service协议,Reactor >.assign(或 detail::win_iocp_socket_service,如果您使用的是 Windows)。它只能在 boost/asio/detail/reactive_socket_service.hpp 中的两个位置产生错误:

if (is_open(impl))
{
  ec = boost::asio::error::already_open;
  return ec;
}

或者

if (int err = reactor_.register_descriptor(
      native_socket, impl.reactor_data_))
{
  ec = boost::system::error_code(err,
      boost::asio::error::get_system_category());
  return ec;
}

由于您没有收到 already_open 错误,因此该错误必须从第二位代码开始。反应器类型来自 boost/asio/stream_socket_service.hpp 中的一系列 ifdef/elif 对,其中仅可用 epoll_reactor 中的 >register_descriptor 函数可能会引发任何错误(当然 detail::win_iocp_socket_service.assign 也可以)。 epoll_reactor中的错误来自于sys/epoll.h,具体来说:

int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
if (result != 0)
  return errno;

在windows实现中,相关代码是

if (iocp_service_.register_handle(native_socket.as_handle(), ec))
  return ec;

我认为这是你错误的根源,但老实说,我无法追踪到这一点。

This is not a complete solution to your second problem by any means. Any errors that it generates should be mapped into a boost::system::error_code, but I don't find anything like it in boost/system/error_code.hpp, so I'm at a loss as to what exactly it is supposed to mean.

But, after looking through the code for boost 1.39, assign is eventually handed off to either detail::reactive_socket_service< Protocol, Reactor >.assign (or detail::win_iocp_socket_service<Protocol>, if you're using windows). It can only be producing an error in two places in boost/asio/detail/reactive_socket_service.hpp:

if (is_open(impl))
{
  ec = boost::asio::error::already_open;
  return ec;
}

or

if (int err = reactor_.register_descriptor(
      native_socket, impl.reactor_data_))
{
  ec = boost::system::error_code(err,
      boost::asio::error::get_system_category());
  return ec;
}

Since, you're not getting an already_open error, the error must from the second bit of code. The reactor type comes from a sequence of ifdef/elif pairs in boost/asio/stream_socket_service.hpp, and of those available only the register_descriptor function in epoll_reactor can throw any error (and of course detail::win_iocp_socket_service<Protocol>.assign can, also). The error in epoll_reactor comes from sys/epoll.h, specifically:

int result = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, descriptor, &ev);
if (result != 0)
  return errno;

In the windows implementation, the related code is

if (iocp_service_.register_handle(native_socket.as_handle(), ec))
  return ec;

I think this is the origin of your error, but honestly, I can't trace it past this point.

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