Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Address already in use" error using Unix socket

Writing the C source below using Unix local sockets I got an error about the address already in use. After having checked man 7 Unix for further informations I tried to create a sub-folder where executing my program (obviously modifying the sun_path field on the current folder) but the error was ever the same.

Is there someone able to help me?

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>

#define MAXLEN  128

int main (int argc, char *argv[]){

        struct sockaddr_un      server;
        int                                     serverfd, clientfd;
        socklen_t                       addrsize = sizeof(struct sockaddr_un);
        char                            buff[MAXLEN], *path;

        if (argc < 2){
                printf("Error: %s [MESSAGE]\n", argv[0]);
                return 1;
        }

        if ((serverfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
                printf("Error \"%s\" in socket()\n", strerror(errno));
                exit(1);
        }
        puts("socket()");

        server.sun_family = AF_UNIX;
        path = strcpy(server.sun_path, "/home/myhome/Dropbox/Sources/C/sub");

        printf("[DEBUG]Address bound at %s\n", path);

        if ((bind(serverfd, (struct sockaddr*)&server, addrsize)) < 0){
                printf("Error \"%s\" in bind()\n", strerror(errno));
                exit(1);
        }
        puts("bind()");


        if ((listen(serverfd, 1)) < 0){
                printf("Error \"%s\" in listen()\n", strerror(errno));
                exit(1);
        }

        if ((clientfd = accept(serverfd, NULL, &addrsize)) < 0){
                printf("Error \"%s\" in accept()\n", strerror(errno));
                exit(1);
        }

        write(clientfd, argv[1], strlen(argv[1]));
        read(clientfd, buff, sizeof(buff));

        puts(buff);

        close(clientfd);
        close(serverfd);
        return 0;
}
like image 892
Acsor Avatar asked Jul 03 '13 15:07

Acsor


People also ask

Why am I getting address already in use error when binding?

Sometimes you try to bind () and have an error "Address already in use". Someone is still handling the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port:

How to fix the IP/port are in use error?

The error is clear: The IP/port are in use. Try to gess what, among all the ports your app uses, is the one in use. To do so, use lsof if it's available on your system or netstat if it's not. Regards. Sometimes you try to bind () and have an error "Address already in use".

How do sockets work in Linux?

This process binds its socket to a known location and accepts incoming connection requests from clients. For each connection request that is received, a new socket is created that is used to communicate with the peer socket ( peer socket = the socket at the other end of the connection, in this case the socket created by some client process).

What are the different types of sockets in Unix?

On a related note, there are two main domains of sockets: Unix domain sockets, which allow processes on the same computer to communicate (IPC), and Internet domain sockets, which allow processes to communicate over a network.


1 Answers

You should unlink() the path file before bind call. You will get this error when file exists during the bind. Either you should ensure to unlink/remove the file before exiting the application or you could always unlink it before bind.

Check man page of bind. Also, note the example given in the man page at the end.

like image 139
VoidPointer Avatar answered Sep 19 '22 15:09

VoidPointer