conexus logo

ipv4_multicast_client.cpp

This is an example of how to use multicasting with the Conexus::IPv4::UDP class.

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This file is part of the conexus library.                             *
 *                                                                         *
 *   The conexus library is free software; you can redistribute it and/or  *
 *   modify it under the terms of the GNU General Public License version 3 *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   The conexus library is distributed in the hope that it will be        *
 *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#include <conexus.h>
#include <glibmm.h>

using namespace Conexus;

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

  Glib::ustring data = "0123456789";
  Glib::ustring mcgroup = "236.1.1.5";
  Glib::ustring local_address;
  int port = 1500;
  bool suppress_loopback = false;
  int hops = 64;

  Glib::OptionContext option_context( "- IPv4 Multicast Client" );
  Glib::OptionGroup option_group( "Options", "" );

  Glib::OptionEntry option_data;
  option_data.set_long_name( "data" );
  option_data.set_description( "Data to send [default=\"0123456789\"]" );
  option_group.add_entry( option_data, data );

  Glib::OptionEntry option_mcgroup;
  option_mcgroup.set_long_name( "group" );
  option_mcgroup.set_description( "Multicast group to send data to [default=236.1.1.5]" );
  option_group.add_entry( option_mcgroup, mcgroup );

  Glib::OptionEntry option_port;
  option_port.set_long_name( "port" );
  option_port.set_description( "Multicast group port to send data to [default=1500]" );
  option_group.add_entry( option_port, port );

  Glib::OptionEntry option_local_address;
  option_local_address.set_long_name( "local-address" );
  option_local_address.set_description( "Local interface address [default=system default]" );
  option_group.add_entry( option_local_address, local_address );

  Glib::OptionEntry option_suppress_loopback;
  option_suppress_loopback.set_long_name( "suppress-loopback" );
  option_suppress_loopback.set_description( "Suppress multicast loopback [default=false]" );
  option_group.add_entry( option_suppress_loopback, suppress_loopback );

  Glib::OptionEntry option_hops;
  option_hops.set_long_name( "hops" );
  option_hops.set_description( "Multicast hops [default=64]" );
  option_group.add_entry( option_hops, hops );

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  Conexus::init();

  IPv4::UDP::pointer multicast;

   if ( local_address.size() > 0 )
     multicast = IPv4::UDP::create(local_address);
   else
    multicast = IPv4::UDP::create();

  IPv4::Address addr( mcgroup, port );

  multicast->set_multicast_hop_limit(hops);
  multicast->enable_multicast_loopback(!suppress_loopback);

  multicast->writeto( addr, Conexus::Data(data.c_str(), data.size()) );

  multicast->connect(addr);
  multicast->write(data.c_str(), data.size());

  return 0;
}

Generated on Wed Jul 8 15:50:07 2009 for conexus by doxygen 1.5.8