Marste
Marste

Reputation: 679

TCP mechanism in Linux C stream socket

I'm using a TCP stream socket in a Linux C program to tranmit data. My question is: what happens on packet loss?

Do Linux sockets implement TCP packet recovery mechanisms so that lost packets are resent after a timeout if they get lost? Or do I have to check the send() return value and resend the data if it is zero?

Upvotes: 0

Views: 595

Answers (1)

Guntram Blohm
Guntram Blohm

Reputation: 9819

Your question doesn't make clear if you're using a TCP socket, or a unix domain socket (which appears in the filesystem with an 's' as file type when you ls -l it). But in either of the 2 cases, the answer is yes, they implement packet recovery (not much to lose/recover from in unix sockets however), and no, you don't have to resend data.

You still should check send() for errors however; the connection might be down because someone just unplugged your network cable, or your send() might overflow internal buffers, resulting in a partial send (there are lots of other reasons as well why the send might go wrong). But once you're done with send(), and its return value does not suggest it "forgot" some of the data, you're done.

Note that the connection might still break after your send() is finished and before the receiver gets all of your data. The only way to check for that is have the receiver acknowledge the data, and check on the sender side if you get the acknowledgement. (I'm not talking about TCP ACK here). But then, your connection might break after the receiver gets all data and before you get the complete ACK. If you really need to protect against that, you'll have to implement some transaction scheme, where you reconnect after the connection breaks and ask the receiver which transactions were processed and re-send the others.

Upvotes: 4

Related Questions