⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pingthreadunit.~pas

📁 DELPHI 下远程控制源码
💻 ~PAS
字号:
unit PingThreadUnit;

interface

uses
  Classes,IdIcmpClient,SysUtils,ComCtrls,IdWinsock;

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:=IPPROTO_ICMP;   
  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.
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -