Problem: can't restart programs which use socket to communicate.
Symptom: Restart Listener dies "Address already in use".
Restart Caller dies "connection refused"
System: Ubuntu 10 (and 9)
General description.
Run Listener and Caller - ok. Stop Caller. Can rerun.
Stop Listener. Now neither will run, errors as above.
Tried?:-
Much net search, but most of it old, and doesn't fix.
BOTH programs try setsockopt with SO_REUSEADDR, to no avail.
Of note:
They are using AF_UNIX. (much of the net stuff is for reuse of
ports on AF_INET.
I'm sure it's too blatant to be anything but a problem with this newbie, but any help would be appreciated - I'm stuck. (I wish "connection refused" would say
why!)
Thanks
Minimised code below:-
Listener:-
Code:
struct sockaddr_un sockaddr ;
int sd;
int turniton;
char wait;
if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
printf("Can't get socket\n");
turniton = 1;
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &turniton, sizeof(turniton)) < 0)
printf("can't set reuse - %s\n", strerror(errno));
memset(&sockaddr, '\0', sizeof(sockaddr)) ;
sockaddr.sun_family = AF_UNIX ;
strcpy(sockaddr.sun_path, "/var/ipcf/ipcfc") ;
if (bind(sd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
printf("bind failed - %s\n", strerror(errno)) ;
wait = getchar();
close(sd);
----------------------
Caller
Code:
struct sockaddr_un sockaddr ;
int sd ;
int turniton;
sd = socket(AF_UNIX, SOCK_STREAM, 0) ;
if (!sd)
printf("no sd!\n");
turniton = 1;
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &turniton, sizeof(turniton)) < 0)
printf( "Can't set reuse - %s\n", strerror(errno));
memset(&sockaddr, '\0', sizeof(sockaddr)) ;
sockaddr.sun_family = AF_UNIX ;
strcpy(sockaddr.sun_path, "/var/ipcf/ipcfc") ;
if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
printf("connect failed - %s\n", strerror(errno));
close(sd);