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?
Here is my 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 =
HttpWebRequest request =
HttpWebResponse response =
StreamReader reader =
request = (HttpWebRequest)WebRequest.Create(TargetURL);
request.Timeout = Timeout * 1000;
request.Proxy =
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);
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 & ClientC i++)
workers[i] = new Thread((new ThreadStart(PerformanceWorker)));
for (int i = 0; i & ClientC i++)
workers[i].Start();
for (int i = 0; i & ClientC i++)
workers[i].Join();
thanks in advance,
解决方案 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.
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:
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.L cnt++)
events[cnt] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(obj =& PerformanceWorker());
WaitHandle.WaitAll(events);
Not tested, may need some adjustment.
本文地址: &
我使用VSTS 2008 + C#+的.Net 3.5开发一个控制台应用程序,我将请求发送到另一台服务器(IIS 7.0的Windows Server 2008上)。我发现当请求的线程#是大(例如2000线),客户端会收到错误“无法连接到远程服务器失败”响应调用时=(HttpWebResponse)request.GetResponse()我的困惑是 - 我有设置超时时间为一个较大的值,但我得到了一分钟之内这样的失败消息。我想,即使连接确实比IIS可能成为更大的,客户不应该得到这样的这么快就失败消息,超时时间后,应该得到这样的消息。有任何意见?任何想法有什么不好?任何想法,使所服务的IIS 7.0的更多数量的并发连接?
下面是我的code,
私有静态诠释ClientCount = 2000;
私人静态字符串TargetURL中=“HTTP://labtest/abc.wmv”;
私有静态诠释超时= 3600;
静态无效PerformanceWorker()
流式传输的数据流= NULL;
HttpWebRequest的要求= NULL;
HttpWebResponse响应=无效;
StreamReader的读卡器= NULL;
请求=(HttpWebRequest的)WebRequest.Create(TargetURL的);
request.Timeout =超时* 1000;
request.Proxy = NULL;
响应=(HttpWebResponse)request.GetResponse();
数据流= response.GetResponseStream();
读者=新的StreamReader(数据流);
// 1M的一次
的char [] C =新的char [1000 * 10]。
而(reader.Read(C,0,c.Length)大于0)
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
赶上(例外前)
Console.WriteLine(ex.Message +“\ N”+ ex.StackTrace);
如果(NULL!=读卡器)
reader.Close();
如果(NULL!=数据流)
dataStream.Close();
如果(NULL!=响应)
response.Close();
静态无效的主要(字串[] args)
主题[]工作人员=新主题[ClientCount]
的for(int i = 0; I< ClientC我++)
工人[我] =新主题((新的ThreadStart(PerformanceWorker)));
的for(int i = 0; I< ClientC我++)
工人[I]。开始();
的for(int i = 0; I< ClientC我++)
工人[I]。加入();
在此先感谢,
解决方案 千电子伏回答你的问题已经,我只想补充一点,创造如此多线程是不是真的好设计方案(只是上下文切换开销是一个很大的负)加它不会缩放良好。
简单的回答是:使用异步操作来阅读,而不是创造一堆线程的数据。或者至少使用线程池(和更低的工作线程数)。请记住,一位消息人士透露更多的连接就只能加快速度到一定程度。尝试基准它,你会看到,可能3-5的连接会工作得更快,在2000年,你现在正在使用。
在这里 - (输入/输出完成端口IOCP),其优点您可以阅读更多有关异步客户端/服务器体系结构。你可以从这里开始:
扫一扫关注官方微信&与服务器断开连接,请刷新网页重试&玩不了到底什么回事啊
全部***(共1个回答)
问题啊,你应该打开你的宽带连接的设置啊,具体操作是,用鼠标右键点击“宽带连接”图标,打开属性,你自动去设置就可以了,设置好了就成功了。也可以让开机就连接,方法如下,打开宽带连接方法如下,打开宽带连接图标,将为下面用户名保存账号和密码框框勾掉就OK,都需要重新启动电脑,才能生效啊,希望能解决你的问题。
大家还关注
确定举报此问题
举报原因(必选):
广告或垃圾信息
激进时政或意识形态话题
不雅词句或人身攻击
侵犯他人隐私
其它违法和不良信息
报告,这不是个问题
报告原因(必选):
这不是个问题
这个问题分类似乎错了
这个不是我熟悉的地区