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

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

问题描述

我正在使用 VSTS 2008 + C# + .Net 3.5 开发控制台应用程序,并将请求发送到另一台服务器(Windows Server 2008 上的 IIS 7.0).我发现当请求线程数很大(例如 2000 个线程)时,客户端在调用 response = (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?

这是我的代码,

   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 + "
" + 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;
        }
    }

推荐答案

Kev 已经回答了你的问题,我只是想补充一点,创建这么多线程并不是很好的设计解决方案(只是上下文切换开销是一个很大的减号)加上它不会很好地扩展.

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.

您可以在此处阅读有关异步客户端/服务器架构(IOCP - 输入/输出完成端口)及其优势的更多信息.你可以从这里开始:

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

MSDN - 使用异步服务器套接字

MSDN - 异步服务器套接字示例

CodeProject - 多线程 .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.

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

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