Click here to Skip to main content
15,617,116 members
Articles / Programming Languages / C#
Posted 1 Jan 2002


198 bookmarked

IP Multicasting in C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (42 votes)
9 Jan 20024 min read
A simple client/server implementation of multicasting in C# .NET Framework


This document provides a simple client/server example for setting up a multicast application in C# .NET as well as the method in which multicasting works and why it is useful now and how, with the increasing availability of bandwidth and the increased use of streaming media, will become a definitive method of data transmission in the near future.

What is IP Multicasting

"IP Multicasting is a bandwidth-conserving technology that reduces traffic by simultaneously delivering a single stream of information to thousands of corporate recipients and homes."CSCO01.

The concept behind multicasting can be thought of as the same as terrestrial and satellite based television broadcasting; there is a single satellite or transmitting mast and many receivers. This works because there is no back-channel to the source and routers along the path to receivers are able to create multiple copies of the stream to pass on to other routers and hosts along the route. The process works well as the implementation of the Internet is similar to driving from one side of the country to the other, you will follow various routes and join new ones where necessary that are en-route to your destination.

Requirements of IP Multicasting

A few things must be in place before developing multicasting applications or migrating unicast applications.

Network Requirements

For IP multicasting to work, all the routers along the path of communication must be multicast enabled. For internet multicasting, devices must transmit and be a member of the MBONE, which is basically a group of devices within a virtual network that support IP multicasting traffic. The MBONE is at best a temporary utility for all multicasting enabled, in time, the MBONE will fade away and just become another part of the Internet as more and more manufacturers produce routers that support multicasting.

System Requirements

The operating system networking interface must support multicasting. Berkeley Sockets, Windows Sockets 2 and Apple Macintosh Open Transport all support multicasting - I guess this is important as .NET will (hopefully) eventually be ported to all these operating systems.

An IP Multicast Application

RFC1112 "Host Extensions for IP Multicasting" ( recommends a number of API calls that should be available for multicast support. These are:

  • Join a multicast group
  • Leave a multicast group
  • Set the TTL of a multicast group

The examples included with this documentation will demonstrate each of these .NET Framework API calls.

Sending Data to a Multicast Group

For a machine to send data to a multicast group, the application must join the multicast group, define a TTL for the data and then send the information to the group. This is shown programmatically below:

Socket s=new Socket(AddressFamily.InterNetwork, 
				SocketType.Dgram, ProtocolType.Udp);

We first create a socket as if we were creating a normal unicast UDP socket.

IPAddress ip=IPAddress.Parse("");

We now need to join a multicast group. Multicast IP addresses are within the Class D range of - we can join any of these addresses but most we will use for example purposes.

SocketOptionName.AddMembership, new MulticastOption(ip));

We now issue the join command, the socket will be a member of the multicast group once we have joined it.

		SocketOptionName.MulticastTimeToLive, 2);

This sets the time to live for the socket - this is very important in defining scope for the multicast data. Setting a value of 1 will mean the multicast data will not leave the local network, setting it to anything above this will allow the multicast data to pass through several routers, with each router decrementing the TTL by 1. Getting the TTL value right is important for bandwidth considerations.

IPEndPoint ipep=new IPEndPoint(ip, 4567);

This creates the end point that allows us to send multicast data, we connect the socket to this end point. We are now a fully fledged member of the multicast group and can send data to it.

// This creates the letters ABCDEFGHIJ
byte[] b=new byte[10];
for(int x=0;x<b.Length;x++) b[x]=(byte)(x+65);



We have now sent the letters ABCDEFGHIJ to the multicast group on port 4567. All applications listening to this will receive this data.

Receiving Data from a Multicast Group

Receiving data is easier to setup then to send in C#.

Socket s=new Socket(AddressFamily.InterNetwork, SocketType.Dgram,

We setup the socket in the same manner as we would for a unicast UDP socket.

IPEndPoint ipep=new IPEndPoint(IPAddress.Any, 4567);

We create an IP endpoint for the incoming data to any IP address on port 4567 and bind that to the socket.

IPAddress ip=IPAddress.Parse("");

        new MulticastOption(ip,IPAddress.Any));

The socket is added to the multicast group

byte[] b=new byte[1024];
string str = System.Text.Encoding.ASCII.GetString(b,0,b.Length);

We can now receive any data that is sent to this multicast group.

Additional Resources

There are many resources for IP multicasting, a selection of which is listed below:



This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Written By
Web Developer
Australia Australia
I am a software developer.

Comments and Discussions

GeneralRe: Question about the parametres. Pin
skywen1-Jul-04 19:33
skywen1-Jul-04 19:33 
GeneralRe: Question about the parametres. Pin
olvr200025-Feb-05 9:52
olvr200025-Feb-05 9:52 
GeneralQuick question about multicast addresses... Pin
Nas18-Dec-03 0:37
Nas18-Dec-03 0:37 
GeneralRe: Quick question about multicast addresses... Pin
Gary Brewer18-Dec-03 1:31
Gary Brewer18-Dec-03 1:31 
Generalconfuse Pin
Ada dech5-Aug-03 3:04
Ada dech5-Aug-03 3:04 
GeneralRe: confuse Pin
Yogendra1-Jun-04 9:15
Yogendra1-Jun-04 9:15 
GeneralProblem with NT Pin
Mausam Hazarika25-May-03 22:16
sussMausam Hazarika25-May-03 22:16 
GeneralRe: Problem with NT Pin
Harmen16-Sep-04 5:28
Harmen16-Sep-04 5:28 
see XP SP2 thread
GeneralGood Article Pin
Member 1044894523-Jan-03 21:33
Member 1044894523-Jan-03 21:33 
Generalvery good article!!! Pin
13-Jun-02 21:18
suss13-Jun-02 21:18 
GeneralRe: very good article!!! Pin
worldspawn11-Jan-04 13:59
worldspawn11-Jan-04 13:59 
GeneralGood article Pin
Ranjeet Chakraborty2-Jan-02 8:54
Ranjeet Chakraborty2-Jan-02 8:54 
GeneralRe: Good article Pin
Gary Brewer3-Jan-02 7:21
Gary Brewer3-Jan-02 7:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.