InfiniTec - Henning Krauses Blog

Don't adjust your mind - it's reality that is malfunctioning

Implement an UDP multicast client/server communication

Affected products

  • .NET Framework 1.0
  • .NET Framework 1.1

Summary

This article explains, how to implement a multicast client/server communication using the UDP protocol.

Description

One feature of the UDP Protocoll is the support for multicast messages. This are messages, that are sent from one host to multiple clients. Additionally, the sending host does not need to know the destination of the packets. Instead, it sends the packet to a special IP address (in the range from 224.0.0.0 to 239.255.255.255). This address is called a multicast group.
The client, on the other side, registers itself to this address on the next hop on the route to the source host. This process is called "joining a multicast group". This next hop is either a router or the destination host. If it is a router, it will propagate this registration forward to the next router, until the destination host is reached.
The MSDN documentation contains an article that covers this topic, but it has two drawbacks:
The client component described in that article does only allow one client on one machine, and
the code doesn't work.

Solution

For the client component, use the following code:
multicastListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
multicastListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
multicastListener.Bind(new IPEndPoint(IPAddress.Any, 65000));
multicastListener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(new IPAddress("224.0.0.42"), IPAddress.Any));
This will create a socket that is bound to port 65000. The second line sets the socket in a non-exclusive state, thus allowing other process to bind to the same port. Finally, the fourth line will cause the socket to join the specified multicast group on all network interfaces.
The following code makes up the server part. It will create a socket, that will open the multicast group on the address 224.0.0.42, port 65000.
multicastSender = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp );
multicastSender.Bind(new IPEndPoint(IPAddress.Any, 0));
multicastSender.Connect(new IPEndPoint(new IPAddress("224.0.0.42", 65000)));
To send messages to the multicast group, the following code is used:
buffer = Encoding.UTF8.GetBytes("Hello world!");
multicastSender.Send(buffer);

More informations

The UDP protocoll is a connectionless, non-reliable protocol. This means, that no connection has to be established before one can send data. Further, it is neither guaranteed that the packets are received by the remote host in the order they were sent, nor that they are received at all.
The addresses 224.0.0.1 and 224.0.0.2 are reserved adresses. The first one is a global group, similar to the broadcast addresses of the IP protocol. The second one is a global adress for routers only.
UDP multicast is described in RFC 1112.
Not all router support the IGMP protocol, which is used for multicast group subscription. Especially over the internet, UDP multicast will most likely fail. It is therefore best suited for intranet applications.

Posted by Henning Krause on Friday, December 31, 2004 1:54 PM, last modified on Friday, December 31, 2004 2:26 PM
Permalink | Post RSSRSS comment feed