Socket listener for IPv6 and IPv4

若如初见. 提交于 2019-11-29 14:22:46

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.

It is fine either way.

If you bind v4 and v6 explicitly, you need to set IPV6_V6ONLY, otherwise, you need to clear it. The default setting varies between platforms.

Some platforms do not support accepting v4 connections on v6 sockets, so for maximum compatibility I'd go with the "two sockets" approach.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!