Receiving multicast message

For a process to recieve multicast messages it needs to
perform the following steps:

1. Craeate a UDP socket msd

msd = socket(PF_INET,SOCK_DGRAM, 0);
2. Bind it to a UDPport, e.g., 1234.
    All processes must bind to the same port in order
    to receive the multicast messages.
 struct   sockaddr_in    groupHost;

 groupHost.sin_family = AF_INET;
 groupHost.sin_port = htons(UDPport);
 groupHost.sin_addr.s_addr  =  htonl(INADDR_ANY);

 bind(msd,  (struct sockaddr *) &groupHost, sizeof(groupHost));

3. Join a multicast group address GroupIPaddress ,
    e.g., 224.111.112.113
joinGroup (msd, GroupIPaddress);
4. Use recv or recvfrom to read the messages, e.g.,
nbytes = recv(msd, recvBuf, BufLen,0);


Sendinging multicast message

For a process to send  multicast messages it needs to
perform the following:

You may use the UDPsocket msd for sending multicast messages or you can use  any other udp socket
(it does not have to join any multicast group): 

   struct sockaddr_in    dest;

    dest.sin_family = AF_INET;
    dest.sin_port =  UDPport;
    dest.sin_addr.s_addr = inet_addr(GroupIPaddress);

    sendto (msd, sendBuf, BufLen,0,  (struct sockaddr *) &dest, sizeof(dest)) ;

Other issues:

Time-to-live:

To control how far the messages can go,
e.g., 2 means at most 2 routers away.
 

u_char   TimeToLive;
TimeToLive = 2;
 
setTTLvalue (s, &TimeToLive);


Loop-back:

To allow the process to get a copy of its own transmission we use:

u_char loop;
loop = 1;

setLoopback (s,  &loop);

Reuse-port:

To allow multiple multicast processes to to run on the same host:

reusePort (s);