一个处理传出连接的限制(.NET) [英] Limit of outgoing connections for one process (.Net)

查看:158
本文介绍了一个处理传出连接的限制(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我下载一个文件在一个线程中,花了0.1秒,但是,当我下载同一个文件中的100个线程 - 花了10秒,每下载。来源$ C ​​$ C:

When I download a file in one thread, it took 0.1 second. But when I download the same file in 100 threads - it took 10 seconds for each downloading. Source code:

private static int _threadsCount;
private static string _url;

private static void Main(string[] args)
{
    _url = ConfigurationManager.AppSettings["Url"];

    int threadsLimit = 1;

    if (0 != args.Length)
        threadsLimit = int.Parse(args[0]);

    for (int i = 0; i < threadsLimit; i++)
    {
        var thread = new Thread(Start);
        thread.Start();
    }

    while (_threadsCount < threadsLimit)
    {
        Thread.Sleep(1000);
    }

    Console.WriteLine("Done");
}

static void Start()
{
    var webClient = new WebClient();

    var stopwatch = new Stopwatch();
    stopwatch.Reset();

    stopwatch.Start();

    for (int i = 1; i <= 10; i++)
    {
        webClient.DownloadData(_url);
    }

    stopwatch.Stop();

    Console.WriteLine(stopwatch.ElapsedMilliseconds);

    Interlocked.Increment(ref _threadsCount);
}

因此​​,如果我跑与100个线程,速度为每档10秒的节目。但是,如果我运行在具有1个线程,速度为每档0.1秒同时的第二个程序的。所以,问题不是出在互联网的速度。

Thus, if I run a program with 100 threads, speed 10 seconds per file. But if I run the second program at the same time with 1 thread, speed 0.1 second per file. So, problem is not in internet speed.

为什么下载速度下降而增加线程数量,但它不影响其它过程(相同的文件)?如何提高速度的一个进程?

Why the download speed goes down with increasing number of threads, but it does not affect the other process (the same file)? How to increase the speed in one process?

推荐答案

1)您可以调整这个参数在配置文件(默认值是2):

1) You can adjust this parameter in your configuration file (default value is 2) :

<system.net>
    <connectionManagement>
        <add address="*" maxconnection="2" />
    </connectionManagement>
</system.net>

2)为了迫使你的程序中创建多个插座,下载不同的应用领域。

2) In order to force your program to create several sockets, download from different application domains.

这篇关于一个处理传出连接的限制(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆