c++11 – Call async_write from outside connect handler in asio

I use the asio library to make TCP connections. The async read/write operation is made from the handler function of async_accept.

        mAcceptor.async_accept(*(mConnection->Socket()),
                boost::bind(&TCPConnection::HandleAcceptForWrite, this, 
                pI8Msg, 
                boost::asio::placeholders::error));
void TCPConnection::HandleAcceptForWrite(
        INT8* pI8Msg, 
        const boost::system::error_code& err)
{
    if (!err) {
        TransmitData(pI8Msg);//--> Want to call this fn from outside the handler
    }
    SocketAcceptConnection(pI8Msg);
}

I want to avoid the call to TransmitData (async_write) from within the handler.

I intend to call write from anywhere outside the handler of accept. When I do this I get the error – ‘Bad file descriptor’

Is it always necessary to do an async write from within the handler? Please share any code sample if it can be called from elsewhere.

Read more here: Source link