1
votes

I am writing a couple of functions to allow me to send out a UDP broadcast / multicast using both IPv4 and IPv6. The follow code does this. The problem I have is that it only does this for a single adapter. If I have two network adapters fitted to my PC it doesn’t send the broadcast out on both. Is it possible to have a single socket configured to handle both IPv4 and IPv6 and to send and receive on all NICs? Or do I have to create separate sockets for each IP address?

    public void CreateBroadcaster(CancellationToken cancellationToken, int discoveryPort, int advancePort)
    {
        _cancellationToken = cancellationToken;

        _broadcastEndpoint = new IPEndPoint(IPAddress.Broadcast, advancePort);

        var epLocal = new IPEndPoint(IPAddress.IPv6Any, discoveryPort);

        _broadcaster = new UdpClient(epLocal);

        var soc = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
        soc.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

        _broadcaster.Client = soc;

        _broadcaster.Client.DualMode = true;

        _broadcaster.Client.Bind(epLocal);

        _broadcaster.EnableBroadcast = true;
        _broadcaster.MulticastLoopback = true;

        _broadcaster.Client.SetSocketOption(
            SocketOptionLevel.IPv6,
            SocketOptionName.AddMembership,
            new IPv6MulticastOption(IPAddress.Parse("ff02::1")));
    }

    public void SendPingsOnAdapterLocalSubnets()
    {
        _broadcaster.Send(_sendData, _sendData.Length, _broadcastEndpoint);
    }
1
You need 2 separate sockets. On the low level, the sockaddr struct will have one or other. IPv4 or IPv6. gnu.org/software/libc/manual/html_node/…ntohl

1 Answers

0
votes

You need 2 separate sockets. On the low level, the sockaddr struct will have one or other. IPv4(AF_INET) or IPv6(AF_INET6).

http://www.gnu.org/software/libc/manual/html_node/Address-Formats.html#Address-Formats