Z
zhfhe
Unregistered / Unconfirmed
GUEST, unregistred user!
在一个多线程的程序中,线程中的函数ScoketHttp执行大约500次之后,发现 recv()会死锁,不会再执行下一句,有何办法解决?
uses WinSock;
...
Function ScoketHttp(Url:String):String;
var
len,s:integer;
name:sockaddr_in;
heHostEnt;
buf:array[0..4095]of char;
str,data:String;
wsd:WSADATA;
begin
WSAStartup($101,wsd);
s:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
he:=gethostbyname('www.ttdown.com');//网站地址
if he=nil then
Exit;
FillChar(name,sizeof(name),0);
name.sin_family:=AF_INET;
name.sin_port:=htons(80);//网站端口
name.sin_addr.S_addr:=PDWORD(PDWORD(he.h_addr)^)^;
if connect(s,name,sizeof(name))<>0 then
begin
closesocket(s);
WSACleanup();
Exit;
end;
str:='HEAD /SoftDown.asp?ID=34589 HTTP/1.1'#13#10'Host:www.ttdown.com'#13#10'Accept: */*'#13#10'Connection: Close'#13#10#13#10;
send(s,PChar(str)^,Length(str),0);
data:='';
while true do
begin
len:=recv(s,buf,sizeof(buf),0);//调试中发现调用函数500多次后此句死锁,不会再执行下一句
if len<1 then
break;
SetString(str,buf,len);
data:=data+str;
end;
closesocket(s);
WSACleanup();
end;
uses WinSock;
...
Function ScoketHttp(Url:String):String;
var
len,s:integer;
name:sockaddr_in;
heHostEnt;
buf:array[0..4095]of char;
str,data:String;
wsd:WSADATA;
begin
WSAStartup($101,wsd);
s:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
he:=gethostbyname('www.ttdown.com');//网站地址
if he=nil then
Exit;
FillChar(name,sizeof(name),0);
name.sin_family:=AF_INET;
name.sin_port:=htons(80);//网站端口
name.sin_addr.S_addr:=PDWORD(PDWORD(he.h_addr)^)^;
if connect(s,name,sizeof(name))<>0 then
begin
closesocket(s);
WSACleanup();
Exit;
end;
str:='HEAD /SoftDown.asp?ID=34589 HTTP/1.1'#13#10'Host:www.ttdown.com'#13#10'Accept: */*'#13#10'Connection: Close'#13#10#13#10;
send(s,PChar(str)^,Length(str),0);
data:='';
while true do
begin
len:=recv(s,buf,sizeof(buf),0);//调试中发现调用函数500多次后此句死锁,不会再执行下一句
if len<1 then
break;
SetString(str,buf,len);
data:=data+str;
end;
closesocket(s);
WSACleanup();
end;