zoukankan      html  css  js  c++  java
  • 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?

  • 相关阅读:
    Redis源码分析(二十一)--- anet网络通信的封装
    leetcode 总结part1
    leetcode String to Integer (atoi)
    leetcode 165. Compare Version Numbers
    leetcode 189. Rotate Array
    leetcode 168. Excel Sheet Column Title
    leetcode 155. Min Stack
    leetcode 228. Summary Ranges
    leetcode 204. Count Primes
    leetcode 6. ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/chucklu/p/5279336.html
Copyright © 2011-2022 走看看