home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / sdk / doc / misc / multcast.txt < prev    next >
Encoding:
Text File  |  1995-07-11  |  5.7 KB  |  138 lines

  1.  
  2. IP MULTICASTING USING WINDOWS NT
  3.  
  4.  
  5. It is possible for a host to become a member of a multicast group and 
  6. receive and send multicast packets with Windows NT using the sockets 
  7. interface. This article has more information on how to do this.
  8.  
  9.  
  10.  
  11. Sending IP Multicast Datagrams
  12.  
  13. IP multicasting is currently supported only on AF_INET sockets of type 
  14. SOCK_DGRAM.
  15.  
  16. To send a multicast datagram, specify an IP multicast address in the 
  17. range 224.0.0.0 to 239.255.255.255 as the destination address in a 
  18. sendto() call.
  19.  
  20. By default, IP multicast datagrams are sent with a time-to-live (TTL) of 
  21. 1, which prevents them from being forwarded beyond a single subnetwork. 
  22. The following code demonstrates how to change this :
  23.  
  24.   int ttl = 7 ; // arbitrary ttl value
  25.   setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl))
  26.  
  27. Multicast datagrams with a TTL of 0 will not be transmitted on any 
  28. subnet. Multicast datagrams with TTL greater than one may be delivered 
  29. to more than one subnet if there are one or more multicast routers 
  30. attached to the first-hop subnet.  
  31.  
  32.  
  33. A multicast router will not forward any multicast datagram with a 
  34. destination address between 224.0.0.0 and 224.0.0.255, inclusive, 
  35. regardless of its TTL.  This range of addresses is reserved for the use 
  36. of routing protocols and other low-level topology discovery or 
  37. maintenance protocols, such as gateway discovery and group membership 
  38. reporting.  The current specification for IP multicasting requires this 
  39. behavior only for addresses 224.0.0.0 and 224.0.0.1.
  40.  
  41.  
  42.  
  43. Each multicast transmission is sent from a single network interface, 
  44. even if the host has more than one multicast-capable interface. A socket 
  45. option is available to override the default for subsequent transmissions 
  46. from a given socket:
  47.  
  48.   unsigned long addr = inet_addr("157.57.8.1");
  49.   setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&addr, sizeof(addr))
  50.  
  51. where "addr" is the local IP address of the desired outgoing interface. 
  52. An address of INADDR_ANY may be used to revert to the default interface. 
  53. Note that this address might be different from the one the socket is 
  54. bound to.
  55.  
  56.  
  57. If a multicast datagram is sent to a group to which the sending host 
  58. itself belongs (on the outgoing interface), a copy of the datagram is, 
  59. by default, looped back by the IP layer for local delivery.  Under some 
  60. versions of UNIX, there is an option available to disable this behavior 
  61. (IP_MULTICAST_LOOP). This option is not supported on Windows NT. A call 
  62. to this change this option will fail with the error WSAENOPROTOOPT (Bad 
  63. protocol option). 
  64.  
  65. A multicast datagram sent with an initial TTL greater than 1 may be 
  66. delivered to the sending host on a different interface from that on 
  67. which it was sent, if the host belongs to the destination group on that 
  68. other interface.  The loopback control option has no effect on such 
  69. delivery.
  70.  
  71.  
  72. Receiving IP Multicast Datagrams
  73.  
  74. Before a host can receive IP multicast datagrams, it must become a 
  75. member of one or more IP multicast groups.  A process can ask the host 
  76. to join a multicast group by using the following socket option:
  77.  
  78.   struct ip_mreq mreq;
  79.  
  80. where "mreq" is the following structure:
  81.  
  82.   struct ip_mreq {
  83.       struct in_addr imr_multiaddr;    /* multicast group to join */
  84.       struct in_addr imr_interface;    /* interface to join on    */
  85.   }
  86.  
  87. For example :
  88.  
  89.   #define RECV_IP_ADDR    "225.6.7.8"  // arbitrary multicast address
  90.  
  91.   mreq.imr_multiaddr.s_addr = inet_addr(RECV_IP_ADDR);
  92.   mreq.imr_interface.s_addr = INADDR_ANY;
  93.   err = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq))
  94.  
  95. Note that it is necessary to bind to an address before calling 
  96. setsockopt( ). Every membership is associated with a single interface, 
  97. and it is possible to join the same group on more than one interface.  
  98. "Imr_interface" should be INADDR_ANY to choose the default multicast 
  99. interface, or one of the host's local addresses to choose a particular 
  100. (multicast-capable) interface. The maximum number of memberships is 
  101. limited only by memory and what the network card supports.
  102.  
  103. To drop a membership, use:
  104.  
  105.   struct ip_mreq mreq;
  106.   setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq))
  107.  
  108. where "mreq" contains the same values as used to add the membership.  
  109. The memberships associated with a socket are also dropped when the 
  110. socket is closed or the process holding the socket is killed.  However, 
  111. more than one socket may claim a membership in a particular group, and 
  112. the host will remain a member of that group until the last claim is 
  113. dropped.
  114.  
  115. The memberships associated with a socket do not necessarily determine 
  116. which datagrams are received on that socket.  Incoming multicast packets 
  117. are accepted by the kernel IP layer if any socket has claimed a 
  118. membership in the destination group of the datagram; however, delivery 
  119. of a multicast datagram to a particular socket is based on the 
  120. destination port (or protocol type, for raw sockets), just as with 
  121. unicast datagrams.  To receive multicast datagrams sent to a particular 
  122. port, it is necessary to bind to that local port, leaving the local 
  123. address unspecified (i.e., INADDR_ANY).
  124.  
  125. More than one process may bind to the same SOCK_DGRAM UDP port if the 
  126. bind( ) is preceded by:
  127.  
  128.   int one = 1;
  129.   setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one))
  130.  
  131. In this case, every incoming multicast or broadcast UDP datagram 
  132. destined to the shared port is delivered to all sockets bound to the 
  133. port.  
  134.  
  135. The definitions required for the new, multicast-related socket options 
  136. are found in winsock.h.  All IP addresses are passed in network byte-
  137. order.
  138.