当我使用TCP发送数据时,我如何知道数据是否已到达?

时间:2016-03-16 07:48:20

标签: linux tcp tcp-ip

当我使用TCP发送数据时,write()函数只是确保数据已被复制到TCP发送缓冲区,但如果TCP不能成功发送数据,我怎么知道?有信号吗?或者什么?

1 个答案:

答案 0 :(得分:2)

Short Answer: you don't. Conventionally the remote TCP peer sends a response, acknowledging your data. This is the 1st step toward building an application level protocol, atop TCP, your transport level protocol.

Longer Answer: This problem is the prime motivation for higher level protocols such as HTTP, STOMP, IMAP, etc.

but if TCP doesn't send data successfully, how do I know?

The write() system call can return -1 and set errno to indicate an error, however you cannot know how much data has been received by the remote peer, and how much was not. That question is best answered by the remote peer.

M.