无法连接到 os x 上的 unix 域套接字
我正在尝试连接到另一个进程创建的 unix 域套接字。 这就是我正在做的事情。当我运行该文件时,该文件存在于文件系统上。
socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (socketFd < 0) {
socketFd = -1;
return -1;
}
bzero(&address, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
size_t address_length = sizeof(address.sun_family) +
sprintf(address.sun_path, "/tmp/%s", COMMON_SOCKET);
connect(socketFd, (struct sockaddr *)&address, address_length);
我收到错误,errno 是 ENOENT。但在 /tmp 上执行 ls 显示文件就在那里。
我做错了什么?
I am trying to connect to unix domain socket created by another process.
here is what i am doing. the file is exists on the file system when I running it.
socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (socketFd < 0) {
socketFd = -1;
return -1;
}
bzero(&address, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
size_t address_length = sizeof(address.sun_family) +
sprintf(address.sun_path, "/tmp/%s", COMMON_SOCKET);
connect(socketFd, (struct sockaddr *)&address, address_length);
i got error and errno is ENOENT. but doing ls on /tmp show me the file is there.
what do i do wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对
address_length
感到困惑。它应该简单地是:或者实际上只是:
You're confused about
address_length
. It should simply be:or really just:
由于您使用的是 Mac OS X,因此您可以尝试:
socketFD = socket(PF_LOCAL, SOCK_STREAM, 0);
Snow Leopard 上不推荐使用 PF_UNIX。不确定 Mac OS 上的 AF_UNIX (至少套接字手册页没有显示它)。
Since your on Mac OS X, you can try:
socketFD = socket(PF_LOCAL, SOCK_STREAM, 0);
PF_UNIX is deprecated on Snow Leopard. Not sure about AF_UNIX on Mac OS (at least the socket man page doesn't show it).