简体   繁体   中英

boost:asio::async_write: Data sent but handler not called

I have the following class definition:

// 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> > {
[...]

Within this class I have the following method '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));
}

And finally the handler (also a class member function) used in '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);
    }
}

Problem:

The data is successfully transmitted to the server, however 'handle_write_async' gets never called. What might be the reason for this?

For continuous execution of run you need to supply io_service::work object. Please read this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM