Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

ipv6

ipv4

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.

like image 321
Christopher Tarquini Avatar asked Aug 17 '09 02:08

Christopher Tarquini


2 Answers

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)

like image 195
Matumba Avatar answered Oct 14 '22 10:10

Matumba


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

like image 26
Matthew Iselin Avatar answered Oct 14 '22 10:10

Matthew Iselin