IPv4 TCPServer Endpoint

For the corresponding clients, see IPv4 TCP Endpoint

IPv4 TCP 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  std::cout << "Spawning connection from thread pid: " << pthread_self() << std::endl;
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data1));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data2));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data3));
  endpoint->start();
  connections.push_back(endpoint);
}

// 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();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
}

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

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

IPv4 TCP Echo 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  endpoint->signal_data().connect(sigc::bind(sigc::ptr_fun(&echo_data),endpoint));
  endpoint->start();
  connections.push_back(endpoint);
}



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

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<**> Received " << d.size();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
  endpoint->write(d);
}

For the corresponding clients, see IPv4 TCP Endpoint

IPv4 TCP 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  std::cout << "Spawning connection from thread pid: " << pthread_self() << std::endl;
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data1));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data2));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data3));
  endpoint->start();
  connections.push_back(endpoint);
}

// 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();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
}

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

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

IPv4 TCP Echo 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  endpoint->signal_data().connect(sigc::bind(sigc::ptr_fun(&echo_data),endpoint));
  endpoint->start();
  connections.push_back(endpoint);
}



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

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<**> Received " << d.size();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
  endpoint->write(d);
}

For the corresponding clients, see IPv4 TCP Endpoint

IPv4 TCP 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  std::cout << "Spawning connection from thread pid: " << pthread_self() << std::endl;
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data1));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data2));
  endpoint->signal_data().connect(sigc::ptr_fun(&print_data3));
  endpoint->start();
  connections.push_back(endpoint);
}

// 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();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
}

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

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

IPv4 TCP Echo 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 <vector>

std::vector<Conexus::Endpoint::pointer> connections;

void on_new_connection(Conexus::Endpoint::pointer endpoint);

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint);

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::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // 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;
    sleep(1);
  }

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

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  endpoint->signal_data().connect(sigc::bind(sigc::ptr_fun(&echo_data),endpoint));
  endpoint->start();
  connections.push_back(endpoint);
}



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

void echo_data(const Conexus::Data d, Conexus::Endpoint::pointer endpoint) {
  std::cout << "Responding thread pid: " << pthread_self() << std::endl;
  std::cout << "<**> Received " << d.size();
  if (d)
    std::cout << " bytes of data (" << *((uint64_t*)(d.data())) << ") [" << d.data() << "]";
  std::cout << std::endl;
  endpoint->write(d);
}


Generated on Tue Mar 13 19:54:48 2007 by  doxygen 1.5.1