Reputation: 2610
Following is my environment:
I am implementing a TCP socket server application.
While developing/debugging, the application might terminate after a connection was accepted, and, before the open sockets were gracefully shutdown & closed.
On such a scenario, the next execution of the application will fail binding to the listening port, only after waiting a minute or so binding will succeed again.
My assumption is that the Linux kernel has some cleanup mechanism to ~collect~ all sockets that were not gracefully shutdown, explaining why I have to wait ~1 min before the port is bind-able again.
Having that said, is there any way of avoiding this 1 min wait? Is there any way of forcing the OS to collect all sockets that were not gracefully shutdown?
Any help will be appreciated.
Nadav at Sophin
Upvotes: 0
Views: 337
Reputation: 182744
Sure there is, just set the SO_REUSEADDR
SOL_SOCKET
level option on the socket.
int yes = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes))
perror("setsockopt");
Upvotes: 4