IPv4 UDP Endpoint

IPv4 UDP client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <iostream>
#include <glibmm.h>

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

  Glib::ustring data;
  Glib::ustring destination;
  int port;

  Glib::OptionContext option_context( "- IPv4 UDP 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_destination;
  option_destination.set_long_name( "dest" );
  option_destination.set_description( "Destination to send data to [default=127.0.0.1]" );
  option_group.add_entry( option_destination, destination );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( data.size() == 0 ) data = "0123456789";
  if ( destination.size() == 0 ) destination = "127.0.0.1";
  if ( port == 0 ) port = 1500;

  Conexus::init();

  // declare the local UDP connection point
  Conexus::IPv4::UDP::pointer udp = Conexus::IPv4::UDP::create();

  // declare the address object that will be used to specify the destination
  Conexus::IPv4::Address addr(destination, port);

  // Example of the sendto method which requires a destination address,
  // but doesn't require a connected port
  Conexus::Data d = Conexus::Data( data.c_str(), data.size() );
  udp->writeto(addr, d);
  std::cout << "1 transmitted" << std::endl;

  // Example of using the connect and send method. The send method doesn't
  // require an address, but instead requires a connected UDP object and
  // just sends to the connected destination.
  udp->set_remote_address(addr);
  udp->set_write_without_connect(true);
  udp->write(data.c_str(), data.size());
  std::cout << "2 transmitted" << std::endl;
  Conexus::Data d2 = Conexus::Data(data.c_str(), data.size());
  udp << d2;
  std::cout << "3 transmitted" << std::endl;

  return 0;
}

IPv4 UDP server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <glibmm.h>

// prototypes of the three print callback functions
void print_data1(const Conexus::Data d);
void print_data2(const Conexus::Data d);
void print_data3(const Conexus::Data d);

int main(int argc, char* argv[]) {
  Glib::ustring interface;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 UDP Server" );
  Glib::OptionGroup option_group( "Options", "" );

  Glib::OptionEntry option_interface;
  option_interface.set_long_name( "if" );
  option_interface.set_description( "Interface address to receive data on [ default = any ]" );
  option_group.add_entry( option_interface, interface );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  // Initialize the Conexus library
  // This is needed because we will use threaded servers and this
  // will do all the behind the scenes work to initialize pthreads
  Conexus::init();

  // Declare the udp object
  Conexus::IPv4::UDP::pointer udp;

  if ( interface.size() == 0 )
    udp = Conexus::IPv4::UDP::create(port);
  else
    udp = Conexus::IPv4::UDP::create(interface, port);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  udp->signal_data().connect(sigc::ptr_fun(&print_data1));
  udp->signal_data().connect(sigc::ptr_fun(&print_data2));
  udp->signal_data().connect(sigc::ptr_fun(&print_data3));

  // Start the server. The server will spawn a separate thread to service
  // received data, so this call will immediately return after the thread
  // is spawned.
  udp->start();

  std::cout << "Main thread pid: " << pthread_self() << std::endl;
  // Set up a loop that will run and print the time every 5
  // seconds. Since the server is threaded, the sleep(1) call will not effect
  // the servicing thread.
  std::cout << "Starting..." << std::endl;
  for (int i=0; i <= run_time; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }
  std::cout << "Time: " << run_time << std::endl;

  // Stop the server and prepare for shutdown
  udp->stop();

  return 0;
}

// These are the three callback functions. Each simply prints out the data received.

void print_data1( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<1> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data2( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<2> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data3( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<3> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

IPv4 UDP multicast client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>
#include <glibmm.h>

using namespace Conexus;

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

  Glib::ustring data;
  Glib::ustring mcgroup;
  Glib::ustring local_address;
  int port;
  bool suppress_loopback;
  int hops;

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

  if ( data.size() == 0 ) data = "0123456789";
  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( hops == 0 ) hops = 64;

  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_hops(hops);
  multicast->set_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;
}

IPv4 UDP multicast server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <unistd.h>
#include <iostream>

#include <glibmm.h>

#include <conexus.h>

using namespace Conexus;

void print_data(const Data d);

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

  Glib::ustring mcgroup;
  Glib::ustring iface1, iface2;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 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 );

  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  Conexus::init();

  IPv4::UDP::pointer multicast;

  multicast = IPv4::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;
}

IPv4 UDP client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <iostream>
#include <glibmm.h>

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

  Glib::ustring data;
  Glib::ustring destination;
  int port;

  Glib::OptionContext option_context( "- IPv4 UDP 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_destination;
  option_destination.set_long_name( "dest" );
  option_destination.set_description( "Destination to send data to [default=127.0.0.1]" );
  option_group.add_entry( option_destination, destination );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( data.size() == 0 ) data = "0123456789";
  if ( destination.size() == 0 ) destination = "127.0.0.1";
  if ( port == 0 ) port = 1500;

  Conexus::init();

  // declare the local UDP connection point
  Conexus::IPv4::UDP::pointer udp = Conexus::IPv4::UDP::create();

  // declare the address object that will be used to specify the destination
  Conexus::IPv4::Address addr(destination, port);

  // Example of the sendto method which requires a destination address,
  // but doesn't require a connected port
  Conexus::Data d = Conexus::Data( data.c_str(), data.size() );
  udp->writeto(addr, d);
  std::cout << "1 transmitted" << std::endl;

  // Example of using the connect and send method. The send method doesn't
  // require an address, but instead requires a connected UDP object and
  // just sends to the connected destination.
  udp->set_remote_address(addr);
  udp->set_write_without_connect(true);
  udp->write(data.c_str(), data.size());
  std::cout << "2 transmitted" << std::endl;
  Conexus::Data d2 = Conexus::Data(data.c_str(), data.size());
  udp << d2;
  std::cout << "3 transmitted" << std::endl;

  return 0;
}

IPv4 UDP server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <glibmm.h>

// prototypes of the three print callback functions
void print_data1(const Conexus::Data d);
void print_data2(const Conexus::Data d);
void print_data3(const Conexus::Data d);

int main(int argc, char* argv[]) {
  Glib::ustring interface;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 UDP Server" );
  Glib::OptionGroup option_group( "Options", "" );

  Glib::OptionEntry option_interface;
  option_interface.set_long_name( "if" );
  option_interface.set_description( "Interface address to receive data on [ default = any ]" );
  option_group.add_entry( option_interface, interface );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  // Initialize the Conexus library
  // This is needed because we will use threaded servers and this
  // will do all the behind the scenes work to initialize pthreads
  Conexus::init();

  // Declare the udp object
  Conexus::IPv4::UDP::pointer udp;

  if ( interface.size() == 0 )
    udp = Conexus::IPv4::UDP::create(port);
  else
    udp = Conexus::IPv4::UDP::create(interface, port);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  udp->signal_data().connect(sigc::ptr_fun(&print_data1));
  udp->signal_data().connect(sigc::ptr_fun(&print_data2));
  udp->signal_data().connect(sigc::ptr_fun(&print_data3));

  // Start the server. The server will spawn a separate thread to service
  // received data, so this call will immediately return after the thread
  // is spawned.
  udp->start();

  std::cout << "Main thread pid: " << pthread_self() << std::endl;
  // Set up a loop that will run and print the time every 5
  // seconds. Since the server is threaded, the sleep(1) call will not effect
  // the servicing thread.
  std::cout << "Starting..." << std::endl;
  for (int i=0; i <= run_time; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }
  std::cout << "Time: " << run_time << std::endl;

  // Stop the server and prepare for shutdown
  udp->stop();

  return 0;
}

// These are the three callback functions. Each simply prints out the data received.

void print_data1( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<1> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data2( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<2> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data3( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<3> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

IPv4 UDP multicast client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>
#include <glibmm.h>

using namespace Conexus;

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

  Glib::ustring data;
  Glib::ustring mcgroup;
  Glib::ustring local_address;
  int port;
  bool suppress_loopback;
  int hops;

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

  if ( data.size() == 0 ) data = "0123456789";
  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( hops == 0 ) hops = 64;

  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_hops(hops);
  multicast->set_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;
}

IPv4 UDP multicast server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <unistd.h>
#include <iostream>

#include <glibmm.h>

#include <conexus.h>

using namespace Conexus;

void print_data(const Data d);

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

  Glib::ustring mcgroup;
  Glib::ustring iface1, iface2;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 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 );

  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  Conexus::init();

  IPv4::UDP::pointer multicast;

  multicast = IPv4::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;
}

IPv4 UDP client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <iostream>
#include <glibmm.h>

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

  Glib::ustring data;
  Glib::ustring destination;
  int port;

  Glib::OptionContext option_context( "- IPv4 UDP 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_destination;
  option_destination.set_long_name( "dest" );
  option_destination.set_description( "Destination to send data to [default=127.0.0.1]" );
  option_group.add_entry( option_destination, destination );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( data.size() == 0 ) data = "0123456789";
  if ( destination.size() == 0 ) destination = "127.0.0.1";
  if ( port == 0 ) port = 1500;

  Conexus::init();

  // declare the local UDP connection point
  Conexus::IPv4::UDP::pointer udp = Conexus::IPv4::UDP::create();

  // declare the address object that will be used to specify the destination
  Conexus::IPv4::Address addr(destination, port);

  // Example of the sendto method which requires a destination address,
  // but doesn't require a connected port
  Conexus::Data d = Conexus::Data( data.c_str(), data.size() );
  udp->writeto(addr, d);
  std::cout << "1 transmitted" << std::endl;

  // Example of using the connect and send method. The send method doesn't
  // require an address, but instead requires a connected UDP object and
  // just sends to the connected destination.
  udp->set_remote_address(addr);
  udp->set_write_without_connect(true);
  udp->write(data.c_str(), data.size());
  std::cout << "2 transmitted" << std::endl;
  Conexus::Data d2 = Conexus::Data(data.c_str(), data.size());
  udp << d2;
  std::cout << "3 transmitted" << std::endl;

  return 0;
}

IPv4 UDP server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>

#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <glibmm.h>

// prototypes of the three print callback functions
void print_data1(const Conexus::Data d);
void print_data2(const Conexus::Data d);
void print_data3(const Conexus::Data d);

int main(int argc, char* argv[]) {
  Glib::ustring interface;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 UDP Server" );
  Glib::OptionGroup option_group( "Options", "" );

  Glib::OptionEntry option_interface;
  option_interface.set_long_name( "if" );
  option_interface.set_description( "Interface address to receive data on [ default = any ]" );
  option_group.add_entry( option_interface, interface );

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

  option_context.set_main_group( option_group );

  option_context.parse( argc, argv );

  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  // Initialize the Conexus library
  // This is needed because we will use threaded servers and this
  // will do all the behind the scenes work to initialize pthreads
  Conexus::init();

  // Declare the udp object
  Conexus::IPv4::UDP::pointer udp;

  if ( interface.size() == 0 )
    udp = Conexus::IPv4::UDP::create(port);
  else
    udp = Conexus::IPv4::UDP::create(interface, port);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  udp->signal_data().connect(sigc::ptr_fun(&print_data1));
  udp->signal_data().connect(sigc::ptr_fun(&print_data2));
  udp->signal_data().connect(sigc::ptr_fun(&print_data3));

  // Start the server. The server will spawn a separate thread to service
  // received data, so this call will immediately return after the thread
  // is spawned.
  udp->start();

  std::cout << "Main thread pid: " << pthread_self() << std::endl;
  // Set up a loop that will run and print the time every 5
  // seconds. Since the server is threaded, the sleep(1) call will not effect
  // the servicing thread.
  std::cout << "Starting..." << std::endl;
  for (int i=0; i <= run_time; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }
  std::cout << "Time: " << run_time << std::endl;

  // Stop the server and prepare for shutdown
  udp->stop();

  return 0;
}

// These are the three callback functions. Each simply prints out the data received.

void print_data1( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<1> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data2( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<2> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

void print_data3( const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<3> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n";
}

IPv4 UDP multicast client example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <conexus.h>
#include <glibmm.h>

using namespace Conexus;

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

  Glib::ustring data;
  Glib::ustring mcgroup;
  Glib::ustring local_address;
  int port;
  bool suppress_loopback;
  int hops;

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

  if ( data.size() == 0 ) data = "0123456789";
  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( hops == 0 ) hops = 64;

  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_hops(hops);
  multicast->set_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;
}

IPv4 UDP multicast server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation version 2.1.                *
 *                                                                         *
 *   This program 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 Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
 ***************************************************************************/
#include <unistd.h>
#include <iostream>

#include <glibmm.h>

#include <conexus.h>

using namespace Conexus;

void print_data(const Data d);

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

  Glib::ustring mcgroup;
  Glib::ustring iface1, iface2;
  int port;
  int run_time;

  Glib::OptionContext option_context( "- IPv4 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 );

  if ( mcgroup.size() == 0 ) mcgroup = "236.1.1.5";
  if ( port == 0 ) port = 1500;
  if ( run_time == 0 ) run_time = 20;

  Conexus::init();

  IPv4::UDP::pointer multicast;

  multicast = IPv4::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 Tue Mar 13 19:54:48 2007 by  doxygen 1.5.1