conexus logo

ipv4_tcp_echo_client.cpp

Conexus::IPv4::TCP Echo Client Example

/***************************************************************************
 *   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>

void print_data(const Conexus::Data d);

int main(int argc, char* argv[]) {
  // 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]);

  Conexus::init();

  // declare the local UDP connection point
  Conexus::IPv4::TCP::pointer tcp = Conexus::IPv4::TCP::create(host, port);

  tcp->signal_data().connect(sigc::ptr_fun(&print_data));

  // 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.
  tcp->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;
    if (i == 3) {
      std::cout << "1 transmitted" << std::endl;
      tcp->write(data, 11);
    }
    if (i == 6) {
      Conexus::Data d = Conexus::Data(data, 11);
      std::cout << "2 transmitted" << std::endl;
      tcp << d;
    }
    sleep(1);
  }

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

  return 0;
}

void print_data(const Conexus::Data d) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<**> Echo Received " << d.size();
  if (d.size() > 0)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
}


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