Tuesday, November 13, 2007

My good deed for today - UdpClient in C# joining a multicast group

Why are simple things so difficult! I spent a couple hours banging my head against the wall on this one. All I wanted to do was push out multicast UDP packets and pick them up from a C# program. The UdpClient is not very well documented, and the examples I found didn't work. So simply, this is what I had to do, marked in red. Now this works!

I hope I saved someone a minor headache.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace UbiSenseUdpClientTest
{
class UbiSenseUdpListener
{
private static readonly IPAddress GroupAddress =
IPAddress.Parse("224.237.248.237");
private const int GroupPort = 64555;

private static void StartListener()
{
bool done = false;

UdpClient listener = new UdpClient(GroupPort); <- even though the samples show the noargs constructor for UdpClient, you must specify the port you are going to use if you want to receive multicast packets
IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);

try
{
listener.JoinMulticastGroup(GroupAddress);
//listener.Connect(groupEP); <--- even thought the MSDN examples say to connect, don't connect before you receive, or you will sit and block at the receive below 'waiting for broadcast' below

while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive(ref groupEP);

Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}

listener.Close();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}

}

static void Main(string[] args)
{
StartListener();
}
}
}

8 comments:

Anonymous said...

It saved me a headache. Tried the MSDN ones which failed, then found this which works! Awesome! Thanks!

Anonymous said...

I tried the example exactly as displayed by you: sorry, I didn't work, since after writing "Waiting for broadcast" it hangs.

I followed you suggestions (marked in red).

Which groupPort should I use and which ip multicast address?

How will I determine the'm for my actual configuration.

Many thanks in advance,
Philip

Unknown said...

Hey anon...

The port and multicast group you select are really up to you, as long as you select the appropriate range between from 224.0.0.0 to 239.255.255.255. These addresses are used to establish groups, they don't point to a specific physical address on the network. A bit of a reference is here: http://www.tcpipguide.com/free/t_IPMulticastAddressing.htm

Make sure multicast is working on your network, and between your client and server, I have found this utility quite useful...

http://www.mikkle.dk/multicasttest/

You did notice in the code sample that the listener.Connect(groupEP) line was commented out, that was meant to indicate that you should not issue that connect.

Does any of that help? MC

Anonymous said...

An old post, but it saved quite a bit of time. Thanks man, very helpful!

Anonymous said...

Agree with previous comment: an old post but has bailed me out of a problem. This is THE way to receive multicast messages.

Anonymous said...

Even older now, but worked like a charm.

Thanks

Unknown said...

If it was old 4 years ago, then by golly it is ancient now. But since it worked out of the chute and helped me ... Kudos, man!
-Jesse

Unknown said...

PS: Now, if only it worked so smoothly on a client machine that has multiple NIC enabled. Sigh.
-Jesse