0
votes

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

1
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.jdweng
@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.P.T. Huff
@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.P.T. Huff
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?jdweng
@jdweng If I manually send a test multicast packet I get to that point, if I instead let it try to receive the packets I see coming in on wirehark it awaits forever on UdpReceiveResult result = await client.ReceiveAsync();P.T. Huff

1 Answers

0
votes

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.