EOF之后的boost :: asio接受器重新打开和异步读取

时间:2013-04-10 12:38:20

标签: c++ sockets boost c++11 boost-asio

遗憾的是,由于我无法理解应该如何使用boost :: asio :: ip :: tcp :: acceptor,所以我在开发过程中处于困境。 TCPReader类设计为单例,标头将以下内容声明为私有。

void runInThread();

void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred);

void accept_handler(const boost::system::error_code &ec);

boost::asio::io_service io_service;
boost::asio::io_service::work runningService;
boost::asio::ip::tcp::socket sock;
boost::asio::ip::tcp::acceptor acceptor;

bool isConnected;

<<

TCPReader::TCPReader() : sock(io_service),
                        runningService(io_service),
                        acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), TCPPort)){
    this->isConnected = false;

    acceptor.async_accept(sock, boost::bind(&TCPReader::accept_handler, this, boost::asio::placeholders::error));
    boost::thread thread(boost::bind(&TCPReader::runInThread, this));
}


 void TCPReader::runInThread(){
    io_service.run();
}

accept_handler比触发read_handler并且它都像魅力一样工作,直到read_handler收到EOF或其他错误。我想要的是让接受器回到一个状态,它可以像初始化后那样接受连接。我尝试了close()cancel()以及open()listen()async_accept(就像在构造函数中一样)。但这一切似乎都不起作用。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

sock上的操作发生错误,而不是acceptor。因此,acceptor的状态不应受到影响。它只需要在async_accept处于初始关闭状态时启动sock操作。

这是一个侦听端口12345的完整基本示例:

#include <iostream>

#include <boost/array.hpp>
#include <boost/asio.hpp>

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

class tcp_reader
{
public:
  tcp_reader(boost::asio::io_service& io_service)
    : io_service_(io_service),
      socket_(io_service),
      acceptor_(io_service, 
                tcp::endpoint(tcp::v4(), 12345))
  {
    accept_connection();
  }

private:

  void accept_connection()
  {
    std::cout << "accepting connection" << std::endl;
    // Verify socket is in a closed state.
    socket_.close();
    // On success or failure, acceptor will open() socket_.
    acceptor_.async_accept(socket_,
      boost::bind(&tcp_reader::handle_accept, this,
                  boost::asio::placeholders::error));
  }

  void handle_accept(const boost::system::error_code& error)
  {
    // On error, return early.
    if (error)
    {
      std::cout << "handle_accept: " << error.message() << std::endl;
      return;
    }

    // Start reading from socket.
    read();    
  }

  void read()
  {
    std::cout << "reading from socket" << std::endl;
    async_read(socket_, boost::asio::buffer(buffer_),
      boost::asio::transfer_at_least(1),
      boost::bind(&tcp_reader::handle_read, this,
                  boost::asio::placeholders::error,
                  boost::asio::placeholders::bytes_transferred));
  }

  void handle_read(const boost::system::error_code& error,
                   std::size_t bytes_transferred)
  {
    // On error, go back to listening for a new connection.
    if (error)
    {
      std::cout << "handle_read: " << error.message() << std::endl;
      accept_connection();
      return;
    }

    // Output read data.
    std::cout.write(&buffer_[0], bytes_transferred);

    // Read data, so read some more.
    read(); 
  }

private:
  boost::asio::io_service& io_service_;
  tcp::socket              socket_;
  tcp::acceptor            acceptor_;
  boost::array<char, 1024> buffer_;
};

int main()
{
  boost::asio::io_service io_service;
  tcp_reader reader(io_service);
  io_service.run();
}

使用它时,我在一个终端中运行它,并连接到端口12345,发送​​消息,终止连接,然后重新建立连接。服务器控制台输出如下:

[twsansbury@localhost]$ ./a.out 
accepting connection
reading from socket
hello
reading from socket
goodbye
reading from socket
handle_read: End of file
accepting connection
reading from socket
oh, oh no
reading from socket
handle_read: End of file
accepting connection
ctrl + c

和客户端控制台:

[twsansbury@localhost]$ nc 127.0.0.1 12345
hello        
goodbye 
ctrl + c
[twsansbury@localhost]$ nc 127.0.0.1 12345
oh, oh no
ctrl + c

需要注意的一个行为细节是,虽然async_accept上的acceptor_操作可能没有待处理,但连接仍在排队。因此,如果已经接受了连接,并且另一个客户端尝试连接,则后一个客户端将使其连接等待接受。如果这不是所需的行为,则接受者需要通过close()转换出监听状态。 This回答图表状态转换并更详细地描述了一些接受者行为。