简体   繁体   English

write_some vs write - boost asio

[英]write_some vs write - boost asio

Why do someone want to use write_some when it may not transmit all data to peer? 为什么有人想在不能将所有数据传输到对等write_some时使用write_some

from boost write_some documentation 来自boost write_some文档

The write_some operation may not transmit all of the data to the peer. write_some操作可能不会将所有数据传输到对等方。 Consider using the write function if you need to ensure that all data is written before the blocking operation completes. 如果需要确保在阻塞操作完成之前写入所有数据,请考虑使用write函数。

What is the relevance of write_some method in boost when it has write method? write_some方法有write方法时,它与boost的相关性是什么? I went through the boost write_some documentation,nothing I can guess. 我查看了boost write_some文档,我无法猜到。

At one extreme, write waits until all the data has been confirmed as written to the remote system. 在一个极端, write等待,直到所有数据都被确认为写入远程系统。 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. 如果路由器出现故障,DNS提供了错误的地址等,您可能会尝试发送到某些不可用的机器(可能)已经很长时间没有。

write_some is kind of a halfway point between these two extremes. write_some是这两个极端之间的中间点。 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. 您正在通过(例如)不可靠的Internet连接发送少量数据。 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. 连接不够可靠,传输数据包的几率实际上与其他数据包的几率无关 - 也就是说,发送一个数据包几乎不能告诉你发送下一个数据包的可能性。

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). basic_stream_socket::write_some是套接字上的一个操作,它几乎包含了操作系统的send操作(大多数send实现不保证完整消息的传输)。 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. asio::write是一个高级包装器,它将循环直到发送所有数据。 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. 使用write_some一个可能原因可能是移植基于套接字并且已经进行循环的现有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM