// 司马华鹏
unit PingThreadUnit;
interface
uses
Classes,IdIcmpClient,SysUtils,ComCtrls,IdStackConsts;
type
PingThread = class(TThread)
private
{ Private declarations }
protected
EchoCount:integer;
SaveTL:TListView;
PingTheIP:String;
MyIcmpClient:TIdIcmpClient;
TheMaxEchoTime:integer;
TheEchoTTl:byte;
TheEchoStr:String;
Procedure SaveIP;
Procedure PingFiveIP;
Procedure MyIdIcmpClientReply(ASender: TComponent; const AReplyStatus: TReplyStatus);
procedure Execute; override;
public
Constructor Create(TheIP:String;TheTL:TListView);
Destructor Destroy;override;
end;
implementation
uses
ScanIPUnit;
Constructor PingThread.Create(TheIP:String;TheTL:TListView);
begin
inherited Create(True);
self.FreeOnTerminate:=True;
EchoCount:=0;
PingTheIP:=TheIP;
SaveTL:=TheTL;
MyIcmpClient:=TIdIcmpClient.Create(nil);
MyIcmpClient.Protocol:=1;
MyIcmpClient.ReceiveTimeout:=5000;
MyIcmpClient.OnReply:=MyIdIcmpClientReply;
LetPingSingle.Enter;
SdPingCount:=SdPingCount+1;
LetPingSingle.Leave;
TheMaxEchoTime:=0;
TheEchoTTl:=0;
self.Suspended:=False;
end;
Procedure PingThread.SaveIP;
var
TheListItem:TListItem;
begin
try
TheListItem:=SaveTL.Items.Add;
TheListItem.ImageIndex:=0;
TheListItem.Caption:=PingTheIP;
TheLIstItem.SubItems.Add(IntToStr((EchoCount*100) div 5)+'%');
TheLIstItem.SubItems.Add(IntToStr(TheMaxEchoTime)+'''ms');
TheLIstItem.SubItems.Add(IntToStr(TheEchoTTL));
TheLIstItem.SubItems.Add(TheEchoStr);
except
end;
end;
Procedure PingThread.PingFiveIP;
var
i:integer;
begin
MyIcmpClient.Host:=PingTheIP;
for i:=1 to 5 do MyIcmpClient.Ping;
end;
procedure PingThread.Execute;
begin
try
PingFiveIP;
except
self.Terminate;
end;
end;
Procedure PingThread.MyIdIcmpClientReply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
begin
Try
TheMaxEchoTime:=TheMaxEchoTime+AReplyStatus.MsRoundTripTime;
TheEchoStr:=IntToStr(AReplyStatus.BytesReceived)+'字节';
TheEchoTTl:=AReplyStatus.TimeToLive;
if (AReplyStatus.ReplyStatusType=rsEcho) then
if(AReplyStatus.FromIpAddress=PingTheIP) then
EchoCount:=EchoCount+1;
except
end;
end;
Destructor PingThread.Destroy;
begin
try
if EchoCount>0 then
Synchronize(SaveIP);
except
end;
MyIcmpClient.Free;
LetPingSingle.Enter;
SdPingCount:=SdPingCount-1;
LetPingSingle.Leave;
inherited Destroy;
end;
end.