0

I'm adding a multicast listening feature for an application I'm developing. A device I'm connecting to will send out a multicast packet every second in certain scenarios, and I'd like the user to be able to listen for that packet (if it's being sent).

I can see the multicast packets being continuously sent from the device using Wireshark, so I know they exist and I know I'm using the correct multicast group and port number. I've tried dozen of different ways without any luck to get my application to capture the packets. If I send a test multicast packet from the application itself it receives it no problem. I've tried to receive the packets both async and sync, no change. I'm really stumped on this one and not sure what I'm doing wrong. Every example I've found leads me to believe this should be working.

My multicast udp listener class:

   class MulticastClient
{
    private UdpClient client;
    private IPEndPoint localEp;
    private IPAddress multicastAddress;

    public byte[] receivedData { get; private set; }

    public MulticastClient(string multicastIP, int port)
    {
        localEp = new IPEndPoint(IPAddress.Any, port);
        client = new UdpClient(AddressFamily.InterNetwork);

        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        client.ExclusiveAddressUse = false;

        client.Client.Bind(localEp);

        this.multicastAddress = IPAddress.Parse(multicastIP);
        client.JoinMulticastGroup(this.multicastAddress);            
    }

    public async Task Listen()
    {
        UdpReceiveResult result = await client.ReceiveAsync();
        receivedData = result.Buffer;    
    }
}

The button click that kicks it off:

MulticastClient client = new MulticastClient("239.255.0.1", 32768);

        await Task.Run(async () =>
        {
            await client.Listen();
        });

        byte[] receivedData = client.receivedData;

        if (receivedData != null)
        {
            //Here I display useful information about the packet to the user
        }

Here is a snippet of a packet capture, showing the multicast packets that I can't seem to receive: Wireshark packet capture

7
  • How do you expect to get to the following line of code : byte[] receivedData = client.receivedData;? You are sitting forever in the Task above. Go to Debug Break All and see where the code is running. Commented Apr 16, 2020 at 18:02
  • @jdweng You're right, that is leftover from when i was trying to receive using a sync method instead. It'll never get to that point since I'm using await. Commented Apr 16, 2020 at 18:29
  • @jdweng I've been trying too many different things.I edited with the while(true) removed, still never receive the packets even though i can see them incoming on Wireshark. Commented Apr 16, 2020 at 18:42
  • Put a break point on receiveData. Are you getting there? If you press F11 are you getting to next instruction? If you hover over receivedData is it null or does it contain bytes? Commented Apr 16, 2020 at 19:03
  • 1
    Does the machine have more than one the one network card or IP? The port could be blocked or there is something wrong with the IP/Mask. From cmd.exe >IPConfig/all (will list all the interface)Also NetStat -A (will tell if when running if listener is running). It could be another application is receiving the data instead of yours. IPANY should receive data from all interfaces. Not sure why is fails. So I would run Netstat -a when you application is not running and see if any app is using the port. Then run Netstat -a and see if app is listening. Commented Apr 16, 2020 at 20:45

1 Answer 1

0

Got it to work using a variation of this post.

@jdweng made me realize it when he/she asked about multiple adapters, I had several virtual NICs for VMs that were likely joining ht multicast group instead of my actual hardware NIC.

Sign up to request clarification or add additional context in comments.

1 Comment

Any chance you can post the solution. I am having issues with multi cast, on a nexus 5 everything works as expected, on a pixel 3 it doesn't work at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.