I'm building a tcp client that must send requests and read responses, but also must be able to detect incoming data from the tcp server that is not a response to a request - the server can originate a tx/rx sequence.
What is the best way to keep an async read active all the time. I tried the following:
In my "handle_connect" method I start an async read and also an async write. The async read looks like this:
size_t bytes_transferred = BUFFER_SIZE; boost::asio::async_read(m_socket, boost::asio::buffer(rcvbuf, bytes_transferred), boost::bind(&CClientSock::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
The async write fills a buffer and then starts the write:
boost::asio::async_write(m_socket, boost::asio::buffer(sndbuf, request_length), boost::bind(&CClientSock::handle_write, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
In the async write handler - it does nothing since the async read is already started.
This doesn't work. The data that should be read from the server's response to the client's write never triggers the async read handler. I'm not actually certain the write ever executes...
Can this be done since the reads and writes are not queued in the normal order? Would I have to start a read, then cancel it if I needed to start a write?