This is the code snippet (from beejus) where I am trying to create a process which will create a thread which will act as a Unix Domain Socket Server.
void *domain_socket_cepki_server() {
s = socket(AF_UNIX, SOCK_STREAM, 0);
fprintf(stderr, "socket val is : %d\n", s);
if (s == -1) {
perror("socket");
fprintf(stderr, "SOCKET ERROR: %d\n", s);
}
memset(&local, 0, sizeof(struct sockaddr_un));
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
//len = strlen(local.sun_path) + sizeof(local.sun_family);
rc = bind(s, (struct sockaddr *) &local, sizeof(local));
if (rc == -1) {
perror("bind");
fprintf(stderr, "bind failed %d : %s", errno, strerror(errno));
exit(1);
}
if (listen(s, 5) == -1) {
perror("listen");
fprintf(stderr, "listen failed : %s", strerror(errno));
exit(1);
}
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
perror("accept");
exit(1);
}
// send and recv code further.
}
With this I am getting following error: bind: Invalid argument
bind() EINVAL says the following:
EINVAL The socket is already bound to an address.
EINVAL addrlen is wrong, or addr is not a valid address for this socket's domain.
What can go wrong in this bind() system call ?