IP Multicasting

Using multicast provides a form of "limited" broadcast support for Pocket PC devices. When you send a traditional broadcast message (a UDP datagram that has the SO_BROADCAST option turned on), it is sent to every machine on a particular subnet. This is typically frowned upon, as sending broadcast messages can cause an extreme amount of network congestion, involve a lot of overhead, and use an enormous amount of bandwidth. The best way to think of multicast is as a "point-to-multipoint" broadcast message, in which a multicast message is sent only to devices that are interested in receiving the message.

This is accomplished by having the target devices join what is known as a multicast group. For example, if ten devices need to be able to communicate with one another, each one joins the same group address. Any messages sent to that group address are replicated to each member, including the device that sent the original message. A multicast address is a class D IP address in the range of 224.0.0.0 through 239.255.255.255 (except for 224.0.0.0 and 224.0.0.1, which are reserved by the IANA). You should be aware that while any device can send multicast datagrams, the only way to receive them is to join a multicast group. Finally, only routers that are capable of using the Internet Group Management Protocol (IGMP) will allow multicast data to traverse them.

IP Multicast Groups

To join or leave an IP Multicast group, you need to use the setsockopt() function to change the membership state of a socket. Both options use a special multicast data structure that is defined as follows:

 struct ip_mreq {    struct in_addr imr_multiaddr;    struct in_addr imr_interface; }; 

The imr_multiaddr field is the address of the multicast host group to join, and the imr_interface field specifies the IP address of the local network interface on which you want to receive multicast datagrams. You can typically set this to INADDR_ANY, which will specify the default interface.

In order to join a multicast group, you should create a UDP datagram socket using the socket function, and then call the setsockopt() function with the option IP_ADD_MEMBERSHIP. Remember that you do not have to join a multicast group to send multicast datagrams to the group, and your socket can be a member of up to 20 different multicast groups. In addition, if you want multiple sockets in your application to join the same group on the same port, you need to call the setsockopt() function with the SO_REUSEADDR option enabled.

If you want to leave a group, you can call setsockopt() and use the IP_DROP_MEMBERSHIP option.

Sending and Receiving Multicast Data

To send and receive data from a multicast group, you can use the standard UDP sendto() and recvfrom() messages. Make sure your datagram messages are sent to the multicast group address, rather than an individual host's address.

If you want to send multicast packets beyond the local network, you need to change the IP time-to-live (or TTL) value for the socket. This is typically set to 1, but you can change it using setsockopt() with the IP_MULTICAST_TTL option. Note that to send packets beyond the local network, all the routers between you and your target device must be multicast-capable.



Pocket PC Network Programming
Pocket PC Network Programming
ISBN: 0321133528
EAN: 2147483647
Year: 2005
Pages: 90

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