Socket bind fails even after using SO_REUSEADDR with IPv6

前端 未结 1 604
情话喂你
情话喂你 2021-01-27 02:27

My application needs to bind a socket on the same port with the same IPv6 address. I am using the code below to achieve the same. However second bind throws an error \'Address a

相关标签:
1条回答
  • 2021-01-27 02:44

    From the socket(7) man page:

    SO_REUSEADDR - Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For AF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listening socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address. Argument is an integer boolean flag.

    (emphasis mine)

    Your first socket is bound to port 143 on the IPv6 equivalent of INADDR_ANY, then it's put in listening state. Thus no other socket could be bound on port 143 as long as the first socket remains open.

    0 讨论(0)
提交回复
热议问题