Unicast is used for end-to-end communication between two hosts, which is used for data communication of a host on all hosts on the local network. Unicast and broadcast are two extremes, either communicating to a single host or communicating to a host on the entire LAN. In practice, it is often necessary to communicate with a specific set of hosts, rather than all hosts on the entire LAN, which is the purpose of multicasting. 11.3.1 Multicast Concept Multicast, also known as "multicast", the network of the same business type of host logically grouped, the data sent and received only in the same group, other hosts did not join this group can not send and receive the corresponding data. When broadcasting on a wide area network, the switches and routers only copy and forward data to the host that needs to obtain the data. A host can request to join or exit a group from a router, and routers and switches in the network selectively replicate and transfer data, transferring data only to hosts within the group. This feature of multicast allows data to be sent to multiple hosts at once, while ensuring that no other communication is affected by other hosts that do not need to be joined to the group. Compared to the traditional one-to-one unicast, multicasting has the following advantages: Q hosts with the same business join the same data stream, share the same channel, save bandwidth and the advantages of the server, with the advantages of broadcasting without the bandwidth required for broadcast. The total bandwidth of the Q server is not limited by client bandwidth. Because the multicast protocol is required by the receiver to determine whether to forward the data stream, the server-side bandwidth is constant, regardless of the number of clients. Q As with unicast, multicast is allowed to be transmitted over the WAN, the Internet, and broadcasts are only on the same LAN. The disadvantage of multicast: Q Multicast has no error correction mechanism compared with unicast, but it is difficult to compensate when errors occur, but it can be implemented in the application layer. There are some defects in the network support of Q multicast, which need the support of router and network protocol stack. Multicast applications mainly include online video, online conferencing and so on. Multicast for 11.3.2 Wan Multicast addresses are specific and Class D addresses are used for multicast. Class D IP address is a multicast IP address, that is, the IP address between 224.0.0.0 and 239.255.255.255, and is divided into local connection multicast addresses, reserved multicast addresses and administrative rights multicast address 3 categories: Q Local Multicast address: Between 224.0.0.0~224.0.0.255, this is the address reserved for routing protocols and other purposes, and routers do not forward IP packets belonging to this range. Q Reserved Multicast addresses: Between 224.0.1.0~238.255.255.255, available on a global scale (such as the Internet) or network protocols. Q Admin Rights multicast address: Between 239.0.0.0~239.255.255.255, can be used within the organization, similar to private IP address, not to the Internet, can limit the multicast scope. 11.3.3 Multicast Programming Multicast programming is implemented using the setsockopt () function and the getsockopt () function, and the multicast option is the IP layer, and its option values and meanings are shown in 11.5. Table 11.5 Multicast-related options
Options for getsockopt ()/setsockopt ( ) |
Meaning |
Ip_multicast_ttl |
Set TTL values for multicast group data |
Ip_add_membership |
Join a multicast group on the specified interface |
Ip_drop_membership |
Quit a multicast group |
Ip_multicast_if |
Get default interface or set interface |
Ip_multicast_loop |
Prohibit Multicast data loopback |
1. Option Ip_multicase_ttl Option Ip_multicast_ttl allows you to set a timeout TTL that ranges from any value between 0~255, for example: unsigned char ttl=255; SetSockOpt (s,ipproto_ip,ip_multicast_ttl,&ttl,sizeof (TTL)); 2. Option Ip_multicast_if Option ip_multicast_if is used to set the default default network interface for multicast, which is sent from a given network interface, and the other network interface ignores this data. For example: struct IN_ADDR addr; SetSockOpt (s,ipproto_ip,ip_multicast_if,&addr,sizeof (addr)); The parameter addr is the IP address of the desired multicast output interface, and the Inaddr_any address is used to echo to the default interface. By default, when the native sends multicast data to a network interface, at the IP layer, the data is echoed to the local loopback interface, and the option Ip_multicast_loop is used to control whether the data is loopback to the local loopback interface. For example: unsigned char loop; |