如何通过boost asio支持TCP服务器中的多个连接

时间:2014-03-05 07:53:05

标签: c++ networking boost tcp boost-asio

我有以下TCP服务器代码:

#include <algorithm>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>

#include <iostream>
#include <string>

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

class tcp_connection
  : public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& io_service)
  {
    return pointer(new tcp_connection(io_service));
  }

  tcp::socket& socket()
  {
    return _socket;
  }

  void start()
  {
    boost::asio::async_read_until(
      _socket
      , _message
      , "\r\n"
      , boost::bind(
        &tcp_connection::handle_read
        , this->shared_from_this()
        , boost::asio::placeholders::error
      )
    );
  }

private:
  tcp_connection(boost::asio::io_service& io_service)
    : _socket(io_service) {}

  void handle_read(const boost::system::error_code& error)
  {
    if (!error)
    {
      std::cout << &_message << '\n';
    }
  }

  tcp::socket _socket;
  boost::asio::streambuf _message;
};

class tcp_server
{
public:
  tcp_server(boost::asio::io_service& io_service)
    : _acceptor(io_service, tcp::endpoint(tcp::v4(), 13))
  {
    start_accept();
  }

private:
  void start_accept()
  {
    tcp_connection::pointer new_connection =
      tcp_connection::create(_acceptor.get_io_service());

    _acceptor.async_accept(
      new_connection->socket()
      , boost::bind(
        &tcp_server::handle_accept
        , this
        , new_connection
        , boost::asio::placeholders::error
      )
    );
  }

  void handle_accept(
    tcp_connection::pointer new_connection
    , const boost::system::error_code& error
  )
  {
    if (!error)
    {
      new_connection->start();
    }

    start_accept();
  }

  tcp::acceptor _acceptor;
};

int main()
{
  try
  {
    boost::asio::io_service io_service;
    tcp_server server(io_service);
    io_service.run();
  }
  catch (const std::exception& ex)
  {
    std::cerr << ex.what() << '\n';
  }
}

我是否正确,此代码不能正确支持多个同时连接,因为在处理当前消息时async_accept不活动?我的意思是:

  void handle_accept(
    tcp_connection::pointer new_connection
    , const boost::system::error_code& error
    )
  {
    // We are unable to accept new connections
    if (!error)
    {
      new_connection->start();
    }
    // before start_accept function call

    start_accept();
  }

如果是,我该如何解决这个问题?如何通过具有多个连接支持的boost asio编写简约异步TCP服务器?

1 个答案:

答案 0 :(得分:1)

此代码支持多个同时连接,因为使用的boost :: asio调用是异步的。 new_connection-&gt; start()立即返回,并调用start_accept()。