Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX socket connection refused

Under OS-X, I've got process named 'listener' that is waiting on 'accept' to read data from local unix socket named listener_socket. unfortunately, any attempt to connect that socket terminate in 'connection refused' error.

Using lsof, to make sure that the 'listener' actually listen to this socket :

sudo lsof -p 570
COMMAND PID USER   FD     TYPE             DEVICE   SIZE/OFF   NODE NAME
...
listener  570 root    3u    unix 0x48a2751a1bad61ef        0t0        /private/var/run/my_sockets/listener_socket

Notice that the file is, in fact, a valid unix socket :

file /private/var/run/my_sockets/listener_socket /private/var/run/my_sockets/listener_socket: socket

However, it still fail to connect, even when i'm using an alternative way from command like (using socat command)

sudo socat LOCAL:/private/var/run/my_sockets/listener_socket,interval=1 EXEC:'aaaaaaaaaaaaaaaaa',nofork

2015/11/23 00:57:33 socat[928] E connect(3, LEN=49 AF=1 "/private/var/run/my_sockets/listener_socket", 49): Connection refused

perhaps there are more i can do to figure out why i cannot send data to the socket, even-though it's obvious that 'listener' waiting for this data on the other side ?

here's the relevant part of my code :

sender:

sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "%s", LISTENER_SOCKET_PATH);
connect(sockfd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)
write ...

receiver:

fd = socket(PF_UNIX, SOCK_STREAM, 0);
unlink(sock_name); // in case the socket is used before
listen(server->fd, 5); // we don't reach 5 listeners for sure ...
chmod(sock_name, mode); // giving root privilages
accept(server->fd, (struct sockaddr *) &server->address, &server->address_length);
read ...

thanks

like image 690
Zohar81 Avatar asked Nov 23 '15 09:11

Zohar81


People also ask

What is Unix socket connection?

A Unix domain socket aka UDS or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. It is also referred to by its address family AF_UNIX .

Are UNIX sockets blocking?

The traditional UNIX system calls are blocking. For example: accept() blocks the caller until a connection is present. If no messages space is available at the socket to hold the message to be transmitted, then send() normally blocks.

Does Unix socket use TCP?

Show activity on this post. When the host is "localhost", MySQL Unix clients use a Unix socket, AKA Unix Domain Socket, rather than a TCP/IP socket for the connection, thus the TCP port doesn't matter.

Is Unix domain socket reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.


1 Answers

The server seems to miss calling bind() on the listening socket.

like image 80
alk Avatar answered Oct 11 '22 04:10

alk