Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Receiving UDP Multicast on multiple interfaces

$
0
0

Hello,

I have a LAN and a Wireless LAN on my computer. I try to send and receive UDP-Multicast packets.

Sending works well, even on all netwrok devices, after inserting this.. (which I have found many times in internet)

                    IPAddress ip = IPAddress.Parse("239.255.255.250");
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface adapter in nics)
                    {
                        IPInterfaceProperties ip_properties = adapter.GetIPProperties();
                        if (!adapter.GetIPProperties().MulticastAddresses.Any())
                            continue; // most of VPN adapters will be skipped
                        if (!adapter.SupportsMulticast)
                            continue; // multicast is meaningless for this type of connection
                        if (OperationalStatus.Up != adapter.OperationalStatus)
                            continue; // this adapter is off or not connected
                        IPv4InterfaceProperties p = adapter.GetIPProperties().GetIPv4Properties();
                        if (null == p)
                            continue; // IPv4 is not configured on this adapter
                        var ipaddress = IPAddress.HostToNetworkOrder(p.Index);
                        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)ipaddress);
                    }

But I have got a problem with receiving:

            var multicastgroup = System.Net.IPAddress.Parse("239.255.255.250");

            try
            {
                ReceiverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                var hardwareToBind = new IPEndPoint(IPAddress.Any, 1900);
                ReceiverSocket.Bind(hardwareToBind);

                try
                {
                    ReceiverSocket.SetSocketOption(SocketOptionLevel.IP,
                        SocketOptionName.AddMembership,
                            new MulticastOption(multicastgroup, IPAddress.Any));
                }
                catch
                {
        
                }

                ReceiverSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, _PacketReceived, this);
            }
            catch
            {
            }

It works well, as long as I disable the Wireless LAN. As soon as I enable the Wireless LAN, nothing is received anymore. But with wireshark, I can see, that the device is receiving UDP-Packets with the correct content. Only my program isn't aware of it.

What am I doing wrong?


Viewing all articles
Browse latest Browse all 8156

Trending Articles