Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Tuesday, November 15, 2005

ACE multicast sender and receiver

//Server

#include "stdafx.h"

#include "ace/OS.h"

#include "ace/SOCK_Dgram.h"

#include "ace/INET_Addr.h"

#include "ace/log_msg.h"

#include "ace/SOCK_Dgram_Mcast.h"

#ifdef _DEBUG

#pragma comment(lib,"D:\\ACE\\ACE_wrappers\\lib\\aced.lib")

#else

#pragma comment(lib,"D:\ACE\ACE_wrappers\lib\ace.lib")

#endif

#define DEFAULT_MULTICAST_ADDR "224.9.9.2"

#define TIMEOUT 5

//The following class is used to receive multicast messages from

//any sender.

class Receiver_Multicast

{

public:

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

          }

      }

      ~Receiver_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 receive data from the sender.

          if(mcast_dgram_.recv (&mcast_info,sizeof (mcast_info),remote_addr_)==-1)

              return -1;

          else

          {

              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 received %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;

};



class Sender_Multicast

{

public:

    Sender_Multicast(int port):

      local_addr_((u_short)0),dgram_(local_addr_),

          multicast_addr_(port,DEFAULT_MULTICAST_ADDR)

      {

      }

      //Method which uses a simple datagram component to send data to the //multicast group.

      int send_to_multicast_group()

      {

          //Convert the information we wish to send into network byte order

          mcast_info= htons (1000);

          // Send multicast

          if(dgram_.send (&mcast_info, sizeof (mcast_info), multicast_addr_)==-1)

              return -1;

          ACE_DEBUG ((LM_DEBUG,

              "%s; Sent multicast to group. Number sent is %d.\n",

              __FILE__,

              mcast_info));

          return 0;

      }

private:

    ACE_INET_Addr multicast_addr_;

    ACE_INET_Addr local_addr_;

    ACE_SOCK_Dgram dgram_;

    int mcast_info;

};

int send_multicast_main(int argc, char*argv[])

{

    Sender_Multicast m(2000);

    if(m.send_to_multicast_group()==-1)

    {

        ACE_DEBUG((LM_ERROR,"Send to Multicast group failed \n"));

        exit(-1);

    }

    else

        ACE_DEBUG((LM_DEBUG,"Send to Multicast group successful \n"));

    return 0;

}

int Receiver_Multicast_main(int argc, char*argv[])

{

    Receiver_Multicast m(2000);

    while(m.recv_multicast()!=-1)

    {

        ACE_DEBUG((LM_DEBUG,"Multicaster successful \n"));

    }

    ACE_DEBUG((LM_ERROR,"Multicaster failed \n"));

    exit(-1);

    return 0;

}

int main(int argc, char* argv[])

{

    return send_multicast_main(argc,argv);

    return Receiver_Multicast_main(argc,argv);

}

0 Comments:

Post a Comment

<< Home