1,593
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdRawBase, IdRawClient,
IdIcmpClient, ExtCtrls, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Memo1: TMemo;
Button2: TButton;
Memo2: TMemo;
Edit3: TEdit;
UpDown1: TUpDown;
Button3: TButton;
Timer1: TTimer;
IdIcmpClient1: TIdIcmpClient;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure IdIcmpClient1Reply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
Timer1.Interval := UpDown1.Position * 1000; //设置循环时间
Timer1.Enabled := True; //开始循环
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//通常在窗体建立的时候我们可以设置一些组件的属性。
Memo1.ScrollBars := ssVertical; //显示垂直滚动条
Memo1.Color := clBlack;
Memo1.Font.Color := clWhite;
Memo2.Font.Color := clRed;
Memo2.ScrollBars := ssVertical;
Timer1.Enabled := False;
UpDown1.Associate := Edit2; //关联到Edit2
UpDown1.Min := 1; //从1秒开始
UpDown1.Position := 5; //默认5秒
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled := False;
Button1.Enabled := True;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
try
IdIcmpClient1.Host := Edit1.Text;
IdIcmpClient1.Ping; //开始Ping操作
except
Timer1.Enabled := False; //非法错误停止循环
Button1.Enabled := True;
end;
end;
procedure TForm1.IdIcmpClient1Reply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
var
sTime: string;
AMemo: TMemo; //当前添加内容的Memo组件
begin
if (AReplyStatus.MsRoundTripTime = 0) then
sTime := '<1'
else
sTime := '=';
//超时Memo2显示,正常状态Memo1显示
if (AReplyStatus.MsRoundTripTime > StrToIntDef(Edit3.Text, 1000)) then
AMemo := Memo2 else
AMemo := Memo1;
AMemo.Lines.Add(Format('%s Reply from %s: bytes=%d time%s%dms TTL=%d', [
DatetimeToStr(now),
AReplyStatus.FromIpAddress,
AReplyStatus.BytesReceived,
sTime,
AReplyStatus.MsRoundTripTime,
AReplyStatus.TimeToLive
]));
end;
end.
看这个源码吧。