是否有一个“最佳”?使用send()时的缓冲区大小?
假设您正在通过 TCP/IP 传输任意长度的文件:
looping...
read(buffer, LENGTH)
send(mysocket, buffer, LENGTH, flags)
我的问题是,LENGTH 的最佳值是多少?或者根本不重要?我见过从 256 字节到 8192 字节的所有内容都被使用。
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 value of LENGTH? Or does it not matter at all? I've seen everything from 256 bytes to 8192 bytes being used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
取决于你所说的最佳是什么意思。为了优化带宽的使用,您希望最大化数据包大小,因此至少发送网络数据包大小(在以太网上通常约为 1500 字节)。如果您从磁盘读取,4096 或 8192 字节将是一个不错的值。
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.
如果缓冲区大小转换为数据包大小,则缓冲区越短越好——发生数据包错误时重传的次数更少。
ATM 通过 54 字节数据包将这一点发挥到了极致。
但根据您的库,它可能会自己进行一些缓冲并独立设置其数据包大小。 YMMV。
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.
如果您通过高延迟连接发送大量数据,则可以使用更大的发送缓冲区获得更好的吞吐量。这是一个很好的解释:
http://www.onlamp.com/pub/ a/onlamp/2005/11/17/tcp_tuning.html
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