It is written in the documentation:
This function is used to asynchronously read data from the stream
socket. The function call always returns immediately.
I know it is asynchronous, so it returns immediately. But what does async_read_some() differ from free function read()? When I try to std::cout my buffer used for async_read_some(), it seems that the function reads many times until the stream is out of data.
Does this mean async_read_some() request continuously until it receives every data, for example, in a HTTP GET request? And the server will write little at a time and send a little to the client (for async_read_some() to read a little bit of whole data), or it dumps all data to the client at once?
No,
async_read_some()does not request continuously.The
ip::tcp::socket::async_read_some()function will make a system call that starts the read.After that, the next time you call
io_service::run()on theio_servicethat you passed to the constructor of theip::tcp::socket, theio_servicewill check to see if theasync_read_some()has read any data.If data has been read, then it will call the
ReadHandlercallback that you passed toasync_read_some().If data has not yet been read, it will return and check for completion again the next time you call
io_service::run().