什么是一个很好的缓冲大小socket编程? [英] What is a good buffer size for socket programming?

查看:253
本文介绍了什么是一个很好的缓冲大小socket编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是.NET和插座。该服务器使用的Socket.Sender(字节[])方法,因此它只是将整个有效载荷。在另一边,我们的客户消费数据。 Socket.Receive(缓冲[])。在微软(和其他人)的所有例子,他们似乎坚持使用8192的缓冲区大小,我们都用这个尺寸,但下降到超过此缓冲区大小的客户飘飞我们正在发送数据。

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.

您无法确定有多少数据服务器发送 - 这是一个的的数据,而你只是阅读大块的时间。你可以阅读的部分的的是服务器的一个发送呼叫发送,或者你可以读出两个数据发送一个呼叫接收呼叫。 8K是一个合理的缓冲区大小 - 没有这么大,你会浪费大量的内存,并没有那么小,你将不得不使用的浪费接收呼叫负载。 4K或16K将很可能被罚款太...我个人不会去启动16K以上的网络缓冲区 - 我怀疑你会很少填补他们

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.

您可以尝试,试图用一个非常大的缓冲区和日志了多少字节收到各种通话 - 这会给你多少一般是一些概念的可用的 - 但它不会真正显示利用较小的缓冲器的作用。你有过使用8K的缓冲区什么顾虑?如果它的性能,你有什么证据证明你的code这方面是一个性能瓶颈?

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?

这篇关于什么是一个很好的缓冲大小socket编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆