boost:asio :: async_write:已发送数据但未调用处理程序

时间:2015-11-26 11:24:56

标签: c++ boost tcp boost-asio

我有以下类定义:

// SocketTypeT may be e.g. 'boost::asio::ip::tcp::socket'
template<class SocketTypeT> 
class Socket : public SocketTypeT, public boost::enable_shared_from_this< Socket<SocketTypeT> > {
[...]

在这个课程中,我有以下方法'writeAsync':

void writeAsync(const std::string& strData) {               
            boost::asio::async_write(*this, boost::asio::buffer(strData),                                   
                                    boost::bind(&Socket::handle_write_async,
                                    shared_from_this(),
                                    boost::asio::placeholders::error,
                                    boost::asio::placeholders::bytes_transferred));
}

最后在'writeAsync'中使用的处理程序(也是类成员函数):

void handle_write_async(const boost::system::error_code& ec, std::size_t cntBytesSent) {
    cout << "handle_write_async" << endl;
    if (m_pSocketAsyncObserver) {
        m_pSocketAsyncObserver->handleAsyncWrite(connectionClosed, cntBytesSent, ec);
    }
}

问题:

数据已成功传输到服务器,但永远不会调用“handle_write_async”。可能是什么原因?

1 个答案:

答案 0 :(得分:0)

要继续执行run,您需要提供io_service::work个对象。请阅读this question