Boost logo

Boost Users :

Subject: Re: [Boost-users] Using boost::asio
From: Michael Caisse (boost_at_[hidden])
Date: 2011-06-02 14:12:09

On 06/02/2011 09:26 AM, Ram Sundar wrote:
> Hi,
> I am trying to use boost::asio for a project where we currently
> use ACE. The way it currently works, the data that is passed between
> the server and client is preceded by a header with the data length.
> Currently with ACE when data is ready to be read at the socket, the
> handler function is invoked, and we invoke a ioctl with FIONREAD to
> read the number of bytes ready to be read and then allocate a buffer
> of that length to read the data. The reason we do this way is because
> the data may be sent in chunks. Internally we keep track of how much
> data we have received etc. I am at a loss to know how to do this using
> boost::asio. All the read methods expect a buffer where data will be
> read and it also needs the size of the buffer. Is there a way we get
> notified when data is ready to be read, and do similar operation. ie
> read the number of bytes available to read and then allocate buffers
> to read the data? Are there any other methods to accomplish the same?
> Thanks,
> Ram

Hi Ram -

async_read_some is your friend. It will allow you to stitch together
messages based on what is available at any particular time. You could
use the read_some method to initially get the header and then use
async_read afterward to read the remaining bytes. With the read_some you
would likely end up (at some point) with the header and part of the
body. Just make sure you take that into account when you initiate the read.

Since you are coming from a reactor implementation, keep in mind that a
proactor has a few important differences. These are outlined in the ASIO
documentation. You may also find my BoostCon'10 presentation on ASIO
useful to get going:

    video :

hth -

Michael Caisse
Object Modeling Designs

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at