开发者

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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜