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

标签 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”。这可能是什么原因?

最佳答案

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

关于c++ - boost:asio::async_write:已发送数据但未调用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937422/

相关文章:

c++ - boost c++ 中的 XML RSI(KUKA 机器人格式)解析库?

c++ - BOOST_DATA_TEST_CASE是否始终要求 sample 的可打印性?

java - 更快地检测 Java/Android 中的损坏套接字

Java : what is the difference between serversocket and datagramsocket?

c - 限制 TCP Socket 客户端连接

C++98/03 std::is_constructible 实现

c++ - 如何打印结构的每个成员

c++ - 使用链表添加多项式?帮助

c++ - 如何为动态数组实现 operator[]?

c++ - 创建没有图形的边缘描述符