[英]Unix domain socket bind failed in windows
I'm running the following code in order to create listener to unix domain socket.我正在运行以下代码以创建 unix 域套接字的侦听器。 Under macOS this code is working fine, but in Windows it produces the following error from the tcp_acceptor command: WSAEOPNOTSUPP
在 macOS 下,此代码运行良好,但在 Windows 中,tcp_acceptor 命令会产生以下错误: WSAEOPNOTSUPP
Here's a minimal reproducible example:这是一个最小的可重现示例:
#include <iostream>
#include <boost/asio/local/stream_protocol.hpp>
constexpr char* kFileName = "file.sock";
using namespace std;
using namespace boost::asio;
int main(int argc, char* argv[])
{
io_context my_io_context;
::_unlink(kFileName); // Remove previous binding.
local::stream_protocol::endpoint server(kFileName);
local::stream_protocol::acceptor acceptor(my_io_context, server);
local::stream_protocol::socket socket(my_io_context);
acceptor.accept(socket);
return 0;
}
While debugging inside the boost library, i saw that the failure comes from the internal bind in the following code:在 boost 库中调试时,我看到失败来自以下代码中的内部绑定:
and this is the frame variables (it's clearly visible that sa_family = AF_UNIX (1):这是框架变量(可以清楚地看到 sa_family = AF_UNIX (1):
I know that unix domain socket was introduced in windows10 few years ago, and i'm working with the latest version so it should be supported.我知道 unix 域套接字是几年前在 windows10 中引入的,我正在使用最新版本,因此应该支持它。 Any idea what's wrong in my code?知道我的代码有什么问题吗?
EDIT: I've found out that in linux based machine I pass the following sockaddr to::bind编辑:我发现在基于 linux 的机器中,我将以下 sockaddr 传递给 ::bind
(const boost::asio::detail::socket_addr_type) *addr = (sa_len = '\0', sa_family = '\x01', sa_data = "/tmp/server.sock")
(lldb) memory read addr
0x7ffeefbffa00: 00 01 2f 74 6d 70 2f 73 65 72 76 65 72 2e 73 6f ../tmp/server.so
0x7ffeefbffa10: 63 6b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ck..............```
and in windows i get a slightly different struct:在 windows 中,我得到了一个稍微不同的结构:
{sa_family=1 sa_data=0x000000fffd33f682 "C:\\temp\\UnixSo... }const sockaddr *
Notice that the len
field is missing in the windows platform.请注意,windows 平台中缺少len
字段。
Thanks谢谢
The issue seems to be the SO_REUSEADDR
socket option, which ASIO by default sets.问题似乎是 ASIO 默认设置的SO_REUSEADDR
套接字选项。 Setting this option itself succeeds, but causes the subsequent bind
to fail.设置此选项本身会成功,但会导致后续bind
失败。
Construct the acceptor with reuse_addr
= false
, then the binding should succeed:使用reuse_addr
= false
构造接受器,然后绑定应该成功:
local::stream_protocol::acceptor acceptor(my_io_context, server, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.