Boost.Asio async_send question

后端 未结 7 1933
半阙折子戏
半阙折子戏 2021-01-05 14:14

I\'m using Boost.Asio for a server application that I\'m writing.

async_send requires the caller to keep ownership of the data that is being sent until

相关标签:
7条回答
  • 2021-01-05 15:12

    The way that I've been doing it is to really take the "TCP is a stream" concept to heart. So I have a boost::asio::streambuf for each connection to represent what I send to the client.

    Like most of the examples in boost, I have a tcp_connection class with an object per connection. Each one has a memeber boost::asio::streambuf response_; and when I want to send something to the client I just do this:

    std::ostream responce_stream(&response_);
    responce_stream << "whatever my responce message happens to be!\r\n";
    
    boost::asio::async_write(
        socket_,
        response_,
        boost::bind(
            &tcp_connection::handle_write,
            shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    
    0 讨论(0)
提交回复
热议问题