"无法连接到远程服务器失败"在HttpWebRequest的 [英] "Unable to connect to remote server fail" in HttpWebRequest

查看:1090
本文介绍了"无法连接到远程服务器失败"在HttpWebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VSTS 2008 + C#+的.Net 3.5开发一个控制台应用程序,我将请求发送到另一台服务器(IIS 7.0的Windows Server 2008上)。我发现当请求的线程#是大(例如2000线),客户端会收到错误无法连接到远程服务器失败响应调用时=(HttpWebResponse)request.GetResponse()我的困惑是 - 我有设置超时时间为一个较大的值,但我得到了一分钟之内这样的失败消息。我想,即使连接确实比IIS可能成为更大的,客户不应该得到这样的这么快就失败消息,超时时间后,应该得到这样的消息。有任何意见?任何想法有什么不好?任何想法,使所服务的IIS 7.0的更多数量的并发连接?

I am using VSTS 2008 + C# + .Net 3.5 to develop a console application and I send request to another server (IIS 7.0 on Windows Server 2008). I find when the # of request threads are big (e.g. 2000 threads), the client will receive error "Unable to connect to remote server fail" when invoking response = (HttpWebResponse)request.GetResponse().My confusion is -- I have set timeout to be a large value, but I got such fail message within a minute. I think even if the connection are really larger than what IIS could serve, client should not get such fail message so soon, it should get such message after timeout period. Any comments? Any ideas what is wrong? Any ideas to make more number of concurrent connection being served by IIS 7.0?

下面是我的code,

   class Program
    {
        private static int ClientCount = 2000;
        private static string TargetURL = "http://labtest/abc.wmv";
        private static int Timeout = 3600;

        static void PerformanceWorker()
        {
            Stream dataStream = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(TargetURL);
                request.Timeout = Timeout * 1000;
                request.Proxy = null;
                response = (HttpWebResponse)request.GetResponse();
                dataStream = response.GetResponseStream();
                reader = new StreamReader(dataStream);

                // 1 M at one time
                char[] c = new char[1000 * 10];

                while (reader.Read(c, 0, c.Length) > 0)
                {
                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                if (null != reader)
                {
                    reader.Close();
                }
                if (null != dataStream)
                {
                    dataStream.Close();
                }
                if (null != response)
                {
                    response.Close();
                }
            }
        }

        static void Main(string[] args)
        {
            Thread[] workers = new Thread[ClientCount];
            for (int i = 0; i < ClientCount; i++)
            {
                workers[i] = new Thread((new ThreadStart(PerformanceWorker)));
            }

            for (int i = 0; i < ClientCount; i++)
            {
                workers[i].Start();
            }

            for (int i = 0; i < ClientCount; i++)
            {
                workers[i].Join();
            }           

            return;
        }
    }

在此先感谢, 乔治

thanks in advance, George

推荐答案

千电子伏回答你的问题已经,我只想补充一点,创造如此多线程是不是真的好设计方案(只是上下文切换开销是一个很大的负)加它不会缩放良好。

Kev answered you question already, I just want to add that creating so many threads is not really good design solution (just context switching overhead is a big minus) plus it won't scale good.

简单的回答是:使用异步操作来阅读,而不是创造一堆线程的数据。或者至少使用线程池(和更低的工作线程数)。请记住,一位消息人士透露更多的连接就只能加快速度到一定程度。尝试基准它,你会看到,可能3-5的连接会工作得更快,在2000年,你现在正在使用。

The quick answer would be: use asynchronous operations to read data instead of creating a bunch of threads. Or at least use thread pool (and lower worker thread count). Remember that more connections to one source will only speed things up till some degree. Try benchmarking it and you will see that probably 3-5 connections will work faster that 2000 you are using now.

您可以阅读更多有关异步客户端/服务器体系结构。你可以从这里开始:

You can read more about asynchronous client/server architecture (IOCP - input/output completion ports) and its advantages here. You can start from here:

- 使用异步服务器套接字

- 异步服务器套接字示例

$ C $的CProject - 多线程.NET TCP服务器实​​例

所有这些例子使用较低的级别的TCP对象,但它可以应用到的WebRequest / WebResponse的为好。

All of these examples uses lower level TCP object, but it can be applied to WebRequest/WebResponse as well.

更新

要试试线程池的版本,你可以做这样的事情:

To try thread pool version, you can do something like this:

ManualResetEvent[] events = new ManualResetEvent[ClientCount];
for (uint cnt  = 0; cnt < events.Length; cnt++)
{
  events[cnt] = new ManualResetEvent(false);
  ThreadPool.QueueUserWorkItem(obj => PerformanceWorker());
}

WaitHandle.WaitAll(events);

没有测试,可能需要一些调整。

Not tested, may need some adjustment.

这篇关于&QUOT;无法连接到远程服务器失败&QUOT;在HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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