[ACCEPTED]-Releasing bound ports on process exit-sockets

Accepted answer
Score: 29

Using SO_REUSEADDR socket option will allow 1 you to re-start the program without delay.

int iSetOption = 1;
setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption,
Score: 5

TCP/IP stack keeps port busy for sometime 5 even after close() - socket will stay in TIME_WAIT and TIME_WAIT2 state.

If 4 I'm not mistaken, usually it takes 2 minutes 3 so if you need to use the same port immediately 2 set SO_REUSEADDR option on your socket before binding, just 1 like Ivo Bosticky suggested.

Score: 4

Not exactly an answer to your question, but 4 for completeness:

On Windows you can set 3 the TcpTimedWaitDelay registry value to set the timeout 2 for releasing closed TCP connections to 1 as low as 30 seconds.

More Related questions