Discussion:
[asio-users] async_read vs. async_read_some
Ruediger Berlich
2007-06-21 00:40:54 UTC
Permalink
Dear all,

apologies if this is an obvious question - I am new to ASIO. I am looking
for pointers on the difference between socket.async_read_some(...) and
async_read(socket, ...). I have tried to dig through the examples and
online-documentation, but am still not sure I understand it.

Thanks for your help,
Ruediger
Christopher Kohlhoff
2007-06-21 14:14:45 UTC
Permalink
On Thu, 21 Jun 2007 02:40:54 +0200, "Ruediger Berlich"
Post by Ruediger Berlich
Dear all,
apologies if this is an obvious question - I am new to ASIO. I am looking
for pointers on the difference between socket.async_read_some(...) and
async_read(socket, ...). I have tried to dig through the examples and
online-documentation, but am still not sure I understand it.
The key phrase is found under the Remarks section for the docs on
async_read_some:

http://asio.sourceforge.net/boost_asio_0_3_8/libs/asio/doc/html/boost_asio/reference/basic_stream_socket/async_read_some.html

"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."

Basically a successful call to async_read_some may read just one byte,
or it may fill the whole buffer, or anywhere in between. The
asio::async_read function, OTOH, can be used to ensure that the entire
buffer is filled before the operation completes. The async_write_some
and asio::async_write functions have the same relationship.

Cheers,
Chris
Ruediger Berlich
2007-06-21 15:18:58 UTC
Permalink
Chris,

thanks a lot!
Post by Christopher Kohlhoff
Basically a successful call to async_read_some may read just one byte,
or it may fill the whole buffer, or anywhere in between. The
asio::async_read function, OTOH, can be used to ensure that the entire
buffer is filled before the operation completes. The async_write_some
and asio::async_write functions have the same relationship.
Essentially, what I'm trying to do is to transmit objects of varying sizes,
serialized through Boost.serialization, over an SSL connection. Some remote
instance modifies them and sends the modified objects back.

Following the "serialization" example in ASIO, I'm trying to use
gather-write to send various pieces of status information (the size of the
serialized data, a checksum to ensure integrity of the data, etc.) together
with the data itself.

What is the best starting point for this ? Should I try to ssl-ify
$ASIOHOME/src/examples/serialization/* ? Or should I rather try to make
$ASIOHOME/src/examples/ssl aware of data buffers with unknown size at the
time of reception (as per the serialization example) ?

My assumption is that the serialization example is a better starting point,
as it doesn't seem to make many assumptions about the objects being
serialized.
Christopher Kohlhoff
2007-06-25 13:33:34 UTC
Permalink
On Thu, 21 Jun 2007 17:18:58 +0200, "Ruediger Berlich"
Post by Ruediger Berlich
What is the best starting point for this ? Should I try to ssl-ify
$ASIOHOME/src/examples/serialization/* ? Or should I rather try to
make $ASIOHOME/src/examples/ssl aware of data buffers with unknown
size at the time of reception (as per the serialization example) ?
It should be very easy to change the serialization example's connection
class to use ssl::stream<tcp::socket> rather than just tcp::socket.
(Even better, you could change it to be a template class where the
template parameter is the stream type -- either ssl::stream<> or plain
tcp::socket.) Then it should be quite simple to integrate that class
into a program based on the examples/ssl client or server.
Aljaz
2007-07-07 20:53:22 UTC
Permalink
Post by Ruediger Berlich
Following the "serialization" example in ASIO, I'm trying to use
gather-write to send various pieces of status information (the size of the
serialized data, a checksum to ensure integrity of the data, etc.) together
with the data itself.
How are you going to implement a checksum to ensure integrity of the data?

I need a similar approach with my server, but dont have the right idea
how to do it..

Regards
A
Ruediger Berlich
2007-07-07 21:49:18 UTC
Permalink
Hi there,

Boost has a CRC library which is quite easy to use (at least for the
character-based communication I'm doing). See
http://www.boost.org/libs/crc/index.html . The checksum can
be sent as a separate buffer with ASIO's gather/write procedure.
If data and checksum are different after the tansmission, one or
both have not arrived intact and you re-transmit the data.

However, I have found that, together with encryption, the overhead
is quite severe, particularly for frequent communication. Plus,
if you can decrypt the data on the other end, chances are the data
is intact anyway.

Hope that helps,
Ruediger
Post by Aljaz
Post by Ruediger Berlich
Following the "serialization" example in ASIO, I'm trying to use
gather-write to send various pieces of status information (the size of
the serialized data, a checksum to ensure integrity of the data, etc.)
together with the data itself.
How are you going to implement a checksum to ensure integrity of the data?
I need a similar approach with my server, but dont have the right idea
how to do it..
Regards
A
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
Arash Partow
2007-07-08 06:37:19 UTC
Permalink
Hi Ruediger,
Post by Ruediger Berlich
Hi there,
Plus, if you can decrypt the data on the other end, chances are the data
is intact anyway.
All decent encryption technologies (symmetric and asymmetric) can not
validate their decryption process. One must instead use some kind of
signature(hash) to verify what they have is what they were
sent/wanted. if the decryption process could determine whether or not
it was successful one could theorise that it could also tell you if
the bit that is currently being decrypted is successful or not, if
thats the case then all one needs to do to break such encryption is
permute the state of each bit so that every query for success/failure
results in a positive outcome.

A CRC of the plain-text coupled with the encrypted text is a good
initial attempt but will cause some problems from a security pov -
best use some kind of cryptographic hash.



Arash
Post by Ruediger Berlich
Hope that helps,
Ruediger
Post by Aljaz
Post by Ruediger Berlich
Following the "serialization" example in ASIO, I'm trying to use
gather-write to send various pieces of status information (the size of
the serialized data, a checksum to ensure integrity of the data, etc.)
together with the data itself.
How are you going to implement a checksum to ensure integrity of the
data?
Post by Aljaz
I need a similar approach with my server, but dont have the right idea
how to do it..
Regards
A
Aljaz
2007-12-22 19:12:55 UTC
Permalink
Has anyone attempted to implement something similar with asio?
Post by Arash Partow
Hi Ruediger,
Post by Ruediger Berlich
Hi there,
Plus, if you can decrypt the data on the other end, chances are the data
is intact anyway.
All decent encryption technologies (symmetric and asymmetric) can not
validate their decryption process. One must instead use some kind of
signature(hash) to verify what they have is what they were
sent/wanted. if the decryption process could determine whether or not
it was successful one could theorise that it could also tell you if
the bit that is currently being decrypted is successful or not, if
thats the case then all one needs to do to break such encryption is
permute the state of each bit so that every query for success/failure
results in a positive outcome.
A CRC of the plain-text coupled with the encrypted text is a good
initial attempt but will cause some problems from a security pov -
best use some kind of cryptographic hash.
Arash
Post by Ruediger Berlich
Hope that helps,
Ruediger
Post by Aljaz
Post by Ruediger Berlich
Following the "serialization" example in ASIO, I'm trying to use
gather-write to send various pieces of status information (the size of
the serialized data, a checksum to ensure integrity of the data, etc.)
together with the data itself.
How are you going to implement a checksum to ensure integrity of the
data?
Post by Aljaz
I need a similar approach with my server, but dont have the right idea
how to do it..
Regards
A
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
Loading...