Releasing bound ports on process exit

前端 未结 3 544
野趣味
野趣味 2020-12-05 08:04

How do I make sure that a socket bound to a port is properly released on process exit such that the port can be reused without bind() failing with EADDRINUSE? I

3条回答
  •  余生分开走
    2020-12-05 08:39

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

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

提交回复
热议问题