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

13,274

Solution 1

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

Solution 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)

Share:
13,274
Christopher Tarquini
Author by

Christopher Tarquini

Updated on June 04, 2022

Comments

  • Christopher Tarquini
    Christopher Tarquini almost 2 years

    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.

  • Matthew Iselin
    Matthew Iselin almost 15 years
    silky: I'd say you're right. I've totally rewritten my answer.
  • Admin
    Admin about 10 years
    Looks like one of your links may have evaporated.