我有以下类定义:
// 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”。这可能是什么原因?
最佳答案
要连续执行run
,您需要提供io_service::work
对象。请阅读this question
关于c++ - boost:asio::async_write:已发送数据但未调用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937422/