Some applications require that some distributed processes need to work together in a group, and the processes in the group usually send messages to all other members. That is, there is a way to send messages to some clearly defined groups. Although these groups have a large number of members, they are very small compared with the size of the whole network. Sending messages to such groups is called multicast, or simply multicast.
I. Introduction of IP Multicast Technology
1.IP multicast address and multicast group
IP multicast communication must depend on IP multicast address. In IPv4, it is a Class D IP address ranging from 224.0.0.0 to 239.255.255, which is divided into three categories: local link multicast address, reserved multicast address and management permission multicast address. Among them, the local link multicast address range is 224.0.0-224.0.0.255, which is reserved for routing protocols, and the router does not forward IP packets belonging to this range; The reserved multicast address is 224.0.1.0 ~ 238.255.255.255, which can be used worldwide (such as Internet) or network protocol. The multicast address with administrative authority is 239.0.0.0~239.255.255.255, which can be used inside the organization, similar to private IP address, but cannot be used in the Internet, and the multicast scope can be limited.
All hosts that use the same IP multicast address to receive multicast packets form a host group, also known as a multicast group. Members of a multicast group can change at any time. Hosts can join or leave the multicast group at any time, and the number and geographical location of multicast group members are not limited. A host can also belong to several multicast groups. In addition, hosts that do not belong to the multicast group can also send packets to the multicast group.
2.2 hardware support. IP multicast technology
In order to realize IP multicast communication, routers, hubs, switches and hosts between multicast sources and receivers need to support IP multicast. At present, IP multicast technology has been widely supported by hardware and software manufacturers.
(1) host
Platforms supporting IP multicast communication include Windows CE 2. 1, Windows 95, Windows 98, Windows NT 4 and Windows 2000. Hosts running these operating systems can perform IP multicast communication. In addition, almost all newly produced network cards support IP multicast.
(2) hubs and switches
At present, most hubs and switches simply send and receive multicast data as broadcast, but some high-end switches provide support for IP multicast. For example, 802. 1p or IGMP multicast filtering function can be enabled on 3COM SuperStack 3 Swith 3300 switch, and multicast packets can only be forwarded to the port where IGMP packets are detected.
(3) Router
Multicast communication requires that all routers between multicast source nodes and destination nodes must provide support for Internet Group Management Protocol (IGMP) and multicast routing protocols (such as PIM and DVMRP).
When a host wants to join a multicast group, it will send an IGMP message of "Host Member Report" to inform the multicast router. When the multicast router receives the data sent to the multicast group, it forwards it to all multicast hosts. The multicast router will also periodically send out an IGMP message of "Host Member Query" to query multicast hosts from the subnet. If a multicast group is found to have no members, it will stop forwarding the data of the multicast group. In addition, when a host supporting IGMP v2 (such as a Windows 98/2000 computer) leaves the multicast group, it will send an IGMP message of "Leave the Group" to the router to inform the router to stop forwarding the multicast group data. But only when all the hosts in the subnet quit the multicast group will the router stop forwarding the multicast group data to the subnet.
Using multicast routing protocol, routers can establish multicast routing tables from multicast source nodes to all destination nodes, thus realizing the forwarding of multicast packets between subnets. For example, PIM (Protocol Independent Multicast) is a multicast routing protocol, which has two types: sparse mode and dense mode. Taking Cisco 262 1 router as an example, the basic settings for enabling IP multicast forwarding function are as follows:
C262 1 (config) # IP Multicast-Routing starts IP Multicast, making the router a multicast router.
C262 1 (Configuration) # int f0/0 Configure Fast Ethernet port 0.
C262 1 (config-if) # IP PIM dense mode (or sparse mode) starts PIM and activates IGMP protocol at the same time.
C262 1 (configuration -if) # intf0/ 1 Configure fast Ethernet port 1.
C262 1 (configuration -if) # IP PIM dense mode (or sparse mode)
Second, the programming method of IP multicast application
In practical applications, programmers usually need to write their own underlying network applications to realize the underlying communication on the Internet, such as realizing the function of IP multicast communication. Writing the underlying network applications usually depends on the network data communication programming interface, but the network programming interfaces provided in different operating systems are different. For example, the network programming interface in Microsoft Windows environment is Windows Socket.
Winsock provides programming interfaces under various communication protocols, including TCP/IP and IPX. Different versions of Windows support different versions of Winsock, among which early versions such as Windows 95 only support Winsock1.1(16-bit) programming (Winsock2.0 can be supported by installing related software packages), while Windows98, Windows NT4.0 and Windows 2000 directly support Winsock2.0(32-bit). Winsock2.0 is an extension of Winsock 1. 1, which is not only compatible with Winsock 1. 1 API, but also defines a set of protocols-independent APIs that can support IP multicast.
The general steps to realize IP multicast using Winsock 2.0 are as follows:
1. Initialize Winsock resource.
Before using Winsock, you must call WSAStartup () function to initialize Windows Sockets DLL. It allows an application or DLL to specify the required version of the Windows Sockets API.
Create a socket
Call WSASocket () function to create a socket using UDP protocol, which is the initialization socket for joining multicast group, and then send and receive data on this socket. For IP multicast communication, the parameter dwFlags can be set as the sum of bits of WSA_FLAG_MULTIPOINT_C_LEAF, WSA_FLAG_MULTIPOINT_D_LEAF and WSA_FLAG_OVERLAPPED, which means that IP multicast communication is rootless in the control layer and data layer, and only has leaf nodes, so you can join a multicast group at will and send from one leaf node. The created sockets have overlapping properties.
3. Set socket options
Call the setsockopt () function to set the SO_REUSEADDR option for the socket to allow the socket to bind to an already used address.
Bundled socket
Call the bind () function to bind the socket, thus associating the created socket with the local address and port. For multicast communication, sending and receiving data usually use the same port.
5. Set the mode of multicast socket
The command code SIO_MULTICAST_LOOP of WSAIoctl () function is used to allow or prohibit whether the communication traffic sent out during multicast communication can also be received on the same socket (i.e. multicast return). It is worth noting that in Windows 95/98/NT 4, multicast return is allowed by default, but it cannot be prohibited, otherwise an error will occur. Allow/Disable Multicast Return can only be set in Windows 2000 or later.
The command code SIO_MULTICAST_SCOPE of WSAIoctl () function is used to set the range of multicast propagation, that is, the lifetime TTL. Whenever a multicast router forwards a multicast packet, the TTL value in the packet will decrease by 1. If the TTL of a packet drops to 0, the router will discard the packet. What is the value of TTL, and how many multicast routers can multicast data pass at most? For example, if the TTL value is 0, multicast can only spread among multiple sockets of the local host, but not to the "network cable"; When the TTL value is 1 (the default value), multicast data will be "ruthlessly" discarded by the first router, and it is not allowed to spread outside the local network, that is, only multicast group members in the same network can receive multicast data.
Multicast in c#
In c#, a general proxy instance (that is, a proxy can only call one method) is the object of the Delegate class by default, so the delegate keyword is usually used to define the proxy, the new operator is used to create the proxy instance, and then the proxy instance is managed by the methods and properties of the delegate class.
MulticastDelegate class is used to support multiple proxies, and its call list can have proxies for multiple methods.
Multi-agent refers to the combination of a group of agents into a collection, which is managed by an object of the MuticastDelegate class, and uses this agent collection to execute multiple methods. This function is called multicast.