c - Linux 套接字 (AF_UNIX) 连接 () 失败

标签 c linux sockets

这周我一直在学习 unix 套接字,我对来自客户端应用程序的 connect() 调用有疑问。

是 connect() 函数调用 BLOCKING。在服务器应用程序创建自己的套接字、将其绑定(bind)到同一地址并开始监听传入连接之前,它会阻塞吗?

在我的客户中,我拥有的是

printf(" abc %s\n", bind_path);
if((retval = connect(sh->sock_fd, (const struct sockaddr*)&sadr, sizeof(struct sockaddr_un))) == -1)
{
      perror("123");
      return SOCKET_ERROR;
}
return SOCKET_OK;

我得到的是,

ankit@ankit-ThinkPad-W530:~/Desktop/week2_3_tasks/ipc_socket_exercise$ ./user_application_proc2_client.out 
**** Process 2 (Client) Started
**** Process 2 PID : 7106
/tmp/sock_10 
**** Socket connected successfully with handle 3
 abc /tmp/sock_10
123: No such file or directory
**** Error connnecting socket to address .. exiting

最佳答案

Is the connect() function call BLOCKING[?]

不适用于 UNIX 套接字。它可能会阻塞其他类型的套接字(例如 IP 套接字)。

Will it BLOCK till the server application creates its own socket, binds it to the same address and start listening for incoming connections?

没有。你自己看到了这个——你得到了一个“没有这样的文件或目录”的错误!显然它在返回该错误之前没有等待服务器。

关于c - Linux 套接字 (AF_UNIX) 连接 () 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891599/

相关文章:

c - 在 C 中引用 fork() 的库

C - 没有循环的位操作

linux - 相当于 Linux 内核 3.18 中的 i2c_adapter_quirks

QT QTcpServer::incomingConnection(qintptr handle) 没有触发?

c - C 和 Go 之间的抽象 Unix 套接字

c - 创建TCP服务器的三个问题——socket,Linux with C

c - 如何使用 C 语言中的文件编写凯撒密码加密代码

c - [C]-将字符串拆分为 2 个字符串

linux - Nginx 没有重定向到 404 页面

linux - 在 XUbuntu 中运行 Genymotion 时出现符号查找错误