What is a good buffer size for socket programming?
http://stackoverflow.com/questions/2811006/what-is-a-good-buffer-size-for-socket-programming
问题:
We are using .Net and sockets.
The server is using the Socket.Sender(bytes[]) method so it just sends the entire payload.
On the other side we are clients consuming the data.
Socket.Receive(buffer[]).
In all the examples from Microsoft (and others) they seem to stick with a buffer size of 8192.
We have used this size but 【every now and then不时地,常常】 we are sending data down to the clients that exceeds this buffer size.
Is there a way of determining how much data the server's sent method sent us? What is the best buffer size?
回答:
Even if you're sending more data than that, it may well not be available in one call to Receive.
You can't determine how much data the server has sent - it's a stream of data, and you're just reading chunks at a time.
You may read part of what the server sent in one Send call, or you may read the data from two Send calls in one Receive call.
8K is a reasonable buffer size - not so big that you'll waste a lot of memory, and not so small that you'll have to use loads of wasted Receive calls.
4K or 16K would quite possibly be fine too... I personally wouldn't start going above 16K for network buffers - I suspect you'd rarely fill them.
You could experiment by trying to use a very large buffer and log how many bytes were received in each call - that would give you some idea of how much is generally available -
but it wouldn't really show the effect of using a smaller buffer.
What concerns do you have over using an 8K buffer?
If it's performance, do you have any evidence that this aspect of your code is a performance bottleneck?
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了