views:

266

answers:

2

Data may be read from or written to a connected TCP socket using the receive(), async_receive(), send() or async_send() member functions. However, as these could result in short writes or reads, an application will typically use the following operations instead: read(), async_read(), write() and async_write().

I don't really understand that remark as read(), async_read(), write() and async_write() can also end up in short writes or reads, right?
Why are those functions not the same?
Should I use them at all?
Can someone clarify that remark for me?

+2  A: 

The read, async_read, write, and async_write are composed functions that call the class functions multiple times until the requested number of bytes is transmitted. They are included by the library as a convenience. Otherwise, every developer would need to implement the same logic.

The class functions wrap the underlying OS functions directly, which basically state in the documentation: these functions may return before all of the bytes are transmitted.

In most cases, you should use the free (composed) functions to transmit data.

Dan
Why was it designed this way? In which case you don't need to receive all the bytes that were requested?Why doesn't the socket object include the read/write/async_read/async_write? You are passing a socket anyway.
the_drow
I don't know why, but you can probably find the answer in the "Networking Library Proposal for TR2." http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2054.pdf
Dan
Regarding the previously linked document, look at page 8.
Dan
A: 

First of all, you have to understand the word "asynchronous", it simply means "need not to wait". After asynchronous actions are invoked, the following action will execute without waiting for the asynchronous action return. While synchronous have to wait until previous synchronous actions return. The two following samples from Boost.Asio would make sense: A synchronous TCP daytime server

(Oops! not enough reputation, the second sample is easy to find though, called "An asynchronous TCP daytime server")

rhapsodyn
How is this related to my question?
the_drow
sorry.... i assume the first paragraph in your question was a quote... so i ingore them.....
rhapsodyn