conexus logo

factory_client.cpp

Conexus::Factory client example

/***************************************************************************
 *   Copyright (C) 2007 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 <iostream>
#include <glibmm.h>

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

  Glib::ustring data = "0123456789";
  Glib::ustring destination = "127.0.0.1";
  int port = 1500;

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

  Conexus::init();

  // declare the local UDP connection point
  Conexus::Endpoint::pointer ptr = Conexus::default_factory().create_endpoint( "IPv4::UDP" );

  Conexus::IPv4::UDP::pointer udp = conexus_dynamic_pointer_cast<Conexus::IPv4::UDP>(ptr);

  // 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);
  ptr->write(data.c_str(), data.size());
  std::cout << "2 transmitted" << std::endl;
  Conexus::Data d2 = Conexus::Data(data.c_str(), data.size());
  ptr << d2;
  std::cout << "3 transmitted" << std::endl;

  return 0;
}

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