16,748
社区成员
发帖
与我相关
我的任务
分享
unit itrsUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg,
Winsock, Buttons, Provider, ComCtrls, CheckLst, IdBaseComponent,
IdComponent, IdRawBase, IdRawClient, IdIcmpClient;
type
IPAddrList = ^TIPAddr;
TIPAddr = record
IP: string;
Sender: TStaticText;
end;
TShowStatus = procedure(AIndex: Integer; AStatus: TReplyStatus) of object;
TItrsForm = class(TForm)
startButton: TButton;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
Button1: TButton;
Timer1: TTimer;
procedure startButtonClick(Sender: TObject);
procedure endClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure ShowStatus(AIndex: Integer; AStatus: TReplyStatus);
public
{ Public declarations }
end;
var
ItrsForm: TItrsForm;
s: Integer;
IPAddr: array[0..2] of TIPAddr;
implementation
{$R *.dfm}
uses
UnitThread;
procedure TItrsForm.ShowStatus(AIndex: Integer; AStatus: TReplyStatus);
begin
if((AStatus.BytesReceived >0) and (AStatus.TimeToLive >0))
and (AStatus.FromIpAddress <> '') and (AStatus.FromIpAddress <> '0.0.0.0') then
IPAddr[AIndex].Sender.Color := clLime
else
IPAddr[AIndex].Sender.Color := clRed;
end;
procedure TItrsForm.Timer1Timer(Sender: TObject);
var
i: Integer;
begin
if s=0 then
begin
Timer1.Enabled := False;
for i := Low(IPAddr) to High(IPAddr) do
TMyThread.Create(IPAddr[i].IP, i, ShowStatus);
Timer1.Enabled := True;
end;
end;
procedure TItrsForm.startButtonClick(Sender: TObject);
begin
startButton.Enabled:=False;
Button1.Enabled:=true;
s:=0; //执行(s=0)或停止(s=1)标志
IPAddr[0].IP := '192.168.1.101';
IPAddr[0].Sender := StaticText1;
IPAddr[1].IP := '192.168.1.102';
IPAddr[1].Sender := StaticText2;
IPAddr[2].IP := '192.168.1.103';
IPAddr[2].Sender := StaticText3;
{IPAddr := (
(IP:'192.168.1.101';Sender:StaticText1),
(IP:'192.168.1.102';Sender:StaticText2),
(IP:'192.168.1.103';Sender:StaticText3)
);}
Timer1.Enabled := True;
end;
procedure TItrsForm.endClick(Sender: TObject);
begin
startButton.Enabled:=True;
Button1.Enabled:=false;
s := 1;
ItrsForm.StaticText1.Color:=TColor(clBlack);
ItrsForm.StaticText2.Color:=TColor(clBlack);
ItrsForm.StaticText3.Color:=TColor(clBlack);
Timer1.Enabled := false;
end;
end.
//UnitThread.pas
unit UnitThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,
Winsock, Buttons, Provider, ComCtrls,IdIcmpClient,
itrsUnit;
type
TMyThread = class(TThread)
ICMP: TIdIcmpClient;
private
FIP: string;
FIndex: Integer;
FStatus: TReplyStatus;
FMethod: TShowStatus;
procedure ShowReplyStatus();
protected
procedure Execute; override;
public
constructor Create(AIP: string; AIndex: Integer; AMethod: TShowStatus);
end;
var
CS: TRTLCriticalSection;
implementation
constructor TMyThread.Create(AIP: string; AIndex: Integer; AMethod: TShowStatus);
begin
inherited Create(False);
Self.FreeOnTerminate := True;
FIP := AIP;
FIndex := AIndex;
FMethod := AMethod;
end;
procedure TMyThread.Execute;
begin
with TIdIcmpClient.Create(nil) do
try
Host := FIP;
Ping();
FStatus := ReplyStatus;
Synchronize(ShowReplyStatus);
finally
Free;
end;
end;
procedure TMyThread.ShowReplyStatus();
begin
if Assigned(FMethod) then
FMethod(FIndex, FStatus);
end;
end.
//界面中
type
TIPAddr = record
IP: string;
Sender: TStaticText;
end;
TShowStatus = procedure(AIndex: Integer; AStatus: TReplyStatus) of object;
TForm1 = class(TForm)
private
procedure ShowStatus(AIndex: Integer; AStatus: TReplyStatus);
end;
var
IPAddr: array[0..29] of TIPAddr;
//初始化,IP地址和TStaticText对应.....
IPAddr[0].IP := '192.168.1.100';
IPAddr[0].Sender := StaticText1;
ShowStatus(AIndex: Integer; AStatus: TReplyStatus);
begin
if((AReplyStatus.BytesReceived >0) and (AReplyStatus.TimeToLive >0)) and (AReplyStatus.MsRoundTripTime < t) and (AReplyStatus.FromIpAddress <> '') and (AReplyStatus.FromIpAddress <> '0.0.0.0')then
IPAddr[AIndex].Sender.Color = clLime
else
IPAddr[AIndex].Sender.Color = clRed;
end;
OnTime();
var
i: Integer;
begin
Time1.Enable := False;
for i := Low(IPAddr) to High(IPAddr) do
TMyThread.Create(IPAddr[i].IP, i, ShowStatus);
Time1.Enable := True;
end;
//线程类
TMyThread = class(TThread)
private
FIP: string;
FIndex: Integer;
FStatus: TReplyStatus;
FMethod: TShowStatus;
procedure ShowReplyStatus();
protect
procedure Execute(); override;
public
constructor Create(AIP: string; AIndex: Integer; AMethod: TShowStatus);
end;
Create(AIP: string; AIndex: Integer; AMethod: TShowStatus);
begin
inherited Create(False);
FreeOnTerminated := True;
FIP := AIP;
FIndex := AIndex;
FMethod := AMethod;
end;
Execute();
begin
with TIdIcmpClient.Create(nil) do
try
Host := FIP;
Ping();
FStatus := ReplyStatus;
Synchronize(ShowReplyStatus);
finally
Free;
end;
end;
ShowReplyStatus();
begin
if Assigned(FMethod) then
FMethod(FIndex, FStatus);
end;