Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP Multicast Sending (using JoinMulticastGroup) in C#

I know there are plenty of examples around the web regarding UDP multicasting in C#. This is more to get a clarification on the need to include the method JoinMulticastGroup when sending only. Most code examples I have come across nearly always include this method as part of the initialisation code. But surely if the program or class is only ever sending, then it is not required?

i.e. on another stackoverflow question someone uses the code

public void SendMessage(string message)
{
    var data = Encoding.Default.GetBytes(message);
    using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
    {
        var address = IPAddress.Parse("224.100.0.1");
        var ipEndPoint = new IPEndPoint(address, 8088);
        udpClient.JoinMulticastGroup(address);
        udpClient.Send(data, data.Length, ipEndPoint);
        udpClient.Close();
    }
}

Is the line udpClient.JoinMulticastGroup(address); not actually redundant in this case?

like image 344
Chris MC Avatar asked Nov 04 '25 09:11

Chris MC


1 Answers

JoinMulticastGroup is indeed for enabling the socket to receive multicast packets destined for that group address. If your client is only sending, then it's not strictly necessary.

However, it doesn't hurt, and does help make the code clear that you're "part of" that multicast group. In this way, if the requirements change in the future, and this application needs to receive packets, then it will already be part of the multicast group.

A source host sends data to a multicast group by simply setting the destination IP address of the datagram to be the multicast group address. Any host can become a source and send data to a multicast group. Sources do not need to register in any way before they can begin sending data to a group, and do not need to be members of the group themselves.

-- metaswitch.com

like image 125
Jonathon Reinhart Avatar answered Nov 05 '25 23:11

Jonathon Reinhart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!