【问题标题】:System.Net.HttpListener on Windows 7 Ultimate x64 Limited to 1k Concurrent ConnectionsWindows 7 Ultimate x64 上的 System.Net.HttpListener 限制为 1k 并发连接
【发布时间】:2026-01-25 06:50:01
【问题描述】:

我一直在尝试对我的工作站上的 HTTP.sys / HttpListener 进行一些测试,似乎有一些限制可以防止超过 1000 个并发连接。有没有人有更多关于这方面的信息? (因为 1k 似乎有点清洁是巧合)。

我试图找到任何线程/配置/注册表设置,但都为空。

提前致谢。
GJ


看起来我有点过分了。

我似乎错过了使用 http.sys / HttpListener BeginGetContext 对并发连接不是很好,因为新的 BeginGetContext 只会在先前请求的响应流关闭后触发。

所以有 1000 个请求的积压,在这种情况下,积压已满。

无论如何 - 如果有人有任何 cmets(或可能的更正),请随时扩展。

谢谢
GJ

【问题讨论】:

    标签: .net sockets httplistener


    【解决方案1】:

    这是一种使用 HttpListener 支持多个并发请求的简单方法。

            for (int i = 0; i < 50; i++) {
                _listener.BeginGetContext(GetContextCallback, null);
            }
    

    这将使您现在可以接收 50 个并发请求。必须承认,我从未尝试过创建 1000 个!

    【讨论】:

    • 谢谢达雷尔 - 这确实有效,但不幸的是不适合我面临的问题。
    【解决方案2】:

    我这样做的方法是让一个线程使用阻塞 GetContext() 方法侦听 HttpListener,但一旦它收到请求,就会通过使用异步调用将其传递给另一个线程IAsyncResult 模式,这似乎工作正常。

        private void Run()
        {
            while (true)
            {
                if (this._disposed || this._shouldTerminate) return;
    
                if (this._listener.IsListening)
                {
                    try
                    {
                        HttpListenerContext context = this._listener.GetContext();
    
                        //Hand it off to be processed asynchronously
                        this._delegate.BeginInvoke(context, new AsyncCallback(this.EndRequest), null);
                    }
                    catch (Exception ex)
                    {
                        this.LogErrors(ex);
                    }
                }
            }
        }
    
        private delegate HttpServerContext HandleRequestDelegate(HttpListenerContext context);
    
        private HttpServerContext HandleRequest(HttpListenerContext context)
        {
            IHttpListenerHandler handler;
            HttpServerContext serverContext = new HttpServerContext(this, context);
            try
            {
                bool skipHandling = this.ApplyPreRequestModules(serverContext);
                if (!skipHandling)
                {
                    handler = this._handlers.GetHandler(serverContext);
                    handler.ProcessRequest(serverContext);
                }
            }
            catch (NoHandlerException noHandlerEx)
            {
                this.LogErrors(noHandlerEx);
                context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
            }
            catch (HttpServerException serverEx)
            {
                this.LogErrors(serverEx);
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
    
            return serverContext;
        }
    
        private void EndRequest(IAsyncResult result)
        {
            try
            {
                HttpServerContext context = this._delegate.EndInvoke(result);
                this.ApplyPreResponseModules(context);
                context.Response.Close();
            }
            catch (Exception ex)
            {
                this.LogErrors(ex);
            }
        }
    

    【讨论】:

    • 干杯 Rob - 这让我越界了!