Search code examples
csocketstcpunistd.h

write() and TCP/IP overhead


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.

Solution

  • 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).