0

I'm trying to open about 25connections to a host at the same time.
My OS is windows 10.
For testing, I bring up a simple website on my local IIS and I response a simple data to the user with a delay of 2 seconds using Thread.Sleep(2000).

Now using this code on the client:

const int len = 25;
for (int i = 0; i < len; i++)
{
    new Thread(new ParameterizedThreadStart((idx) =>
    {
        // start downloading data.
        var res = new WebClient().DownloadString("http://192.168.1.101:8090/");

        // log index and time when done.
        Console.WriteLine($"{Convert.ToInt32(idx).ToString("00")} done at:{ DateTime.Now.ToString("HH:mm:ss:ffff") }");
    })).Start(i);
}

I got the following result:

Thread 01 done at 40:8476 ms
Thread 00 done at 40:8476 ms
Thread 03 done at 40:8496 ms
Thread 04 done at 40:8496 ms
Thread 02 done at 40:8506 ms
Thread 05 done at 40:8506 ms
Thread 07 done at 40:8516 ms
Thread 06 done at 40:8516 ms
Thread 08 done at 40:8536 ms
Thread 09 done at 40:8545 ms
Thread 11 done at 42:8510 ms
Thread 10 done at 42:8510 ms
Thread 12 done at 42:8560 ms
Thread 14 done at 42:8560 ms
Thread 15 done at 42:8570 ms
Thread 13 done at 42:8580 ms
Thread 16 done at 42:8590 ms
Thread 17 done at 42:8590 ms
Thread 18 done at 42:8610 ms
Thread 19 done at 42:8610 ms
Thread 21 done at 44:8565 ms
Thread 20 done at 44:8565 ms
Thread 23 done at 44:8634 ms
Thread 24 done at 44:8654 ms
Thread 22 done at 44:8654 ms

The above result tells us that:
1- Thread 0 to 9 got the data at the same time.(second 40)
2- Thread 10 to 19 got the data at the same time 2 seconds later after previous step.(second 42)
3- Thread 20 to 24 got the data at the same time. 2 seconds later after previous step.(second 44)

Now my question is WHO limited me and why it only opens 10 HTTP connections at the same time and how can I set it to unlimited.
If there is any other platform or programming language it will be welcomed.

Mohi
  • 1,776
  • 1
  • 26
  • 39
  • 1
    By the way, it's inefficent to use a thread for I/O, especially 1 thread per connection. Look into _I/O Completion Ports_ or just use `async/await` –  Oct 09 '19 at 14:14
  • 1
    Run it on Windows Server and you should have a different outcome. – mxmissile Oct 09 '19 at 14:16
  • Must be something about your setup, mine ran differently, probably then number of cores +n as actually you're running 5 at a time – BugFinder Oct 09 '19 at 14:18
  • @BugFinder on which OS? – CodeCaster Oct 09 '19 at 14:27
  • Win10, but if you look at the timing, yours started as a batch of 5 and then things were slotted in as they fitted. What happens if you change them to tasks not threads – BugFinder Oct 09 '19 at 14:32
  • @BugFinder I'm not the OP. I don't think this has anything to do with threading. Do you perhaps have Windows 10 Pro or Enterprise? Perhaps they have different limits. – CodeCaster Oct 09 '19 at 14:34
  • Im on Pro, but I remember reading something about workstation limiting threads – BugFinder Oct 09 '19 at 14:44
  • I hosted the app on a windows server 2016 with IIS-10 and the same result happened. running code in browser using `fetch()` function of the `javascript` also results the same and not opens 25 connection and gets the result after 2 seconds. @MickyD @CodeCaster @BugFinder @mxmissile – Mohi Oct 09 '19 at 15:41

1 Answers1

7

Who? Your OS and web server manufacturer, being Microsoft.

Why? Because Windows 10 is a client OS, and Microsoft doesn't want you to host any serious web applications on that.

See for example:

And from While using signalr, will there be any connection limits on IIS, linking to the SignalR documentation:

When SignalR is hosted in IIS, the following versions are supported. Note that if a client operating system is used, such as for development (Windows 8 or Windows 7), full versions of IIS or Cassini should not be used, since there will be a limit of 10 simultaneous connections imposed, which will be reached very quickly since connections are transient, frequently re-established, and are not disposed immediately upon no longer being used. IIS Express should be used on client operating systems.

There are other posts, like Does windows 10 connection limit apply to self-hosted applications?, mentioning 20 connections, but that's 20 devices (probably recognized by remote IP address) connecting to selected Windows service (SMB, IIS, ...).

The IIS limit is 10, and has been for many years, I think since Windows 7.

So the first half of the answer to this question is:

  • If you need more than 10 simultaneous incoming HTTP connections while developing on Windows 10, use IIS Express or any other web server than IIS or Cassini.

But there's a second half of this answer. If you need more than 10 (or 2, or ..., depending on the environment) simultaneous outgoing HTTP connections, there's ServicePointManager who manages this. See:

So change the limit before executing the requests:

System.Net.ServicePointManager.DefaultConnectionLimit = 25;
CodeCaster
  • 147,647
  • 23
  • 218
  • 272