| Home | Libraries | People | FAQ | More

# echo/blocking_tcp_echo_server.cpp

    #include <cstdlib>
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/smart_ptr.hpp>
    #include <boost/asio.hpp>
    #include <boost/thread.hpp>

    using boost::asio::ip::tcp;

    const int max_length = 1024;

    typedef boost::shared_ptr<tcp::socket> socket_ptr;

    void session(socket_ptr sock)
    {
      try
      {
        for (;;)
        {
          char data[max_length];

          boost::asio::error error;
          size_t length = sock->read_some(
              boost::asio::buffer(data), boost::asio::assign_error(error));
          if (error == boost::asio::error::eof)
            break; // Connection closed cleanly by peer.
          else if (error)
            throw error; // Some other error.

          boost::asio::write(*sock, boost::asio::buffer(data, length));
        }
      }
      catch (boost::asio::error& e)
      {
        std::cerr << "Error in thread: " << e << "\n";
      }
      catch (std::exception& e)
      {
        std::cerr << "Exception in thread: " << e.what() << "\n";
      }
    }

    void server(boost::asio::io_service& io_service, short port)
    {
      tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
      for (;;)
      {
        socket_ptr sock(new tcp::socket(io_service));
        a.accept(*sock);
        boost::thread t(boost::bind(session, sock));
      }
    }

    int main(int argc, char* argv[])
    {
      try
      {
        if (argc != 2)
        {
          std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
          return 1;
        }

        boost::asio::io_service io_service;

        using namespace std; // For atoi.
        server(io_service, atoi(argv[1]));
      }
      catch (boost::asio::error& e)
      {
        std::cerr << e << "\n";
      }
      catch (std::exception& e)
      {
        std::cerr << "Exception: " << e.what() << "\n";
      }

      return 0;
    }

| Copyright © 2003 - 2006 Christopher M. Kohlhoff