Multicasting Overview



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., 1234.



 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 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)) ;

Other issues:

Time-to-live:

To control how far the messages can go,
e.g., 2 mean 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 in C

 

·    Textbook Example:  (chapter 21):

 

 

 main.c  &  send.c  recv.c

 

        Usage: sendrecv  <maddr>   <port>

        E.g: % sendrecv  224.2.2.2    1234 
  

 

·    Additional  Example:

·      group chat

 Usage: mchat <group>

        E.g: % mchat 22

 

·      IPV6 version of group chat

    Usage: mchat <group>

   E.g: % mchat   22

 

 

 

Multicasting Examples in Java