I'm familiarising myself with Unix Sockets for Inter-Process Communication, and following a guide here. Each time I run the program that acts as a server (referred to as echos.c in the guide), I am getting an error that says bind: Address already in use
, except for the first time I run it after deleting all build files.
The error is coming from this section of code:
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, s2, len;
socklen_t t;
struct sockaddr_un local, remote;
char str[100];
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}
// ...
In the initial run, the program creates a socket file called echo_socke
(I have checked this with ls -l
, and it is strange that the name is missing the 't' to make it echo_socket
), and then goes on to successfully bind
and listen
for the client process.
However, in subsequent runs where I do not delete the build files, I get the error bind: Address already in use
, coming from the lines
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
Doing additional testing I found that unlink(local.sun_path)
is returning a No such file or directory
error, so I figure the socket file that the bind
is attempting to access is not being found. Could this be due to the file name missing a character as mentioned earlier? (But it works on the first time around, so it can't be?). Any help figuring out what is going on would be much appreciated.