A tcp connection cannot be established when there is a tcp connection with the state `TIME_WAIT`

I wrote a simple tcp client and server and started them. I notice that when I use the ctrl+ctcp connection to close the program, the tcp connection state is similar TIME_WAIT if I started the client again and the tcp server the tcp connection cannot be established again a RSTthe packet is always sent by the tcp server I need to wait a while until the next tcp connection is established.

what is the reason for this and how to deal with this problem?

In addition, I notice on one host that the tcp connection is in a state FIN_WAIT_2 even if it does not receive FIN, the tcp connection will be closed after a while Why?

thank

+1
source share
1 answer

The solution to this problem is to set the SO_REUSEADDR option. This tells the stack to allow binding to addresses that are in TIME_WAIT state.

The reason for the TIME_WAIT state seems to be to allow some time for debug packages. If the stack cannot verify that the connection has been gracefully disconnected, it may be that packets are in transit or the sender may even send more data actively. The stack wants to avoid mixing these packets with recently connected connection traffic.

Here is a good answer about using SO_REUSEADDR for TCP and UDP.

+2
source

All Articles