http://stackoverflow.com/questions/4253454/question-about-listening-and-backlog-for-sockets

The listen backlog is, as Pieter said, a queue which is used by the operating system to store connections that have been accepted by the TCP stack but not, yet, by your program.

Conceptually, when a client connects it's placed in this queue until your Accept() code removes it and hands it to your program.

As such, the listen backlog is a tuning parameter调优参数 that can be used to help your server handle peaks峰值 in concurrent connection attempts.

Note that this is concerned with peaks in concurrent connection attempts and in no way related to the maximum number of concurrent connections that your server can maintain.

For example, if you have a server which receives 10 new connections per second then it's unlikely that tuning the listen backlog will have any affect even if these connections are long lived

and your server is supporting 10,000 concurrent connections (assuming your server isn't maxing out the CPU serving the existing connections!).

However, if a server occasionally experiences short periods when it is accepting 1000 new connections per second then you can probably prevent some connections from being rejected

by tuning the listen backlog to provide a larger queue and therefore give your server more time to call Accept() for each connection.

As for pros优点 and cons缺点, well the pros are that you can handle peaks in concurrent connection attempts better and the corresponding con is that the operating system needs to allocate more space for the listen backlog queue because it is larger. So it's a performance vs resources trade off权衡.

Personally I make the listen backlog something that can be externally tuned via a config file.

How and when you call listen and accept depends upon the style of sockets code that you're using.

With synchronous code you'd call Listen() once with a value, say 10, for your listen backlog and then loop calling Accept().

The call to listen sets up the end point that your clients can connect to and conceptually creates the listen backlog queue of the size specified.

Calling Accept() removes a pending connection from the listen backlog queue, sets up a socket for application use and passes it to your code as a newly established connection.

If the time taken by your code to call Accept(), handle the new connection, and loop round to call Accept() again is longer than the gap间隙 between concurrent connection attempts

then you'll start to accumulate累加 entries登记 in the listen backlog queue.

With asynchronous sockets it can be a little different, if you're using async accepts you will listen once, as before and then post several (again configurable) async accepts.

As each one of these completes you handle the new connection and post a new async accept.

In this way you have a listen backlog queue and a pending accept 'queue' and so you can accept connections faster

(what's more the async accepts are handled on thread pool threads so you don't have a single tight accept loop).

This is, usually, more scalable and gives you two points to tune to handle more concurrent connection attempts.

最新文章

  1. iOS Salesforce SDK 小知识
  2. 使用c#访问脚本里变量的方法
  3. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
  4. Solaris进程管理
  5. mysql中的unique
  6. c++实现将表达式转换为逆波兰表达式
  7. hexo建立github,gitcafe博客并实时同步的要点
  8. c#语言中的Process进程类型的使用示例
  9. 西北地区打不开github的解决办法~
  10. react中对于key值的理解
  11. eclipse和sublime3打开freemarker(.ftl)文件
  12. Python中的闭包 - Closure
  13. Linux上强制踢出其他正在登录的用户
  14. springmvc实现json交互 -requestBody和responseBody
  15. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏
  16. CSS的六种定位模型
  17. 给js创建的一个input数组绑定click事件
  18. 「PKUSC2018」真实排名
  19. 解决java使用Runtime.exec执行linux复杂命令不成功问题
  20. flow 类型生成工具 flow-typed 简单使用

热门文章

  1. 笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)
  2. jquery data属性 attr vs data
  3. 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)
  4. centos6 文件管理
  5. 在rubymine中集成heroku插件
  6. php7 安装swoole扩展
  7. 嵌入式linux启动信息完全注释
  8. MySQL数据库连接不上的一种可能的解决办法
  9. A Small Definition of Big Data
  10. JavaEE JDBC RowSet行集