10

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;
}
7
  • please post the exact error message you are seeing.
    – xaxxon
    Jul 3, 2013 at 15:28
  • The error displayed is this: "Error "Address already in use" in bind()"
    – Acsor
    Jul 3, 2013 at 15:31
  • hey, DO NOT use a dropbox filesystem for AF_UNIX. It really needs to be a local filesystem. You can google around for why. Here there be dragons, so until you try it on a local filesystem and reproduce it, I don't think anyone can help you.
    – xaxxon
    Jul 3, 2013 at 15:32
  • It sounds pretty strange, since the Dropbox folder is located on my filesystem. Anyway I'm gonna try it.
    – Acsor
    Jul 3, 2013 at 15:36
  • 1
    also, are you making sure to unlink the file if it exists? beej.us/guide/bgipc/output/html/multipage/unixsock.html
    – xaxxon
    Jul 3, 2013 at 15:40

2 Answers 2

25

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.

2
  • 1
    yeah, I just saw this, too
    – xaxxon
    Jul 3, 2013 at 15:41
  • Yes, just tried and it works. Anyway, thanks to both and +1 to xaxxon.
    – Acsor
    Jul 3, 2013 at 15:49
-4

You can try to use the SO_REUSEADDR flag like so:

int yes = 1;
if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    // error handling
    exit(1);
}
1
  • I read some stuff that while it's not strongly defined that this isn't meaningful for non network protocols.
    – xaxxon
    Jul 3, 2013 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.