简体   繁体   中英

When performing an async_write with a tcp socket, when is the handler called?

This is just a simple question of how async_write behaves with tcp sockets. Basically, when working with a tcp socket, does the write handler get called when the data gets written to the socket, or when an ack is received from the destination?

AFAIK,只要数据写入套接字的内核缓冲区,就会调用处理程序。

Same behavior as the BSD socket's send() - it completes when the OS has a copy of the data. This will be before an ACK.

The only guarantee provided by Boost.Asio is that the handler will be called when the operation completes. In the case of async_write , the operation is considered complete when any of the following are true:

  • The entire buffer sequence has been written to the stream.
  • The operation has been canceled. For example. socket_.cancel() .
  • An error occurs. For example, the remote endpoint closes their socket.

Once the operation completes, the handler is posted for deferred invocation. However, it is unspecified as to exactly when and the order in which handlers will be invoked. Consider the scenario where an async_write operation has been initiated for 2 different sockets. Any of the following sequences are possible:

  1. async_write operation 1 completes.
  2. operation 1's handler invoked.
  3. async_write operation 2 completes.
  4. operation 2's handler invoked.
  1. async_write operation 1 completes.
  2. async_write operation 2 completes.
  3. operation 1's handler invoked.
  4. operation 2's handler invoked.
  1. async_write operation 1 completes.
  2. async_write operation 2 completes.
  3. operation 2's handler invoked.
  4. operation 1's handler invoked.

I think you need high layer protocals. if a remote peer program blows your request package, ACK is also does not fit your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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