[英]ECONNREFUSED on datagram Unix socket
What are the possible reasons for ECONNREFUSED when sending over a connectionless datagram Unix socket? 通过无连接数据报Unix套接字发送时,ECONNREFUSED的可能原因是什么?
Also any advice on how to debug this is welcomed as this problem is reproducible. 由于该问题是可重现的,因此也欢迎提供有关如何调试它的任何建议。
I get the error regardless if sendto()
or sendmsg()
is used. 无论使用sendto()
还是sendmsg()
我都会收到错误消息。
if ((sock = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
{
return 0;
}
unlink("/tmp/serv");
addr.sun_family = AF_UNIX;
strncpy(&addr.sun_path[0], "/tmp/serv", sizeof(addr.sun_path));
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
return 0;
}
sockaddr_un from;
int fromlen = sizeof(from);
if (recvfrom(sock, &i, sizeof(i),0,(sockaddr*)&from,(socklen_t*)&fromlen) < 0 )
{
//some error handling code
}
printf("from.sun_family=%d, from.sun_path=%s",from.sun_family,from.sun_path); // this prints, as expected "from.sun_family=1, from.sun_path=/tmp/client"
strncpy(&addr.sun_path[0], "/tmp/client", sizeof(addr.sun_path));
sendto(sock,"abc",3,0,(sockaddr*)&addr, sizeof(addr)); //this fails with ECONNREFUSED
From man 7 unix
: 从man 7 unix
:
ECONNREFUSED The remote address specified by connect(2) was not a listening socket. ECONNREFUSED connect(2)指定的远程地址不是侦听套接字。 This error can also occur if the target filename is not a socket. 如果目标文件名不是套接字,也会发生此错误。
In Linux, sendto
on Unix socket does the following : 在Linux中,Unix套接字上的 sendto
执行以下操作 :
1548 if (sock_flag(other, SOCK_DEAD)) {
1549 /*
1550 * Check with 1003.1g - what should
1551 * datagram error
1552 */
1553 unix_state_unlock(other);
1554 sock_put(other);
1555
1556 err = 0;
1557 unix_state_lock(sk);
1558 if (unix_peer(sk) == other) {
1559 unix_peer(sk) = NULL;
1560 unix_state_unlock(sk);
1561
1562 unix_dgram_disconnected(sk, other);
1563 sock_put(other);
1564 err = -ECONNREFUSED;
1565 } else {
1566 unix_state_unlock(sk);
1567 }
1568
1569 other = NULL;
1570 if (err)
1571 goto out_free;
1572 goto restart;
1573 }
In other words, there is no reader on the other end of the socket you send to, or the socket does not longer exist in the filesystem. 换句话说,发送到套接字的另一端没有读取器,或者该套接字在文件系统中不再存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.