On 2/08/2017 13:30, jupiter wrote:
I have a socket server program to activate in asio::async_read for receive any messages initiated from client. But when the server sends a message to the client, it uses sync write and flowing a sync read (asio::read) to immediately receive the response. The issue I can think of is who will get first read when both asio::async_read and asio::read are active?
One or both of them selected at semi-random by the OS. Basically: don't do that. (Especially because both read and async_read are not necessarily single operations -- they can internally be composed of multiple calls to [async_]read_some.) Each individual data block will only be received by at most one of the handlers, but a single "response" could come in as arbitrarily many blocks, and if you have multiple read operations pending simultaneously then the chances of multiple blocks hitting multiple handlers are nearly certain. You will need to remove the sync read and only do the async read; your async read handler (or downstream code) will need to recognise the difference between an unsolicited message and a response and handle it accordingly.