Why is the port not immediately freed after closing the socket?

I found that by default, the OS does not immediately release the port that my server socket uses after the server shuts down. By providing SO_REUSEADDRwhen setting up the socket, you can avoid this problem, but I do not understand why it is useful to hold the port for a while. If the server shuts down, the socket closes, will any data transferred to this port not be processed anyway?

+4
source share
2 answers

When the port is released, it enters the state TIME_WAITto prevent duplication of packets that were delayed on the way to the first connection, which will be delivered to the second connection.

, TIME_WAIT:

  • ( a, p) ( b, q)
  • ( a, p) ( b, q)
  • , .

, , . , TIME_WAIT .

+5

- , (TCP) (UDP), , "" . - , .

SO_REUSEADDR , ( - ).

+1

Source: https://habr.com/ru/post/1532753/


All Articles