Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write_some vs write - boost asio

Why do someone want to use write_some when it may not transmit all data to peer?

from boost write_some documentation

The write_some operation may not transmit all of the data to the peer. Consider using the write function if you need to ensure that all data is written before the blocking operation completes.

What is the relevance of write_some method in boost when it has write method? I went through the boost write_some documentation,nothing I can guess.

like image 511
InQusitive Avatar asked Sep 15 '16 17:09

InQusitive


2 Answers

At one extreme, write waits until all the data has been confirmed as written to the remote system. It gives the greatest certainty of successful completion at the expense of being the slowest.

At the opposite extreme, you could just queue the data for writing and return immediately. This is fast, but gives no assurance at all that the data will actually be written. If a router was down, a DNS giving incorrect addresses, etc., you could be trying to send to some machine that isn't available and (possibly) hasn't been for a long time.

write_some is kind of a halfway point between these two extremes. It doesn't return until at least some data has been written, so it assures you that the remote host you were trying to write to does currently exist (for some, possibly rather loose, definition of "currently"). It doesn't assure you that all the data will be written but may complete faster, and still gives a little bit of a "warm fuzzy" feeling that the write is likely to complete.

As to when you'd likely want to use it: the obvious scenario would be something like a large transfer over a local connection on a home computer. The likely problem here isn't with the hardware, but with the computer (or router) being mis-configured. As soon as one byte has gone through, you're fairly assured that the connection is configured correctly, and the transfer will probably complete. Since the transfer is large, you may be saving a lot of time in return for a minimal loss of assurance about successful completion.

As to when you'd want to avoid it: pretty much reverse the circumstances above. You're sending a small amount of data over (for example) an unreliable Internet connection. Since you're only sending a little data, you don't save much time by returning before all the data's sent. The connection is unreliable enough that the odds of a packet being transmitted are effectively independent of the odds for other packets--that is, sending one packet through tells you little about the likelihood of being able to send the next.

like image 94
Jerry Coffin Avatar answered Oct 30 '22 10:10

Jerry Coffin


There is no reason really. But these functions are at different levels.

  • basic_stream_socket::write_some is an operation on a socket that pretty much wraps the OS's send operation (most send implementations do not guarantee transmission of the complete message). Normally you wrap this call in a loop until all of the data is sent.
  • asio::write is a high-level wrapper that will loop until all of the data is sent. It accepts a socket as an argument.

One possible reason to use write_some could be when porting existing code that is based on sockets and that already does the looping.

like image 36
rustyx Avatar answered Oct 30 '22 12:10

rustyx