用INDY的TIdIcmpClient,这是一个PING程序,代码如下:
Function ping(Host:String;Timeout:Integer):String;
var
ICMP:TIdIcmpClient;
begin
Result:='';
ICMP:=TIdIcmpClient.Create(nil);
try
ICMP.ReceiveTimeout:=Timeout;
ICMP.Host:=Host;
ICMP.Ping;
Result:=ICMP.ReplyStatus.FromIpAddress;
ICMP.Free;
except
ICMP.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
address,ping_info:string;
begin
address:='www.sina.com.cn';
ping_info:=ping(address,10000);
if (ping_info='0.0.0.0') or (ping_info='') then showmessage('Disconnected') else showmessage('Connected');
end;