PDA

View Full Version : Socket bind error



rburge
20th September 2007, 23:18
I have a program that has been in the field for over a year now. Once in awhile I get a call that it is not working and when I take a look it is because the program is getting a report that the socket is bound to another process. I have put in code to try and capture shutdown events and close the socket more gracefully and this has helped.

It seem that the qtsocket class must be spawning another process that binds the listner. When the main program dies this phantom process is still there tying up the socket resource. You can query the OS and sure enough it thinks there is a process attached but can't tell you what is attached.

I've seen the posts that talk about setting the address for resuse, that's fine but it looks to be a bug in the qtsocket class. There is a companion program that uses native socket binding and it never has this problem.

I have experienced this problem on both Unix and Linux OS with UNIX seeming to be more prone to the problem.

Anyone who thinks they have dealt with this succesfully I would love to hear form you. This problem has been plaguing me for a year now.

Thanks

wysota
21st September 2007, 01:18
It seem that the qtsocket class must be spawning another process that binds the listner. When the main program dies this phantom process is still there tying up the socket resource. You can query the OS and sure enough it thinks there is a process attached but can't tell you what is attached.
No, it's nothing like that. It is the operating system that disallows to bind to a socket which has been ungracefully closed a while earlier. This is to prevent a situation where a new process that binds to the socket receives stale data that should have been received by the old process (take into consideration the fact that if a process dies, the other side of connection is not aware of that and might try transmitting some data).


I've seen the posts that talk about setting the address for resuse, that's fine but it looks to be a bug in the qtsocket class.
Qt sockets have nothing to do with it.

There is a companion program that uses native socket binding and it never has this problem.
The companion program must be preventing the default behaviour then. Try a simple program that calls socket(), bind(), listen(), accept() and then kill the process and try restarting it. You'll see that the bind fails. Or look into Qt sources to see how the socket is implemented - it calls native bind().


I have experienced this problem on both Unix and Linux OS with UNIX seeming to be more prone to the problem.
As I said, it's not a problem, it's a failsafe.


Anyone who thinks they have dealt with this succesfully I would love to hear form you. This problem has been plaguing me for a year now.
The simplest solution is to improve your application so that it doesn't crash ;) Or just set SO_REUSE on the socket as advised.