Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何知道我的webclient打开了多少个连接?_C#_Asp.net_.net_Web Services - Fatal编程技术网

C# 我如何知道我的webclient打开了多少个连接?

C# 我如何知道我的webclient打开了多少个连接?,c#,asp.net,.net,web-services,C#,Asp.net,.net,Web Services,我有一个简单的webclient,它只向服务器发送POST消息并获取响应。我通过覆盖System.Net.WebClient的GetWebRequest(…)”来设置KeepAlive=true,以使用持久连接 我的客户机与之对话的服务器对我可以打开的连接数量有限制(我想是为了避免DoS攻击!)。我的客户机只是并行地对该服务器执行一系列查询——因此,为了限制我对服务器发出的请求数量,我使用了一个信号量(请参阅代码片段)——也就是说,为了确保我在任何给定时间都没有超过连接限制 但是,在特定客户的环

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

我的客户机与之对话的服务器对我可以打开的连接数量有限制(我想是为了避免DoS攻击!)。我的客户机只是并行地对该服务器执行一系列查询——因此,为了限制我对服务器发出的请求数量,我使用了一个信号量(请参阅代码片段)——也就是说,为了确保我在任何给定时间都没有超过连接限制

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

我的问题

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

  • 由于我限制了使用信号量发出的请求数量(在本例中为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();
                }
    
     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;
    
            }        
    
        }
    
    我的Web客户端(通过覆盖GetWebRequest将keep alive设置为true):

     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();
                }
    
     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;
    
            }        
    
        }
    
    相关链接

  • 也许这会有帮助


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

    ——基本上,他正在缓存来自特定IP的请求数,并在一定限制后拒绝连接。我的外部团队的服务器也这样做(类似)(他们还提到了客户端可以opn的连接数),但正如我的客户端所示,我使用持久http连接,并使用信号量来确保我不会发出超过75个请求。服务器仍在删除连接-这就是为什么我想知道到底有多少个持久连接(请注意,当我在客户机上运行测试时,我确保没有其他应用程序发出请求。当你说删除连接时,你到底看到了什么?“503服务不可用”?如果是,请检查IIS中的应用程序池设置,而不是来自IIS-在服务器跟踪日志中,它明确表示已达到连接限制,因此拒绝进一步的请求-但根据servicepoint.connectionlimit,默认情况下只有2个连接。现在甚至不确定,即使我执行了请求,会发生什么并行时,最多应该有“2”个连接。是的,这意味着服务器端可能存在错误-我只是试图从客户端的角度理解,以准确地计算我的并行请求打开了多少个连接。我知道了-持久化HTTP连接和服务器在任何给定时刻可以服务的请求数t可以不同。默认情况下,windows仅使用“2”连接来序列化输入/输出数据包。