Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out if a message over tcp was delivered

When i send()/write() a message over a tcp stream, how can i find out if those bytes were successfully delivered?

The receiver acknowledges receiving the bytes via tcp, so the senders tcp stack should know.

But when I send() some bytes, send() immediately returns, even if the packet could not (yet) be delivered, i tested that on linux 2.6.30 using strace on netcat, pulling my network cable out before sending some bytes.

I am just developing an application where it is very important to know if a message was delivered, but implementing tcp features ("ack for message #123") feels awkward, there must be a better way.

like image 951
user185582 Avatar asked Oct 07 '09 12:10

user185582


People also ask

Is TCP order delivered?

TCP guarantees delivery of data and also guarantees that packets will be delivered in the same order in which they were sent.

How do I know if my TCP connection is working?

Press the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.

Can TCP packets arrive out of order?

TCP "guarantees" that a receiver will receive the reconstituted stream of bytes as it was originally sent by the sender. However, between the TCP send/receive endpoints (i.e., the physical network), the data can be received out of order, it can be fragmented, it can be corrupted, and it can even be lost.

Does TCP check for lost messages?

When an application or application-layer protocol is through using the connection, it tells TCP to tear it down. The receiving TCP simply receives data and reorders it, asking for lost or missing segments.


2 Answers

The sending TCP does know when the data gets acknowledged by the other end, but the only reason it does this is so that it knows when it can discard the data (because someone else is now responsible for getting it to the application at the other side).

It doesn't typically provide this information to the sending application, because (despite appearances) it wouldn't actually mean much to the sending application. The acknowledgement doesn't mean that the receiving application has got the data and done something sensible with it - all it means is that the sending TCP no longer has to worry about it. The data could still be in transit - within an intermediate proxy server, for example, or within the receiving TCP stack.

"Data successfully received" is really an application-level concept - what it means varies depending on the application (for example, for many applications it would only make sense to consider the data "received" once it has been synced to disk on the receiving side). So that means you have to implement it yourself, because as the application developer, you're really the only one in a position to know how to do it sensibly for your application.

like image 179
caf Avatar answered Oct 26 '22 17:10

caf


Having the receiver send back an ack is the best way, even if it "feels awkward". Remember that IP might break your data into multiple packets and re-assemble them, and this could be done multiple times along a transmission if various routers in the way have different MTUs, and so your concept of "a packet" and TCP's might disagree.

Far better to send your "packet", whether it's a string, a serialized object, or binary data, and have the receiver do whatever checks it needs to do to make it it's there, and then send back an acknowledgement.

like image 23
Paul Tomblin Avatar answered Oct 26 '22 16:10

Paul Tomblin