//Example 5
#include "ace/SOCK_Dgram_Mcast.h"
#include "ace/OS.h"
#define DEFAULT_MULTICAST_ADDR
"224.9.9.2"
#define TIMEOUT
5
class Reciever_Multicast{
public:
Reciever_Multicast(int port):
mcast_addr_(port,DEFAULT_MULTICAST_ADDR),remote_addr_((u_short)0){
// Subscribe to multicast address.
if (mcast_dgram_.subscribe (mcast_addr_) == -1){
ACE_DEBUG((LM_DEBUG,"Error in subscribing to Multicast address
\n"));
exit(-1);
}
}
~Reciever_Multicast(){
if(mcast_dgram_.unsubscribe()==-1)
ACE_DEBUG((LM_ERROR,?Error in unsubscribing from Mcast group\n?));
}
//Receive data from someone who is sending data
on the multicast group
//address to do so it must use the multicast
datagram component
//mcast_dgram_.
int recv_multicast(){
//get ready to recieve data from the sender.
if(mcast_dgram_.recv
(&mcast_info,sizeof (mcast_info),remote_addr_)==-1)
return -1;
ACE_DEBUG ((LM_DEBUG, "(%P|%t) Received multicast from %s:%d.\n",
remote_addr_.get_host_name(), remote_addr_.get_port_number()));
ACE_DEBUG((LM_DEBUG,"Successfully receieved %d\n", mcast_info));
return 0;
}
private:
ACE_INET_Addr mcast_addr_;
ACE_INET_Addr remote_addr_;
ACE_SOCK_Dgram_Mcast mcast_dgram_;
int mcast_info;
};
int main(int argc, char*argv[]){
Reciever_Multicast m(2000);
//Will run forever
while(m.recv_multicast()!=-1) {
ACE_DEBUG((LM_DEBUG,"Multicaster succesful \n"));
}
ACE_DEBUG((LM_ERROR,"Multicaster failed \n"));
exit(-1);
}