| < Day Day Up > |
|
When a socket is opened and a local address is bound to it, the act of closing this socket causes it to enter the TIME_WAIT state. The purpose of this state is to ensure that the local address is not immediately used again, with the possibility of confusing the local stack by a remote socket that still believes that it’s connected. By waiting TIME_WAIT seconds (typically 120), one can be reasonably assured that remote connections will be closed as well.
The problem that TIME_WAIT creates is that the local address previously bound to the socket cannot be bound again within the TIME_WAIT period. Therefore, one must wait for the TIME_WAIT timeout to occur, or use the SO_REUSEADDR socket option. This option allows a socket to locally bind to the local address in the TIME_WAIT state. To set this option for a given socket, the following code snippet can be used:
int socket, on, ret; socket = socket( AF_INET, SOCK_STREAM, 0 ); on = 1; ret = setsockopt( socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
| < Day Day Up > |
|