Multicast refers to a group of hosts, where one host sends data and other hosts in the group receive the data. First, describe the principles of Multicast:
We also regard the vro192.168.192.168.0.1 port as a host, so that four hosts are connected to the vswitch. According to the principle of Ethernet, the switch does not know the existence of the IP address, but only the MAC address. The switch forwards data packets to a port based on the destination MAC address, so that only hosts with this MAC address will receive data packets. Therefore, the MAC address controls the switch forwarding data.
In WinSock, the multicast function is set to setsockopt (sock, ipproto_ip, ip_add_membership, (char *) & MCAST, sizeof (MCAST )); this function adds a sock socket to a group. The IP address of this group is represented by MCAST, and then multicast data is sent through the sock socket. The sendto address is also MCAST. what does it mean to add data to a group? In fact, this function does two things: one is to tell the system network driver that when sock sends multicast data, the MAC address added to the link layer frame is not the MAC address of the network adapter, but a multicast MAC address. When the switch receives the data, it broadcasts the data group based on the multicast MAC address. Another thing is to send an IGMP message to notify the router that sock has been added to a group. If you encounter data in this group in the future, forward the data to other networks based on TTL, this refers to forwarding data to other networks. Therefore, if multicast data sent by 192.168.0.2 is not sent to port 192.168.0.1. Therefore, the following conclusion: Even if 192.168.1.7 is not in the same network segment as the host connected to other wide ports of the vswitch, As long as 192.168.1.7 is added to the same group of 192.168.0.2, the multicast data can be received. The program below proves this point of view. The program is divided into two types: one is dedicated to sending data, and the other is dedicated to receiving data, which runs on. 0.2 and. 1.7 respectively.
Sender Program (all calls fail to be judged)
# Include <winsock2.h> <br/> # include <ws2tcpip. h> <br/> # include <stdio. h> </P> <p> int main (INT argc, char * argv []) <br/> {<br/> wsadata wsad; <br/> wsastartup (makeword (1,1), & wsad); </P> <p> socket socksend = socket (af_inet, sock_dgram, ipproto_udp ); </P> <p> sockaddr_in local; <br/> Local. sin_family = af_inet; <br/> Local. sin_addr.s_un.s_addr = inaddr_any; <br/> Local. sin_port = 4004; <br/> memset (local. sin_zero, 0, 8); <br/> BIND (socksend, (sockaddr *) & Local, sizeof (local); </P> <p> ip_mreq MCAST; <br/> MCAST. imr_interface.s_un.s_addr = inaddr_any; <br/> MCAST. counter = inet_addr ("234.0.23.23"); <br/> setsockopt (socksend, ipproto_ip, ip_add_membership, (char *) & MCAST, sizeof (MCAST )); </P> <p> int optval = 8; <br/> setsockopt (socksend, ipproto_ip, ip_multicast_ttl, (char *) & optval, sizeof (INT )); </P> <p> int loop = 0; <br/> setsockopt (socksend, ipproto_ip, ip_multicast_loop, (char *) & Loop, sizeof (INT )); </P> <p> sockaddr_in remote; <br/> remote. sin_addr.s_un.s_addr = inet_addr ("234.0.23.23"); <br/> remote. sin_family = af_inet; <br/> remote. sin_port = 4004; <br/> memset (remote. sin_zero, 100); </P> <p> char sendbuf [100]; <br/> memset (sendbuf, 0, sizeof (char ); <br/> for (INT I = 0; I <3600; I ++) <br/>{< br/> sprintf (sendbuf, "Send: this is test % d/N ", I); <br/> printf (sendbuf); <br/> int RT = sendto (socksend, sendbuf, strlen (sendbuf ), 0, (sockaddr *) & remote, sizeof (remote); </P> <p> sleep (1000 ); <br/>}</P> <p> setsockopt (socksend, ipproto_ip, ip_drop_membership, (char *) & MCAST, sizeof (MCAST )); </P> <p> closesocket (socksend); </P> <p> return 0; <br/>}
Receiver Program (all calls fail to be judged)
# Include <winsock2.h> <br/> # include <ws2tcpip. h> <br/> # include <stdio. h> </P> <p> int main (INT argc, char * argv []) <br/> {<br/> wsadata wsad; <br/> wsastartup (makeword (1,1), & wsad); </P> <p> socket socksend = socket (af_inet, sock_dgram, ipproto_udp ); </P> <p> sockaddr_in local; <br/> Local. sin_family = af_inet; <br/> Local. sin_addr.s_un.s_addr = inaddr_any; <br/> Local. sin_port = 4004; <br/> memset (local. sin_zero, 0, 8); <br/> BIND (socksend, (sockaddr *) & Local, sizeof (local); </P> <p> ip_mreq MCAST; <br/> MCAST. imr_interface.s_un.s_addr = inaddr_any; <br/> MCAST. counter = inet_addr ("234.0.23.23"); <br/> setsockopt (socksend, ipproto_ip, ip_add_membership, (char *) & MCAST, sizeof (MCAST )); </P> <p> int optval = 8; <br/> setsockopt (socksend, ipproto_ip, ip_multicast_ttl, (char *) & optval, sizeof (INT )); </P> <p> int loop = 0; <br/> setsockopt (socksend, ipproto_ip, ip_multicast_loop, (char *) & Loop, sizeof (INT )); </P> <p> char recvbuf [100]; <br/> memset (recvbuf, 0, sizeof (char) * 100 ); <br/> for (INT I = 0; I <10000; I ++) <br/>{< br/> sockaddr_in from; <br/> int fromlen = sizeof (from); <br/> int RT = recvfrom (socksend, recvbuf, 100,0, (sockaddr *) & from, & fromlen ); <br/> If (RT> 0) <br/> {<br/> recvbuf [RT] = 0; <br/> printf ("Recv: -- % s --, from: % s, Port: % d/N ", recvbuf, inet_ntoa (from. sin_addr), from. sin_port); <br/>}< br/> setsockopt (socksend, ipproto_ip, ip_drop_membership, (char *) & MCAST, sizeof (MCAST )); </P> <p> closesocket (socksend); </P> <p> return 0; <br/>}