5,442,984 members and growing! (18,764 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Client/Server Development     Intermediate

IP Multicasting in C#

By Gary Brewer

A simple client/server implementation of multicasting in C# .NET Framework.
C#.NET 1.0, Win2K, WinXP, Windows, .NET, Visual Studio, Dev

Posted: 1 Jan 2002
Updated: 9 Jan 2002
Views: 152,662
Bookmarked: 86 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
31 votes for this Article.
Popularity: 6.54 Rating: 4.39 out of 5
1 vote, 4.8%
1
1 vote, 4.8%
2
1 vote, 4.8%
3
2 votes, 9.5%
4
16 votes, 76.2%
5

Introduction

This document provides a simple client/server example for setting up a multicast application in C# .NET as a 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" (http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1112.html) 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("224.5.6.7");
    

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

    s.SetSocketOption(SocketOptionLevel.IP,
    SocketOptionName.AddMembership, new MulticastOption(ip));
    

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

    s.SetSocketOption(SocketOptionLevel.IP, 
    		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);
    s.Connect(ipep);
    

    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);
    
    s.Send(b,b.Length,SocketFlags.None);
    
    s.Close();
    

    We have now sent the letters ABCDEFGHIJ to the multicast group 224.5.6.7 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,
     			ProtocolType.Udp);
    

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

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

    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("224.5.6.7");
    
    s.SetSocketOption(SocketOptionLevel.IP,
        SocketOptionName.AddMembership,
            new MulticastOption(ip,IPAddress.Any));
    

    The socket is added to the multicast group 224.5.6.7.

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

    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 are listed below -

    IPMI - http://www.ipmulticast.com/
    University of Southern California Multicast - http://netweb.usc.edu/multicast/
    RFC1112 - http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1112.html


    References

    CSCO01 - http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/ipmulti.htm.

  • License

    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

    About the Author

    Gary Brewer


    I am a software developer.
    Occupation: Web Developer
    Location: Australia Australia

    Other popular Internet / Network articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 25 of 55 (Total in Forum: 55) (Refresh)FirstPrevNext
    Subject  Author Date 
    QuestionMuticastmemberSmithakrishnan23:02 13 Mar '08  
    Questionunable to recieve messages in the clientmemberAnand_1814:28 6 Sep '07  
    Questionhi help me outmembervikram panwar20:05 24 Jul '07  
    GeneralErrormemberLazyInNet15:44 18 Apr '07  
    QuestionTESTING ON LOCALHOSTmemberphydthekid11:05 23 Mar '07  
    GeneralCan it detect a printer IP address over the network?memberJan Palmer17:44 2 Jan '07  
    GeneralRe: Can it detect a printer IP address over the network?memberJan Palmer17:47 2 Jan '07  
    GeneralProblem with VPNmembersalman.ali5:07 28 Oct '06  
    GeneralDatagram packet information...where is it?memberNNN10:53 16 May '06  
    AnswerRe: Datagram packet information...where is it?memberNNN11:59 16 May '06  
    GeneralFirst Connect to the Endpoint before setting socket optionsmemberalphons7:53 27 Oct '05  
    GeneralRe: First Connect to the Endpoint before setting socket optionsmembernunomag1:20 18 Jan '06  
    QuestionRun-time Problems with Sender on VS.NET 2003susswickwack12:41 5 Oct '05  
    AnswerRe: Run-time Problems with Sender on VS.NET 2003memberK.A.L3:41 30 Dec '05  
    GeneralRe: Run-time Problems with Sender on VS.NET 2003memberGraemeWoodhouse4:17 5 Mar '07  
    GeneralPlease help: IP multicasting with windows service programsmemberKing Yu14:12 9 Jun '05  
    GeneralRe: Please help: IP multicasting with windows service programsmemberGary Brewer12:22 26 Jan '06  
    GeneralPort numbermemberMichaelCoder13:22 25 Oct '04  
    GeneralRe: Port numbermemberGary Brewer15:32 9 Jan '05  
    GeneralXP SP2 (or the 0x2726 error)memberHarmen6:17 16 Sep '04  
    GeneralRe: XP SP2memberGary Brewer6:27 16 Sep '04  
    GeneralRe: XP SP2memberHarmen6:34 16 Sep '04  
    GeneralRe: XP SP2 (or the 0x2726 error)memberthe-unforgiven22:17 4 Apr '05  
    GeneralErrorsussqwerrewq322412:00 18 Jul '04  
    Generalask for some questions:memberskywen22:37 1 Jul '04  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 9 Jan 2002
    Editor: Nishant Sivakumar
    Copyright 2002 by Gary Brewer
    Everything else Copyright © CodeProject, 1999-2008
    Web12 | Advertise on the Code Project