cannot receive multicast udp packets

Hamza Outa 61 Reputation points
2023-03-21T11:28:16.91+00:00

I have a C# script that listens for UDP-packets sent by some devices to a broadcast address (mDNS). But for some reason I cannot receive these packets on my work laptop. The strange thing is when I run the same script on my private laptop I can receive the packets.

I also have a program from the device manufacturer that can receive these packets on my work laptop. And when I checked the IP rules i saw that nothing was blocking the port.

My question now is where do i need to look to resolve this issue. I tried turning the firewall off and adding extra ip rules for the port.

Both laptops are on the same network and running window 11. I am using a usb-c to ethernet adapter.

My code:

public void cameraListener()
        {
            // Multicast IP-address
            IPAddress ip = IPAddress.Parse("224.0.0.251");

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));// Add socket to the multicast group

            IPEndPoint listener = new IPEndPoint(IPAddress.Parse(localIP), 5353);

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint senderRemote = (EndPoint)sender;

            socket.Bind(listener);
            Console.WriteLine("server started listening\n");

            List<string> IPList = new List<string>();
            string cameraIP;


            // Receive messages
            while (_running)
            {

                byte[] data = new byte[3024];
                socket.ReceiveFrom(data, ref senderRemote);
                string str = System.Text.Encoding.ASCII.GetString(data);
                // Console.WriteLine(str);

                cameraIP = senderRemote.ToString();
                // Console.WriteLine(cameraIP);
                // Console.WriteLine(senderRemote.ToString());
                
                if(!IPList.Contains(cameraIP) && cameraIP != (localIP + ":5353")){
                    IPList.Add(cameraIP);
                }               

                Console.WriteLine("printing the list");
                IPList.ForEach(p => Console.WriteLine(p));
                Console.WriteLine("finished printing list\n");
            }
        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,808 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,483 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,221 Reputation points
    2023-03-22T15:14:54.0166667+00:00

    Hello there,

    Are you listening for responses on the same port that you sent the query to? Are the machines responding to your broadcast query sending their responses to the same source address and port number that they received them from?

    Make sure you are sending and receiving on the same UDP port and test on a fresh, unmodified Windows system.

    Usually, the firewall will automatically allow you to receive data on whatever port you sent data on.

    Your receiver should not bind to a local address. It should instead bind to either INADDR_ANY or the multicast address you intend on joining. Binding to a local address breaks multicast on Linux systems.

    Note that if you bind to a multicast address, this means you'll only receive packets for that multicast address. If you want to receive from multiple multicast addresses or if you also want to receive unicast packets then you need to bind to INADDR_ANY.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.