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/conexus.h>

#include <iostream>

int main(int argc, char* argv[]) {
  Conexus::init();
  
  // declare the local UDP connection point
  Conexus::IPv4::UDP udp;

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

  // The data to send
  char data[] = "0123456789";

  // Some default values for host and port
  char defaulthost[] = "127.0.0.1";
  char* host = defaulthost;
  int port = 1500;

  // Check to see if user provided command line arguments and change
  // host and port variables if necessary
  if (argc > 1)
    host = argv[1];

  if (argc > 2)
    port = atoi(argv[2]);

  // Set the address object to the destination hostname and port
  addr.set_address(host);
  addr.set_port(port);

  // Example of the sendto method which requires a destination address,
  // but doesn't require a connected port
  udp.writeto(addr, data, 11);
  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, 11);
  std::cout << "2 transmitted" << std::endl;
  Conexus::Data d(data, 11);
  udp << d;
  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/conexus.h>

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

// 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() {
  // 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 udp;

  // Set the port
  udp.local_interface().set_port(1500);

  // 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 for 20 seconds 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=1; i <= 20; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }

  // 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 << "]\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 << "]\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 << "]\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/conexus.h>

using namespace Conexus;

int main(int argc, char* argv[]) {
  Conexus::init();
  
  IPv4::UDP multicast;
  IPv4::Address addr;

  char defaultdata[] = "0123456789";
  char defaulthost[] = "224.1.1.5";
  char* host;
  int port;
  char* data;

  if (argc <= 1)
    host = defaulthost;
  else
    host = argv[1];

  if (argc <= 2)
    port = 1500;
  else
    port = atoi(argv[2]);

  if (argc <= 3)
    data = defaultdata;
  else
    data = argv[3];

  // Since we're just sending, we don't need to do a formal multicast join
  multicast.set_multicast_auto_join(false);
  
  addr.set_address(host);
  addr.set_port(port);
  multicast.set_local_interface(addr);

  multicast.set_multicast_hops(64);
  multicast.set_multicast_loop(true);

  // Do a writeto without connecting
  multicast.writeto(addr, data, strlen(data)+1);

  // After connecting we don't need to supply an address
  multicast.connect(addr);
  multicast.write(data, strlen(data)+1);

  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 <conexus/conexus.h>
#include <unistd.h>
#include <iostream>

using namespace Conexus;

void print_data(Data d);

int main() {
  Conexus::init();

  IPv4::UDP multicast;
  IPv4::Address addr;

  addr.set_address("224.1.1.5");
  addr.set_port(1500);
  multicast.set_local_interface(addr);

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

  return 0;
}

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

Generated on Sat Aug 26 17:34:53 2006 by  doxygen 1.4.6