我有了一个端口检测程序如下 function TProxyTest.ConnectToServer(TestAddress,TestPORT:String):Boolean;
var
rc : integer;
sa : TSockAddr;
begin
// Startup WinSock
Result:=false;
FillChar(sa, sizeof(sa), 0);
sa.sin_family := AF_INET;
sa.sin_port := htons(StrToInt(TestPORT));
sa.sin_addr.S_addr := inet_addr(PChar(TestAddress));
DataSock := socket(AF_INET, SOCK_STREAM, 0);
SetOutTime(DataSock,1000); //超时设为1秒
rc := connect(DataSock, sa, sizeof(sa));
if rc<>0 then Exit;
Result:=True;
end; procedure TProxyTest.SetOutTime(Sock: TSocket;Timeout:Integer);
var
TimeVal: TTimeVal;
begin
TimeVal.tv_sec := Timeout div 1000;
TimeVal.tv_usec := (Timeout mod 1000) * 1000;
setsockopt(Sock, SOL_SOCKET,SO_RCVTIMEO, @TimeVal,sizeof(TimeVal));
setsockopt(Sock, SOL_SOCKET,SO_SNDTIMEO, @TimeVal,sizeof(TimeVal));
end; 运行发现设置超时没有起作用,查询WINSOCK的HELP有如下一段: BSD options not supported for setsockopt are: Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening
SO_RCVLOWAT int Receive low water
SO_RCVTIMEO int Receive time-out
SO_SNDLOWAT int Send low water
SO_SNDTIMEO int Send time-out
SO_TYPE int Type of the socket winsock不支持超时设置,不知有没有其他办法.
最好不要在起线程监控.
var
rc : integer;
sa : TSockAddr;
begin
// Startup WinSock
Result:=false;
FillChar(sa, sizeof(sa), 0);
sa.sin_family := AF_INET;
sa.sin_port := htons(StrToInt(TestPORT));
sa.sin_addr.S_addr := inet_addr(PChar(TestAddress));
DataSock := socket(AF_INET, SOCK_STREAM, 0);
SetOutTime(DataSock,1000); //超时设为1秒
rc := connect(DataSock, sa, sizeof(sa));
if rc<>0 then Exit;
Result:=True;
end; procedure TProxyTest.SetOutTime(Sock: TSocket;Timeout:Integer);
var
TimeVal: TTimeVal;
begin
TimeVal.tv_sec := Timeout div 1000;
TimeVal.tv_usec := (Timeout mod 1000) * 1000;
setsockopt(Sock, SOL_SOCKET,SO_RCVTIMEO, @TimeVal,sizeof(TimeVal));
setsockopt(Sock, SOL_SOCKET,SO_SNDTIMEO, @TimeVal,sizeof(TimeVal));
end; 运行发现设置超时没有起作用,查询WINSOCK的HELP有如下一段: BSD options not supported for setsockopt are: Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening
SO_RCVLOWAT int Receive low water
SO_RCVTIMEO int Receive time-out
SO_SNDLOWAT int Send low water
SO_SNDTIMEO int Send time-out
SO_TYPE int Type of the socket winsock不支持超时设置,不知有没有其他办法.
最好不要在起线程监控.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货