Search code examples
c#udpipv6udpclient

C# UDP server multiple instances ipv6 same port


I need multiple UDP servers, using the UDPClient class from .net. For IPv4 i can achieve this by doing the following:

var udpServer1 = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 53));
var udpServer2 = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 53));
var udpServer3 = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.3"), 53));

And it works, i can listen on all 3 addresses on port 53. I need to do the same for IPv6. But it seems that i can listen on only 1 loopback address "::1".

If i try to use "::2" i get a "The requested address is not valid in its context" error. Any help would be appreciated.

Thanks!


Solution

  • So, after some more investigation i found out that indeed, IPv6 has only 1 loopback address: "::1".

    BUT! There is a little thing called a "link-local" address, that starts with "fe80:..." and you have 1 of those unique to each of your network adapters, that represents the loopback address for that specific network adapter.

    So, i can open a server on ::1 port 53, or i can open multiple servers, one for each of the network adapters i do have.