开发者

Is there an 'optimal' buffer size when using send()?

Let's say you're transferring a file of arbitrary length in chunks over TCP/IP:

looping...
    read(buffer, LENGTH)
    send(mysocket, buffer, LENGTH, flags)

My question is, what would be optimal开发者_Python百科 value of LENGTH? Or does it not matter at all? I've seen everything from 256 bytes to 8192 bytes being used.


Depends what you mean by optimal. For optimal usage of the bandwidth, you want to maximize the packet size so send at least the network packet size (which on Ethernet is usually about 1500 bytes). If you are reading from disk 4096 or 8192 bytes would be a good value.


If your buffer size translates into packet size, then shorter buffers are better -- less to retransmit in event of a packet error.

ATM took this to the extreme with a 54-byte packet.

But depending upon your library, it might be doing some buffering of its own and setting its packet size independantly. YMMV.


If you are sending large amounts of data over a high latency connection, you can get better throughput with a larger send buffer. Here is a good explanation: http://www.onlamp.com/pub/a/onlamp/2005/11/17/tcp_tuning.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜