我使用VSTS 2008 + C#+。NET 3.5的开发控制台应用程序,我请求发送到另一台服务器(Windows Server 2008上的IIS 7.0)。 我发现,当请求线程#是大(例如2000线),客户端会收到错误“无法连接到远程服务器失败”响应调用时=(HttpWebResponse)request.GetResponse()我的困惑是 - 我有超时设置为较大的值,但我得到了一分钟之内这样的失败消息。 我想,即使连接确实比IIS可以起到较大,客户不应该得到这样的这么快就失败消息,超时后应该得到这样的消息。 任何意见? 任何想法有什么不好? 任何想法,使通过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 + "\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;
}
}