title: How do I know how many connections are opened by my webclient?
body: <p>I have a simple webclient which simply sends POST messages to server and get the responses. I set <code>KeepAlive=true</code> by overriding GetWebRequest(...)' of System.Net.WebClient)' to use persistent connections.</p>
  
  <p>And the server to which my client talks to have a limit on the number of connections I can open (to avoid DoS attacks, I guess!). My client simply executes bunch of queries against this server in parallel - so to limit the number of requests I make against the server I used a semaphore (please see code snippet) - i.e. to ensure I didn't cross the connection limit at any given time.</p>
  
  <p>However on a specific customer's environment, the server is actively denying requests by saying 'the connection limit has been reached'.</p>
  
  <p><strong>My questions</strong>:</p>
  
  <ol>
  <li><p>How do I know at any given instant, how many connections my client is opening against the server? I am using Fiddler - but not too sure whether this info is available?</p></li>
  <li><p>Since I am limiting the number of requests I make using semaphore (in this example 75), assuming there are no connections opened by other clients or apps from the customer's box to server, is there a possibility the .net opening more than '75' persistent connections since I set the 'keep alive' to true. My guess is since at most there are only 75 requests, the connections shouldn't be more than 75, and all the further requests simply reuse the existing connections. But, since I can't see - I can't confirm I guess.</p></li>
  </ol>
  
  <p><strong>Limiting number of concurrent requests by using semaphore:</strong></p>
  
  <pre><code> 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();
              }
  </code></pre>
  
  <p><strong>My Web Client (setting keep alive to true by overriding GetWebRequest):</strong></p>
  
  <pre><code> 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;
  
          }        
  
      }
  </code></pre>
  
  <h2><strong>Related Links</strong></h2>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/866350/how-can-i-programmatically-remove-the-2-connection-limit-in-webclient?rq=1">How can I programmatically remove the 2 connection limit in WebClient</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/1361771/max-number-of-concurrent-httpwebrequests">Max number of concurrent HttpWebRequests</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/2960056/trying-to-run-multiple-http-requests-in-parallel-but-being-limited-by-windows">Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)</a> </p></li>
  <li><p><a href="https://stackoverflow.com/questions/9843369/how-to-set-or-increase-number-of-persistent-http-connections-in-c-net?rq=1">How to Set or Increase Number of Persistent HTTP Connections in C#/.NET</a></p></li>
  </ol>