conexus logo

ipv6_udp_client.cpp

This is an example of how to use 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 <conexus.h>

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

int main(int argc, char* argv[]) {
  Glib::ustring data = "0123456789";
  Glib::ustring destination = "::1";
  int port = 1500;

  Glib::OptionContext option_context( "- IPv6 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= ::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::IPv6::UDP::pointer udp = Conexus::IPv6::UDP::create();

  // declare the address object that will be used to specify the destination
  Conexus::IPv6::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;
}

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