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 ???)
(224.0.0.0 represents 11100000.00000000.00000000.00000000)
(239.255.255.255 represents 11101111.11111111.11111111.11111111)
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 publish this specific port# some how (a "meta problem")- the multicast messages sent to group G must use the same port# to communicate with members of multicast group G...
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 an example of a multicast group address
You can use any address between 224.0.0.0 and 239.255.255.255
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)
// Set up multicast group address and port # of multicast group dest_addr.sin_addr.s_addr = inet_addr("224.0.0.1"); dest_addr.sin_port = Same port used by receivers... // Send message in "line" to multicast group sendto(s, line, strlen(line)+1, 0 /* flags */, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); ^^^^^^^^^^ multicast address |
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); |
How to compile:
Usage:
mcast-recv MCAST_addr Port#
mcast-send MCAST_addr Port#
Type message into the mcast-send and see the message appear at all receivers....
(The presentation on multicast programming is to should you the effect of multicast programming; we won't do any programming with multicasting, that's why I kept it very brief)