write() and TCP/IP overhead

1k views Asked by At

If I am writing to a socket file descriptor using write() bytes by bytes,

  • Is every byte now a packet?
  • will the socket add TCP/IP header to every byte?
  • Or does it have a buffer mechanism (I personally doubt so since I don't have explicitly flush).

For example:

write(fd, 'a', 1);
write(fd, 'b', 1);
write(fd, 'c', 1);

Will this be less efficient than say

write (fd, 'abc', 3);
  • I have to ask this here because I do not have the expertise to monitor TCP/IP header in traffic. Thanks.
3

There are 3 answers

0
John Zwinck On BEST ANSWER

No, not every byte will become a packet. Some may be coalesced due to Nagle's Algorithm and other things. There will be one TCP header per packet, not per byte.

That said, you should avoid calling write/send byte by byte because each one is a system call, which is expensive (on the local machine, not in terms of how it ends up on the network).

1
chrisaycock On

Adding to John's answer, you can disable Nagle's Algorithm (via TCP_NODELAY) and then the first version will become slower.

And for the reverse, you can call writev() instead of write(), which will cause the first version to perform exactly as the second.

1
davidstites On

It really depends on the implementation of the TCP/IP stack. It would really depend on the segmentation that is implemented in the OS. Most OSes have a lot of optimization already built in.

If you're looking at a worst case situation, a TCP header is 20 bytes, an IP header is 20 bytes and the size of the frame header (depending on the protocol you're using, probably ethernet), so you could expect that plus your payload. That being said, the majority of the traffic in the internet is dominated by ACKs however, your network stack should combine the payloads.