Multicasting



Ø     Receiving Multicast Messages

Ø     Sending Multicast Message

Ø     Multicast Control

Ø     Multicast Examples

 

 

 


Ø   Receiving Multicast Message

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

ü Craeate a UDP socket sd

sd = socket (PF_INET,SOCK_DGRAM, 0);

 

ü Bind it to a UDPport, e.g., 10203.



 struct   sockaddr_in    groupHost;

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

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

All processes must bind to the same port   in order to receive the multicast messages.

ü Join a multicast group address GroupIPaddress ,  e.g., 224.111.112.113

 

joinGroup (sd, GroupIPaddress);

 

ü  Use recv or recvfrom to read the messages, e.g.,

 

nbytes = recv (sd, recvBuf, BufLen,0);

 

 

 

 

 


Ø   Sendinging Multicast Message

 

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

ü You may use the same UDPsocket  sd  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 (sd, sendBuf, BufLen,0,  (struct sockaddr *) &dest, sizeof(dest)) ;

 

 

 

 


Ø   Multicast Control:

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 (sd, &TimeToLive);


Loop-back:

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

u_char loop;
loop = 1;

setLoopback (sd,  &loop);

Reuse-port:

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

reusePort (sd);

 

 

  

 

 


  

Ø   Multicast Examples

 

ü     Group Chat

 Usage: mchat <group>

        E.g: % mchat 22

 

 

 

ü     Textbook Example:    (chapter 21):

 

        Usage: sendrecv  <maddr>   <port>

        E.g: % sendrecv  224.2.2.2    1234