C# server that supports IPv6 and IPv4 on the same port

半城伤御伤魂 提交于 2019-12-10 15:55:28

问题


Is it possible to have a Socket that listens and accepts both IPv6 and IPv4 clients? I used a IPv6 socket in C# hoping that it would automatically be backwards compatible but IPv4 clients cause an invalid ip address exception.


回答1:


Have a look here. You can accept IPv4 clients as well as IPv6 clients with the one server socket.




回答2:


Set the socket's IPv6Only option to false:

Socket MySocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
MySocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

(taken from Matthew Iselin's second link)



来源:https://stackoverflow.com/questions/1285953/c-sharp-server-that-supports-ipv6-and-ipv4-on-the-same-port

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