IP Multicasting


Earlier in the chapter, we introduced the concept of multicasting data, a method of delivering data from one source to many receivers thats accomplished by each receiver joining a particular multicast address. All receivers that are interested in the same traffic join the same multicast group . Both the IPv4 and IPv6 address spaces reserve a portion for multicast addresses.

A multicast socket is simply a UDP socket that uses the SocketOptionName.AddMembership and SocketOptionName.DropMembership socket options to join one or more groups. Both IPv4 and IPv6 support multicasting, so the option levels specified are SocketOptionLevel.IP and SocketOptionLevel.IPv6 , respectively.

An IPv4 and IPv6 multicast Socket sample exists under the Chap08\Multicast folder.

Joining a Group

To join a multicast group, the UDP socket must be bound to a local address and port. When joining a group, the multicast address and the local interface on which to join the group are parameters to the SetSocketOption call, which means that its possible to join one or more multicast groups on one or more local interfaces all on the same socket. Therefore, the UDP socket should be bound to the wildcard address.

When joining an IPv4 multicast group, a MulticastOption object must be created that identifies the multicast group IPAddress and the local interface IPAddress to be joined. The following code illustrates IPv4 and multicasting:

C#

 SocketmcastSocket; IPEndPointlocalAddress=newIPEndPoint(IPAddress.Any,5150); MulticastOptionmcastOption; try { mcastOption=newMulticastOption(IPAddress.Parse(234.6.7.8), IPAddress.Parse(10.10.10.1)); mcastSocket=newSocket(localAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); mcastSocket.Bind(localAddress); mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption); } catch(SocketExceptionerr) { Console.WriteLine(Error:{0}",err.Message); } 

Visual Basic .NET

 DimmcastSocketAsSocket DimlocalAddressAsIPEndPoint=NewIPEndPoint(IPAddress.Any,5150) DimmcastOptionAsMulticastOption Try mcastOption=NewMulticastOption(_ IPAddress.Parse("234.6.7.8"),_ IPAddress.Parse("10.10.10.1")_) mcastSocket=NewSocket(_ localAddress.AddressFamily,_ SocketType.Dgram,_ ProtocolType.Udp_) mcastSocket.Bind(localAddress) mcastSocket.SetSocketOption(_ SocketOptionLevel.IP,_ SocketOptionName.AddMembership,_ mcastOption_) CatcherrAsSocketException Console.WriteLine("Error:{0}",err.Message) EndTry 

This code creates an IPv4/UDP socket, binds it to the wildcard address and port 5150, and joins the group 234.6.7.8 on the local interface 10.10.10.1. If no such local interface exists, a SocketException is thrown. Once joined, the socket can receive data via the ReceiveFrom method for multicast data sent to the group 234.5.6.8 and port 5150.

Joining multicast groups on an IPv6 socket is slightly different in that it takes a different structure to join the group on an interface. Instead of a MulticastOption object, an IPv6MulticastOption is required, which is constructed with the IPv6 multicast groups IPAddress and the ScopeId of the local interfaces IPAddress object. Because IP addresses tend to be transient, IPv6 multicasting allows only joining groups given the interfaces index, which never changes (unless the interface is disabled or removed or the machine is rebooted). The following code shows how to create an IPv6 socket and join a multicast group:

C#

 SocketmcastSocket; IPEndPointlocalAddress=newIPEndPoint(IPAddress.IPv6Any,5150); IPv6MulticastOptionmcastOption; try { mcastOption=newIPv6MulticastOption(IPAddress.Parse("ff12::1"), IPAddress.Parse("::%5").ScopeId); mcastSocket=newSocket(localAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); mcastSocket.Bind(localAddress); mcastSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, mcastOption); } catch(SocketExceptionerr) { Console.WriteLine("Error:{0}",err.ToString()); } 

Visual Basic .NET

 DimmcastSocketAsSocket DimlocalAddressAsIPEndPoint=NewIPEndPoint(IPAddress.IPv6Any,5150) DimmcastOptionAsIPv6MulticastOption Try mcastOption=NewIPv6MulticastOption(_ IPAddress.Parse(ff12::1),_ IPAddress.Parse(::%5).ScopeId_) mcastSocket=NewSocket(_ localAddress.AddressFamily,_ SocketType.Dgram,_ ProtocolType.Udp_) mcastSocket.Bind(localAddress) mcastSocket.SetSocketOption(_ SocketOptionLevel.IPv6,_ SocketOptionName.AddMembership,_ mcastOption_) CatcherrAsSocketException Console.WriteLine(Error:{0},err.ToString()) EndTry 

Notice that when the IPv6MulticastOption is created, the string ::%5 is parsed into an IPAddress and the ScopeId is passed to the objects constructor, but we could have simply specified the long value 5 . Attempting to parse the string %5 results in a FormatException because the string is not a valid IPv6 address.

Because multicasting is a one-to-many communication, its possible that multiple sockets on the same computer will want to receive data from the same multicast group and port. To do so, each socket will have to bind to the same port, which is not allowed by default. In this case, each socket calls SetSocketOption with SocketOptionName.ReuseAddress , as described in Table 8-4. This call will allow multiple sockets to bind to the same address and port, and any multicast data received on that address and port will be delivered to each multicast socket bound to it. For more information on the ReuseAddress option and possible security implications, see Chapter 9.

Sending Data to a Multicast Group

Receiving data sent to a multicast group is the same as with non-multicast sockets, but sending data to a multicast group has a catch. Because any computer can join a group on any interface, the network stack has no clue as to which local interface multicast data should be sent from. With non-multicast traffic, the local interface to originate traffic from is determined by the routing table. With multicast traffic, this does not work because multicast recipients might be present on any or all of the local computers networks if the local computer has multiple network interfaces. (A computer with multiple network interfaces is multihomed .) By default, the network stack will send the multicast traffic on the first interface in the routing table unless specified otherwise .

An application can specify the local interface that multicast traffic should be sent from on a socket by using SetSocketOption and the SocketOptionName.MulticastInterface . The option level is IP or IPv6, depending on which protocol is being used. The option value is the IPv4 IPAddress object or the ScopeId field of the IPv6 IPAddress that identifies the local interface that multicast data should be sent from.

Note that to send data to a multicast group, its not required for the socket to join the group its sending to or for it to join any group at all. Also note that the default time-to-live (TTL) value for sent multicast traffic is one, which means that any data sent will not travel beyond the first router on the network. However, the SocketOptionName.MulticastTimeToLive socket option can change the default TTL.

Leaving a Group

Membership to a multicast group can be dropped by following the same steps for joining except specifying SocketOptionName.DropMembership instead of SocketOptionName.AddMembership . This ability to drop membership is useful if the multicast groups that the socket is interested in change over time, such as an application that receives various channels of streaming media and each media channel is sent to a separate multicast group. Otherwise, if the socket is closed with the Close method, all multicast groups joined will be dropped implicitly.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net