关于听力和积压插座问题 [英] Question about listening and backlog for sockets

查看:110
本文介绍了关于听力和积压插座问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中,需要处理传入的连接的应用程序,我从来没有做过服务器端编程。这使我对这些下列问题:

I am writing an application in C# that needs to handle incoming connections and I've never done server side programming before. This leads me to these following questions:


  • 赞成和积压高/低积压的利弊?为什么我们不设置积压数量庞大?

  • 如果我叫Socket.Listen(10),10后接受()■我必须打电话听()再次?还是我打电话听()后,每接受()?

  • 如果设置我的积压为0,假设两个人想在同一时间连接到我的服务器,会是什么发生? (我在一个循环中调用Socket.Select和检查监听套接字的可读性,之后我处理的第一个连接将第二个连接成功后,下一个迭代,如果我叫听()再次?)

先谢谢了。

推荐答案

监听积压, 如彼得所述的,它被用来通过一个队列的操作系统,以存储已经由TCP堆栈接受,但不连接,但是,由程序。从概念上讲,当客户端连接它被放置在这个队列中,直到你的接受()代码删除它,它的手到你的程序。

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.

因此,听积压是可以用来帮助服务器处理高峰并发连接尝试一个调整参数。请注意,这是关注的并发连接尝试,并没有丝毫关系到您的服务器可以保持并发连接的最大数目的峰值。例如,如果你有每秒接收10个新连接的服务器,然后它不太可能调整听积压将有即使这些连接都长期生活的影响你的服务器支持10,000个并发连接(假设你的服务器是不是杏出CPU服务现有连接!)。但是,如果一台服务器会偶尔出现短时间当它被接受每秒1000个新的连接,那么你也许可以防止通过调整侦听backlog提供了较大的队列,所以将你的服务器有更多的时间来调用<$ C被拒绝一些连接$ C>接受()为每个连接。

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.

至于利弊,以及优点是可以处理的并发连接的峰值尝试更好和相应con是,操作系统需要分配更多的空间用于听积压队列,因为它是大的。所以这是一个性能VS资源权衡。

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.

如何以及何时调用倾听和接受取决于您所使用的插座的代码风格。使用同步代码,你会打电话收听()一旦与一个值,比如10,你听积压,然后循环调用接受()。通话监听设置了终点,你的客户端可以连接到和概念创建指定大小的监听backlog队列。调用接受()将删除侦听backlog队列中挂起的连接,建立应用程序使用一个套接字并把它传递给你的代码作为一个新建立的连接。如果你的代码所花费的时间打电话给接受(),处理新的连接,循环轮叫接受()再比并发连接尝试之间的差距越长,那么你就会开始堆积在听backlog队列的条目。

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.

通过异步插座也可以是一个有点不同,如果你使用异步接受,你会听一次,和以前一样,然后几个后(同样配置)异步接受。由于这些完成了对每一个你处理新的连接,并发布一个新的异步接受。通过这种方式,你有听backlog队列和挂起接受排队,所以你可以更快地接受连接(更重要的是异步接受的线程池中的线程的处理方式,所以你不要有一个紧密的接受循环)。这是,通常情况下,更具可扩展性,并为您的两点调整以处理更多的并发连接尝试。

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.

这篇关于关于听力和积压插座问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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