Reuse socket fails "Address already in use" or "Connection refused"
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)
Run Listener and Caller - ok. Stop Caller. Can rerun.
Stop Listener. Now neither will run, errors as above.
Much net search, but most of it old, and doesn't fix.
BOTH programs try setsockopt with SO_REUSEADDR, to no avail.
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!)
Minimised code below:-
first of all welcome to the linux forum :-)
You geht this 'address alrady in use' because the path name, in your case /var/ipcf/ipcfc, still exists when your program exists. When you then try to re-bind to this existing path, you get the error message, though you need to make sure that the file system path does not exist when you bind to it, e.g. by calling unlink(2)
Why the re-use doesn't work, I don't really know, yet ;-)
- Andi -
Yes, thanks Andi.
For now I have got round it by sharing an incremental new name on startup.
This has the spinoff that the caller can even start before the listener and wait after "connection refused", because it can tell when the new listener is available.
Problem bypassed with additional benefits.
Problem is still there and I would like to understand the proper cure, because surely I'm not the first to fall over this.
A single keyword in your answer is the key.
Immediately before Bind, call unlink(name) and ignore any error. This should mean the file is not there or is no longer there.
So I now have both bits in there, and no problems.
[ Still think the original problem is valid if anyone reads this far ]
|All times are GMT -5. The time now is 04:09 AM.|