Discussion:
[asio-users] Expected behaviour of subsequent read after read_until?
Christian Hammerl
2013-08-14 12:37:41 UTC
Permalink
Hello,

I am currently using asio from boost 1.54 and recently discovered the
following (in my opinion) unexpected behaviour.

The code in question is:

size_t bytes_transferred = read_until(socket, buffer, "foo", error);
bytes_transferred = read(socket, buffer, transfer_all(), error);

The call to read_until results in "foobar" being written to the buffer
and bytes_transferred is set to "3". Everything as expected so far.

However, the following call to read returns "0" for bytes_transferred
and the error_code is set to "eof".
This is just logical because the read function tries to read data from
the socket without checking the buffer first.
Because there is no more data, "0" is returned and "eof" is set for the
error code.

This forces me to manually check the buffer if there is additional data
that is not being processed by the read_until function and I cannot
just rely on the bytes_transferred returned by the functions.

I would expect that the read function does this for me and checks the
buffer content first before attempting to read from the socket just
like a subsequent call to read_until would do.

Then I could just sum up all of the bytes_transferred returned by the
functions and would get the total bytes transferred without the need to
manually inspect the buffer for additional bytes after each call to
read_until.

I would like to know if this is expected behaviour.

Regards,
Christian Hammerl
Shane Powell
2013-08-15 19:50:00 UTC
Permalink
Are you using windows? If so, then I have hit the same problem. See

https://svn.boost.org/trac/boost/ticket/8933

where I posted a patch to fix the problem.
--
Regards,

Shane
Loading...