Boost.Asio 文档不存在。这些错误是什么意思?
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论如何,这都不是解决您的第二个问题的完整解决方案。它生成的任何错误都应该映射到
boost::system::error_code
中,但我在boost/system/error_code.hpp
中没有找到类似的内容,所以我不知道它到底是什么意思。但是,在查看了 boost 1.39 的代码后,
assign
最终被移交给detail::reactive_socket_service
detail::reactive_socket_service
detail::reactive_socket_service
detail::reactive_socket_service
协议,Reactor >.assign
(或detail::win_iocp_socket_service
,如果您使用的是 Windows)。它只能在boost/asio/detail/reactive_socket_service.hpp
中的两个位置产生错误:或者
由于您没有收到
already_open
错误,因此该错误必须从第二位代码开始。反应器类型来自boost/asio/stream_socket_service.hpp
中的一系列ifdef
/elif
对,其中仅可用
函数可能会引发任何错误(当然epoll_reactor
中的 >register_descriptordetail::win_iocp_socket_service.assign
也可以)。epoll_reactor
中的错误来自于sys/epoll.h
,具体来说:在windows实现中,相关代码是
我认为这是你错误的根源,但老实说,我无法追踪到这一点。
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 inboost/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 eitherdetail::reactive_socket_service< Protocol, Reactor >.assign
(ordetail::win_iocp_socket_service<Protocol>
, if you're using windows). It can only be producing an error in two places inboost/asio/detail/reactive_socket_service.hpp
:or
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 ofifdef
/elif
pairs inboost/asio/stream_socket_service.hpp
, and of those available only theregister_descriptor
function inepoll_reactor
can throw any error (and of coursedetail::win_iocp_socket_service<Protocol>.assign
can, also). The error inepoll_reactor
comes fromsys/epoll.h
, specifically:In the windows implementation, the related code is
I think this is the origin of your error, but honestly, I can't trace it past this point.