Villetaneuse
Villetaneuse

Reputation: 185

tcp send and recv: always in loops?

What are the best practices when sending (or writing) and recving (or reading) to/from a TCP socket ?

Assume usual blocking I/O on sockets. From what I understand :

Am I correct ? Or is there some "small message size" or other conditions allowing to read safely outside a loop ?

I am confused because I have seen examples of "naked reads", for instance in Tanenbaum-Wetherall:

read(sa, buf, BUF_SIZE); /* read file name in socket */

Upvotes: 0

Views: 985

Answers (1)

pm100
pm100

Reputation: 50110

Yes you must loop on the receive

Once a week I answer a question where someones TCP app stops working for this very reason. The real killer is that they developped the client and server on the same machine, so they get loopback connection. Almost all the time a loopback will receive the send messages in the same blocks as they were sent. This makes it look like the code is correct.

The really big challenge is that this means you need to know before the loop how big the message is that you are going to receive. Possibilities

  • send a fixed length length (ie you know its , say, 4 bytes) first.
  • have a recognizable end sequence (like the double crlf at the end of an HTTP request.
  • Have a fixed size message

I would always have a 'pull the next n bytes' function.

Writing should loop too, but that easy, its just a matter of looping.

Upvotes: 1

Related Questions