UNIX socket connection refused

10,880

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

Share:
10,880
Zohar81
Author by

Zohar81

Updated on June 19, 2022

Comments

  • Zohar81
    Zohar81 almost 2 years

    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