关于delphi中利用IdIcmpClient空间实现ping命令解决思路
关于delphi中利用IdIcmpClient空间实现ping命令
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdRawBase, IdRawClient, IdIcmpClient, Vcl.ActnMan,
Vcl.ActnColorMaps, Vcl.ExtCtrls, Vcl.ComCtrls;
type
TForm1 = class(TForm)
ICMP: TIdIcmpClient;
edtHost: TEdit;
btnPing: TButton;
btnStop: TButton;
Info: TListBox;
stat1: TStatusBar;
procedure btnPingClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure ICMPReply(ASender: TComponent; const AReplyStatus: TReplyStatus);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
i,b,s : Integer;
implementation
{$R *.dfm}
procedure TForm1.btnPingClick(Sender: TObject);
begin
b := 0; //标记掉包数量
i := 0; //记录ping的总次数
s := 0; //执行ping操作
Info.Clear;
ICMP.Host := edtHost.Text;
while (i<100000) and (s=0) do
begin
Application.ProcessMessages;
i := i+1;
ICMP.Ping; //执行ping
if (i= 10) and (b=10) then //若前10次均ping失败则结束本次ping
s := 1;
end;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
s := 1; //停止ping操作
end;
procedure TForm1.ICMPReply(ASender: TComponent;const AReplyStatus: TReplyStatus);
var
FMsg: string;
Tm: integer;
begin
with ICMP.ReplyStatus do
FMsg := 'Reply from'+' '+edthost.Text;
FMsg := FMsg+' '+'Byte='+IntTostr(AReplyStatus.BytesReceived); //返回的字节
FMsg := FMsg+' '+'TTL='+IntTostr(AReplyStatus.TimeToLive); //返回生存时间
Tm:= AReplyStatus.MsRoundTripTime; //返回执行时间
if Tm<1 then Tm:=1;
FMsg := FMsg+' '+ 'Time='+IntToStr(Tm)+'ms';
Info.Items.Add(FMsg);
if (AReplyStatus.BytesReceived=0) or (AReplyStatus.TimeToLive=0) then
begin
b := b+1; //记录掉包数量
info.ItemIndex := info.Items.Count-1;
end;
if Info.Count>20 then info.Clear;
if i mod 100 = 0 then stat1.SimpleText:='Lost:'+
IntToStr(b)+'/'+IntToStr(i)+'='+copy (FloatToStr(b/i*1000),1,4)+'‰';
//ping满100次显示一次掉包数
end;
end.
ping的时候出错,这是什么原因呢
------解决方案--------------------
路过,学习一下。
------解决方案--------------------
怎么把这个放到一个线程中间去
------解决方案--------------------
S:='ping -n 20 192.168.1.100';
WinExec( pchar(S),SW_SHOW);
这样,也可以。
------解决方案--------------------
XE5 ping出现 10040错误信息
------解决方案--------------------
我也是在xe5执行ping的时候出现Error 10040 的错误,找了好几久都没找到问题
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdRawBase, IdRawClient, IdIcmpClient, Vcl.ActnMan,
Vcl.ActnColorMaps, Vcl.ExtCtrls, Vcl.ComCtrls;
type
TForm1 = class(TForm)
ICMP: TIdIcmpClient;
edtHost: TEdit;
btnPing: TButton;
btnStop: TButton;
Info: TListBox;
stat1: TStatusBar;
procedure btnPingClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure ICMPReply(ASender: TComponent; const AReplyStatus: TReplyStatus);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
i,b,s : Integer;
implementation
{$R *.dfm}
procedure TForm1.btnPingClick(Sender: TObject);
begin
b := 0; //标记掉包数量
i := 0; //记录ping的总次数
s := 0; //执行ping操作
Info.Clear;
ICMP.Host := edtHost.Text;
while (i<100000) and (s=0) do
begin
Application.ProcessMessages;
i := i+1;
ICMP.Ping; //执行ping
if (i= 10) and (b=10) then //若前10次均ping失败则结束本次ping
s := 1;
end;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
s := 1; //停止ping操作
end;
procedure TForm1.ICMPReply(ASender: TComponent;const AReplyStatus: TReplyStatus);
var
FMsg: string;
Tm: integer;
begin
with ICMP.ReplyStatus do
FMsg := 'Reply from'+' '+edthost.Text;
FMsg := FMsg+' '+'Byte='+IntTostr(AReplyStatus.BytesReceived); //返回的字节
FMsg := FMsg+' '+'TTL='+IntTostr(AReplyStatus.TimeToLive); //返回生存时间
Tm:= AReplyStatus.MsRoundTripTime; //返回执行时间
if Tm<1 then Tm:=1;
FMsg := FMsg+' '+ 'Time='+IntToStr(Tm)+'ms';
Info.Items.Add(FMsg);
if (AReplyStatus.BytesReceived=0) or (AReplyStatus.TimeToLive=0) then
begin
b := b+1; //记录掉包数量
info.ItemIndex := info.Items.Count-1;
end;
if Info.Count>20 then info.Clear;
if i mod 100 = 0 then stat1.SimpleText:='Lost:'+
IntToStr(b)+'/'+IntToStr(i)+'='+copy (FloatToStr(b/i*1000),1,4)+'‰';
//ping满100次显示一次掉包数
end;
end.
------解决方案--------------------
路过,学习一下。
------解决方案--------------------
怎么把这个放到一个线程中间去
------解决方案--------------------
S:='ping -n 20 192.168.1.100';
WinExec( pchar(S),SW_SHOW);
这样,也可以。
------解决方案--------------------
XE5 ping出现 10040错误信息
------解决方案--------------------
我也是在xe5执行ping的时候出现Error 10040 的错误,找了好几久都没找到问题