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.

socket listen backlog的更多相关文章

  1. [python]socket.listen(backlog)中的backlog含义

    http://www.nosa.me/2015/09/16/socket-listenbacklog-%E4%B8%AD-backlog-%E6%8C%87%E7%9A%84%E6%98%AF%E4% ...

  2. TCP SOCKET中backlog参数的用途是什么? ---图解

    recv_queue中的包大小,为内核的包大小,而不是ip包大小. 如果发出去的包太大,需要修改write_queue和tx_queue两个参数,tx_queue主要是流量控制. 多进程必须在sock ...

  3. 浅谈tcp socket的backlog参数

    最近看netty源码碰到ChannelOption.SO_BACKLOG参数,通过跟踪代码发现其实是用于设置底层tcp socket的backlog参数,由于不了解这个参数,有必要彻底的理解一下. 底 ...

  4. socket listen参数中的backlog

    服务器监听时,在每次处理一个客户端的连接时是需要一定时间的,这个时间非常的短(也许只有1ms 或者还不到),但这个时间还是存在的.而这个backlog 存在的意义就是:在这段时间里面除了第一个连接请求 ...

  5. socket listen参数中的backlog 的意义!

    服务器监听时,在每次处理一个客户端的连接时是需要一定时间的,这个时间非常的短(也许只有1ms 或者还不到),但这个时间还是存在的.而这个backlog 存在的意义就是:在这段时间里面除了第一个连接请求 ...

  6. TCP之listen&backlog

    1. listen函数: #include <sys/socket.h> int listen(int sockfd, int backlog); ret-成功返回0 失败返回- list ...

  7. socket listen/accept

    listen函数 摘要:listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定 ...

  8. Socket listen 简要分析

    #include <sys/types.h> /* See NOTES */#include <sys/socket.h>int listen(int sockfd, int ...

  9. Socket之listen() receive()

    socket.listen([backlog]) 相比listen方法,它俩就好理解多了,一个是Client用于连接Server的方法,一个是Server用于接收Client的连接申请的方法.  但事 ...

随机推荐

  1. [模板] Treap

    插入x 删除x 查询排名为x的数 查询x的排名 求x的前驱.后继 //Stay foolish,stay hungry,stay young,stay simple #include<iostr ...

  2. win10 专业版 安装tornado 的步骤

    win10 专业版 安装tornado 的步骤: 1.下载tornado源码压缩包 下载网址:https://github.com/tornadoweb/tornado 若是没有github 账号可以 ...

  3. Linux思维导图之网络管理

    查漏补缺,理解概念,及时总结,欢迎拍砖. IP地址和MAC地址: 1.设计形态不同.IP地址是基于网络拓扑设计出来的,可以人为改动:而MAC地址是制造商烧录好的不能改动,网卡决定了MAC地址,是固定的 ...

  4. (十)python3 生成器

    生成器(generator):在 Python 中,不必创建完整的 list,从而节省大量的空间.一边循环一边计算的机制. 创建一个 generator,有很多种方法.第一种方法很简单,只要把一个列表 ...

  5. [Android] java代码无错误,但跳转失败

    今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...

  6. Ubuntu中Python3虚拟环境的搭建

    1.环境准备 首先请自行安装好Python3和pip3(一般Ubuntu是自带Python3的,可以通过sudo apt-get install python3-pip命令来安装pip3) 安装完成后 ...

  7. 集训第六周 数学概念与方法 数论 筛素数 H题

    Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“ ...

  8. 添物不花钱学JavaEE(基础篇) --HTML

    HTML是什么? HTML – Hyper Text Markup Language HTML官方网址 http://www.w3.org/TR/2014/REC-html5-20141028/ 其实 ...

  9. 全文搜索(A)-相关性

    文章:搜索相关性 文章:推荐系统中相似度算法介绍及效果测试 文章:常用的相似度计算方法原理及实现 文章:推荐系统用户相似度计算方法研究

  10. HDU 4465 递推与double的精确性

    题目大意不多说了 这里用dp[i][0] 代表取完第一个盒子后第二个盒子剩 i 个的概率,对应期望就是dp[i][0] *i dp[i][1] 就代表取完第二个盒子后第一个盒子剩 i 个的概率 dp[ ...