开发者

Receive buffer size for tcp/ip sockets

开发者 https://www.devze.com 2022-12-18 01:07 出处:网络
What\'s the maximum data size one should expect in a receive operation? The data that has to be se开发者_开发问答nt is very large, but at some point there will be packet fragmentation I guess?

What's the maximum data size one should expect in a receive operation? The data that has to be se开发者_开发问答nt is very large, but at some point there will be packet fragmentation I guess?


  1. You can always limit the size of the buffer recv() would fill (parameter)
  2. Your application design should not be sensitive to the amount of bytes recv() is willing to provide in one call.

It has little to do with MTU. In some TCP stack designs, one call to recv() would not return more than one datagram of underlying packet protocol. In others, it may be as big as socket's receive buffer.


There is something like maximum network packet size:

MTU

this indicates the max size off the low level buffer (3 iso/osi layer IP) during data transfer over network (not loopback). Which is typically 1492 in Ethernet networks.

So it's worth to optimize data transfer to size of this amount.

(there are also so called Jumbo frames which breaks this rule, but there must be software/hardware which accepts that)

However simple recv() on socket, can return more bytes than MTU. So you need to transfer first packet with the size of the rest data.

size = recv(512) // size should came in one shot 
while( count(data) == size) // the rest of actual data can came sliced, so You should receive until size
    data[offset] = recv(size)
0

精彩评论

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

关注公众号