Delphi 模拟Ping需要借助 Indy中的 ICMP控件。
源码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
Ping(icmp, Edit1.Text);
end;
procedure TForm1.icmpReply(ASender: TComponent; const AReplyStatus:
TReplyStatus);
var
sTime: string;
begin
//检测Ping的回复错误
if (AReplyStatus.MsRoundTripTime = 0) then
sTime := '<1'
else
sTime := '=';
//在列表框中显示Ping消息
ListBox1.Items.Add(Format('ICMP_SEQ=%d Reply from %s [%s] : Bytes=%d time%s%d ms TTL=%d',
[AReplyStatus.SequenceId,
Edit1.Text,
AReplyStatus.FromIpAddress,
AReplyStatus.BytesReceived,
sTime,
AReplyStatus.MsRoundTripTime,
AReplyStatus.TimeToLive]));
end;
procedure TForm1.Ping(AICMP: TIdIcmpClient; AIP: string);
var
i: integer;
begin
AICMP.Host := AIP;
AICMP.ReceiveTimeout := 1000; //设置超时
for i := 0 to 3 do begin
AICMP.Ping();
Application.ProcessMessages
end;
end;
end.
效果图如下: