Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How async_read_some() in boost::asio::ip::tcp::socket actually works?

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?

like image 618
Amumu Avatar asked Sep 03 '25 06:09

Amumu


1 Answers

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 the io_service that you passed to the constructor of the ip::tcp::socket, the io_service will check to see if the async_read_some() has read any data.

If data has been read, then it will call the ReadHandler callback that you passed to async_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().

like image 145
James Brock Avatar answered Sep 04 '25 19:09

James Brock