Reputation: 43518
I developed a service which contains a tcp server.
When I restart my application, sometime my application is unable to bind the port for the TIME_WAIT reason.
In my application I want to add a procedure when the bind fails. this procedure should check at the beginning the type of the bind fails:
TIME_WAIT
, then wait amoment and
retry again.How I can know the type of the bind fails?
NOTE:
SO_REUSEADDR
errno
is the same for both type of failsUpvotes: 1
Views: 1330
Reputation: 29
Firstly, I would use the SO_REUSEADDR
sockopt on your listening socket to avoid this situation in the first place. SO_REUSEADDR
will allow you to reuse the same socket your TCP server was using previously upon restart because it is still being held open by the operating system.
Secondly, error handling is always a good idea. I encourage you to check the return value of the bind
and handle the most likely errno
you expect to encounter. You can get an errno
list for bind
in section 2 of the man page.
$> man -s 2 bind
Finally, the errno
for a socket in TIME_WAIT
is the same errno
as an address in use: EADDRINUSE
. This is because a socket in TIME_WAIT
is in use by the OS.
Upvotes: 2