0

I have a simple webclient which simply sends POST messages to server and get the responses. I set KeepAlive=true by overriding GetWebRequest(...)' of System.Net.WebClient)' to use persistent connections.

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.

However on a specific customer's environment, the server is actively denying requests by saying 'the connection limit has been reached'.

My questions:

  1. 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?

  2. 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.

Limiting number of concurrent requests by using semaphore:

 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();
            }

My Web Client (setting keep alive to true by overriding GetWebRequest):

 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;

        }        

    }

Related Links

  1. How can I programmatically remove the 2 connection limit in WebClient

  2. Max number of concurrent HttpWebRequests

  3. Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

  4. How to Set or Increase Number of Persistent HTTP Connections in C#/.NET

halfer
  • 19,824
  • 17
  • 99
  • 186
Dreamer
  • 3,371
  • 2
  • 34
  • 50

1 Answers1

0

Maybe this might help...

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

Have a look at the section titled "Prevent Denial of Service (DOS) Attack"

Mick
  • 6,527
  • 4
  • 52
  • 67
  • That is from the server side - basically he is caching number of requests from a particular IP, and denying connections after some limit. And my external team's server also does that (similar) (they also mentioned the number of connections client can opn), but as shown from my client I am using persistent http connections and using a semaphore to make sure I wont make more than 75 requests. Server is still dropping the conenctions - that's why I want to know exactly how many persisted connections are there (note that, I made sure no other app is making requests when I run tests on customer env – Dreamer Mar 11 '14 at 03:11
  • When you say dropping connections, what exactly are you seeing? "503 Service Unavailable" ? If so check the application pool settings in IIS http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/21a8be6f-69ef-4701-9d2d-0119c8b81289.mspx?mfr=true – Mick Mar 11 '14 at 04:04
  • Not from IIS - in the server trace logs it clearly says connection limit has been reached, so its denying further requests - but according to servicepoint.connectionlimit, by default there should only be 2 connections. Now am not even sure, whats happening as even though I execute the requests in parallel there should at most '2' connections. Yes, it means there is a possibility that there is bug at server side - I am just trying to understand from client perspective to figure out exactly how many connections my parallel requests open. – Dreamer Mar 11 '14 at 04:09
  • i got it - persisted HTTP connections and number of requests a server can serve at any given any instant can be different. by default windows simply uses '2' connections to serialize input/output packets. – Dreamer Mar 11 '14 at 17:59