G
getmoney
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的GetWebText函数中的setsockopt...部分,不起作用。设置的是3秒,但是要等上20秒左右。 函数是用来获取网页代码的,但是有一些网站如果打不开,或者响应非常慢,我想让函数在3秒后 自动断开。 但是超时部分代码 好像有问题,怎么解决????function GetWebText: string;const H = #13#10;var Len, Skt: integer; Name: SockAddr_IN; He: PHostEnt; Buf: array[0..1023] of Char; Str, Data: string; WSD: WSADATA; Timeout: Integer;begin Timeout := 3000; //设置超时3秒 WSAStartup($101, WSD); Skt := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); He := GetHostbyName(PChar('www.78ai.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)^)^; Connect(Skt, Name, Sizeof(Name)); setsockopt(Skt, SOL_SOCKET, SO_SNDTIMEO, PChar(@timeout), sizeof(timeout)); //设置发送超时 setsockopt(Skt, SOL_SOCKET, SO_RCVTIMEO, PChar(@timeout), sizeof(timeout)); //设置接收超时 Str := ''; Str := Str + 'GET / HTTP/1.1' + H; Str := Str + 'Host: www.78ai.com' + H; Str := Str + 'Connection: Close' + H + H; Send(Skt, PChar(Str)^, Length(Str), 0); while True do begin Len := Recv(Skt, Buf, Sizeof(Buf), 0); if Len < 1 then Break; SetString(Str, Buf, Len); Data := Data + Str; end; Closesocket(Skt); WSACleanup(); Result := Data;end;