下午好。我为我的英语本身从乌克兰道歉并且说得不好))我有以下问题,我的程序在不同的网址上提出请求,然后从答案中解析一些信息。网址数量超过数百万。为了快速处理,我使用了很多线程,有时大约 500-700 个线程。在某些机器上程序运行良好,但有些机器上会出现错误。像这样的错误:System.Net.Sockets.SocketException (0x80004005): The remote host forcibly broke the existing connection.
我的代码:
void _thread()
{
while(true)
{
string request =
"POST http://" + hostf + "/ HTTP/1.1\r\n" +
"Host: " + host +
"\r\nConnection: Close\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n" +
"Content-Length: " + ByteArr.Length +
"\r\nContent-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n\r\n" +
parametres;
Byte[] bytesSent = Encoding.GetEncoding("UTF-8").GetBytes(request);
Byte[] bytesReceived = new Byte[256];
Socket s = null;
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry(host);
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 80);
Socket tempSocket =new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
if (s == null)
continue;
s.Send(bytesSent, bytesSent.Length, 0);
int bytes = 0;
string page = "";
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.GetEncoding("UTF-8").GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
s.Shutdown(SocketShutdown.Both);
s.Close();
//here some work whith page content
}
}
如您所见,每个线程在每次迭代中创建套接字、发送请求、然后获取答案并关闭套接字等等。每个线程打开自己的套接字并使用不同的 url,但在某些机器上,当线程数超过某个数字时,错误开始并且所有套接字都无法正常工作。有人可以帮我一些建议,为什么会这样?有些机器对连接有某种限制或什么?谢谢大家。