Page Summary:
|
Examples of such "traditional" communication:
Unicast communication has been supported efficiently by using routing tables.
You can realise multicast communication by having the sender transmit a copy of the (same) message to each receiver:
Obviously, multicast routing can support multicast communication much more efficiently than "naive multicast communication" (= sending multiple unicast messages).
As a result:
IP multicast must adopt unreliable delivery (UDP) (because how can you achieve reliability if you don't even know who the receivers are ???)
See: click here
s = socket(AF_INET, SOCK_DGRAM, 0); |
struct sockaddr_in in_addr; /* Internet Address socket structure */ in_addr.sin_family = AF_INET; /* Protocol domain */ in_addr.sin_addr.s_addr = INADDR_ANY; /* Use wildcard IP address */ in_addr.sin_port = htons(portNumber); /* Use this UDP port */ bind(s, (struct sockaddr *)&in_addr, sizeof(in_addr) ); |
You need to use a specific port# - you cannot allow the system to assign you with one, because all multicast program must use the same port# to communicate with each other...
struct ip_mreq mreq; /* IP Multicast request */ mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.1"); /* Multicast group address */ mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
The address inet_addr("224.0.0.1") is the multicast group address
setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq) ); |
The variable s is a UDP socket that you have binded to some UDP port on the local host (see UDP programming)
sendto(s, line, strlen(line)+1, 0 /* flags */, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); ^^^^^^^^^ With: dest_addr.sin_addr.s_addr = inet_addr("224.0.0.1"); dest_addr.sin_port = Same port used by receivers... |
char ttl = ...; /* Set a value between 1 and 255 */ /* Multicast with TTL = 1 will only reach your own network */ /* Multicast with TTL = 255 will reach the whole Internet */ setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl); |
mcast-r MCAST_addr Port#
udp4-s2 MCAST_addr Port#