Search code examples
c++asynchronousboostboost-asio

Boost asio correctly reading from a socket


I'm familiar with regular sockets and to read unformatted data from them I usually use recv() function, and this is what I expect from it,

For connection-oriented sockets (type SOCK_STREAM for example), calling recv will return as much data as is currently available—up to the size of the buffer specified.

But I'm getting confused with Boost Asio, for example async_read_some,

The read operation may not read all of the requested number of bytes. Consider using the async_read function if you need to ensure that the requested amount of data is read before the asynchronous operation completes.

What does that mean? What should I expect when calling async_read_some, how much data will it read? Can I read everything that is being available on the socket in for example 32 bytes chunks and be sure that I will not miss any data that is being available on the socket?

Lets say I will be reading 32 bytes at a time and now I'm having 36 bytes incoming data, how that will be handled with async_read_some? (one 32 bytes and then the rest?!)

Im new to Boost asio and concept of asynchronous programming, so please forgive any mistake I might have in explaining my problem.

Thanks


Solution

  • I haven't tested it, but I worked with boost::asio and would expect the following:

    async_read_some function will try to read as much data as can. It's limited by buffer size. The indicator for data absence should be boost::asio::error::eof error which is supposed to be correctly handled in a "handler" you pass along with buffer.

    async_read will not call the "handler" until the whole required amount of bytes is read.