views:

270

answers:

2

What's the maximum data size one should expect in a receive operation? The data that has to be sent is very large, but at some point there will be packet fragmentation I guess?

A: 

There is somethng 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)
bua
+3  A: 
  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.

Pavel Radzivilovsky