You should always bind both sockets explicitly, with the IPv6 socket bound with IPV6_V6ONLY
.
Why? Cross-platform compatibility.
Windows by default requires that you explicitly bind on IPv4 and IPv6. Binding only to IPv6 will not implicitly bind to IPv4 as well.
Linux by default will implicitly bind to IPv4 as well when you bind on IPv6, only if the net.ipv6.bindv6only
sysctl is set to 0
. Distributions such as Debian change this default to 1
, breaking your assumption.
I can't remember what Mac OS X does here (someone chirp in the comments please?), but the point is that explicitly binding to both protocols leaves no surprises.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…