conexus logo

ipv6_multicast_server.cpp

This is an example of how to use multicasting with the Conexus::IPv6::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 <glibmm.h>
#include <conexus.h>
#include <unistd.h>
#include <iostream>

using namespace Conexus;

void print_data( const Data d );

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

  Glib::ustring mcgroup= "FFFF::5";
  Glib::ustring iface1, iface2;
  int port = 1500;
  int run_time = 20;

  Glib::OptionContext option_context( "- IPv6 Multicast Server" );
  Glib::OptionGroup option_group( "Options", "" );

  Glib::OptionEntry option_mcgroup;
  option_mcgroup.set_long_name( "group" );
  option_mcgroup.set_description( "Interface address to receive data on [ 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( "Port to receive data on [default=1500]" );
  option_group.add_entry( option_port, port );

  Glib::OptionEntry option_run_time;
  option_run_time.set_long_name( "run" );
  option_run_time.set_description( "Running time in seconds [default=20]" );
  option_group.add_entry( option_run_time, run_time );

  Glib::OptionEntry option_iface1;
  option_iface1.set_long_name( "iface1" );
  option_iface1.set_description( "Multicast interface 1 [default=system default]" );
  option_group.add_entry( option_iface1, iface1 );

  Glib::OptionEntry option_iface2;
  option_iface2.set_long_name( "iface2" );
  option_iface2.set_description( "Multicast interface 2 [default=none]" );
  option_group.add_entry( option_iface2, iface2 );

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  Conexus::init();

  IPv6::UDP::pointer multicast;

  multicast = IPv6::UDP::create( mcgroup, port );

  if ( iface1.size() > 0 )
    multicast->add_multicast_interface( iface1 );

  if ( iface2.size() > 0 )
    multicast->add_multicast_interface( iface2 );

  multicast->signal_data().connect( sigc::ptr_fun( &print_data ) );
  multicast->start();
  for ( int i = 1; i <= run_time; i++ ) {
    if ( i % 5 == 0 )
      std::cout << "Time: " << i << std::endl;
    sleep( 1 );
  }
  multicast->stop();

  return 0;
}

void print_data( const Data d )
{
  std::cout << "Received " << d.size() << " bytes of data [" << (uint64_t)d.data() << "]\n\t" << d.hex_string() << std::endl;
}

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