繁体   English   中英

我如何知道我的网络客户端打开了多少个连接?

[英]How do I know how many connections are opened by my webclient?

我有一个简单的 webclient,它只是将 POST 消息发送到服务器并获得响应。 我通过覆盖 System.Net.WebClient)' 的 GetWebRequest(...)' 来设置KeepAlive=true以使用持久连接。

我的客户端与之通信的服务器对我可以打开的连接数有限制(我猜是为了避免 DoS 攻击!)。 我的客户端只是并行地对该服务器执行一堆查询 - 所以为了限制我对服务器发出的请求数量,我使用了信号量(请参阅代码片段) - 即确保我没有超过任何给定的连接限制时间。

但是,在特定客户的环境中,服务器通过说“已达到连接限制”来主动拒绝请求。

我的问题

  1. 我如何知道在任何给定时刻,我的客户端对服务器打开了多少个连接? 我正在使用 Fiddler - 但不太确定此信息是否可用?

  2. 由于我限制使用信号量发出的请求数量(在本例中为 75),假设其他客户端或应用程序从客户的盒子到服务器没有打开连接,那么 .net 是否有可能打开超过“75”持久连接,因为我将“保持活动”设置为 true。 我的猜测是因为最多只有 75 个请求,连接不应超过 75 个,所有进一步的请求只是重用现有的连接。 但是,因为我看不到 - 我猜我无法确认。

使用信号量限制并发请求数:

 Semaphore semaphore = new Semaphore(initialCount:75, maximumCount:75);
            try
            {
                //at any given time do not send more than 75 requests to server
                semaphore.WaitOne();
                using (var myWebClient = new MyWebClient(timeoutSeconds: 300, useragent: "dreamer"))
                {
                    byte[] responseBytes = myWebClient.UploadData("http://myserverurl",
                        UTF8Encoding.UTF8.GetBytes("inputMessageRequest"));
                }
            }
            finally
            {
                semaphore.Release();
            }

我的 Web 客户端(通过覆盖 GetWebRequest 将 keep alive 设置为 true):

 class MyWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
            request.UserAgent = this.UserAgent;
            request.KeepAlive = true;
            request.Timeout = this.TimeoutSeconds;
            request.PreAuthenticate = true;
            return request;
        }
        public int TimeoutSeconds { get; set; }
        public string UserAgent { get; set; }        
        public MyWebClient(int timeoutSeconds, string useragent)
        {
            this.TimeoutSeconds = timeoutSeconds;
            this.UserAgent = useragent;

        }        

    }

相关链接

  1. 如何以编程方式删除 WebClient 中的 2 个连接限制

  2. 最大并发 HttpWebRequests 数

  3. 尝试并行运行多个 HTTP 请求,但受 Windows(注册表)限制

  4. 如何在 C#/.NET 中设置或增加持久性 HTTP 连接的数量

也许这可能会有所帮助...

http://www.codeproject.com/Articles/23306/ASP-NET-Performance-and-Scalability-Secrets

查看标题为“防止拒绝服务 (DOS) 攻击”的部分

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM