關於delphi的ping |
答題得分者是:geniustom
|
breeze_1
一般會員 發表:33 回覆:72 積分:21 註冊:2003-05-26 發送簡訊給我 |
|
hjhjack1
一般會員 發表:7 回覆:32 積分:24 註冊:2002-08-07 發送簡訊給我 |
|
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
|
geniustom
版主 發表:100 回覆:303 積分:260 註冊:2003-01-03 發送簡訊給我 |
使用INDY CLIENTS頁籤裡的IdIcmpClient..PING判定為TIMEOUT的時間可由ReceiveTimeout這個屬性來設定..以下是範例(我試過可用)請注意紅色部分
-----------------------------------------------------------------
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; IdIcmpClient1: TIdIcmpClient; procedure Button1Click(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 IdIcmpClient1.Host:=edit1.Text; IdIcmpClient1.Ping; end; procedure TForm1.IdIcmpClient1Reply(ASender: TComponent; const AReplyStatus: TReplyStatus); begin if AReplyStatus.BytesReceived=0 then showmessage('time out') //PING不到 else showmessage(inttostr(AReplyStatus.BytesReceived) ' mSec'); //顯示PINGTIME end; end. |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
kevinhom
一般會員 發表:0 回覆:1 積分:0 註冊:2007-01-02 發送簡訊給我 |
這個是我自己上班之餘無聊寫的...除了ping之外還有索取系統時間的小範例...不嫌它爛就參考看看吧...
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, IdTCPConnection, IdTCPClient, IdTime, IdTCPServer, IdTimeServer; type TForm1 = class(TForm) LIST: TMemo; Panel1: TPanel; BTN1: TButton; ICMP: TIdIcmpClient; BTN_STOP: TButton; Panel2: TPanel; Label1: TLabel; Panel3: TPanel; Label2: TLabel; Label3: TLabel; IdTime1: TIdTime; Panel4: TPanel; Edit1: TEdit; Timer1: TTimer; IdTimeServer1: TIdTimeServer; ComboBox1: TComboBox; procedure BTN1Click(Sender: TObject); procedure BTN_STOPClick(Sender: TObject); procedure ICMPReply(ASender: TComponent; const AReplyStatus: TReplyStatus); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure ComboBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public { Public declarations } end; var Form1: TForm1; GO : Boolean; implementation {$R *.dfm} procedure TForm1.BTN1Click(Sender: TObject); var I : Integer; begin ComboBox1.Items.Add(ComboBox1.Text);//將自行輸入的位址加到comboBox中 Go := true; List.Clear; //每次執行程式時先將ListBox清空一次 ICMP.OnReply := ICMPReply; ICMP.ReceiveTimeout := 1000; BTN1.Enabled := false; Label1.Caption := '正在進行連線Ping中.....'; Label3.Caption := '目前連結的位址是' ComboBox1.Text; try for I := 1 to 10000 do //ping一萬次 begin ICMP.Host := ComboBox1.Text; //將要ping的位址送給IdIcmpClient ICMP.Ping; //呼叫IdIcmpClient的ping函式 Application.ProcessMessages; if Go <> TRUE then Break; //假如Go變成false就離開迴圈 SLEEP(500);//睡覺函式 end; finally BTN1.Enabled := true; end; end; procedure TForm1.BTN_STOPClick(Sender: TObject); begin GO := false; //按下Stop按鈕時將Go變成false才能讓ping的函式結束 Label1.Caption := '連線已停止.....'; Label3.Caption := '目前對任何位址已無任何連結'; end; procedure TForm1.ICMPReply(ASender: TComponent; const AReplyStatus: TReplyStatus); var s : string; begin if (AReplyStatus.MsRoundTripTime = 0)then begin s := '<1'; end else begin S := '='; end; if (AReplyStatus.BytesReceived = 0)then begin List.Lines.Add('連結' ComboBox1.Text '失敗') end else begin List.Lines.Add(Format('%d位元,從IP:%s,流水號=%d ttl=%d 回應時間 %s%d ms', [AReplyStatus.BytesReceived,AReplyStatus.FromIpAddress, AReplyStatus.SequenceId, AReplyStatus.TimeToLive,s,AReplyStatus.MsRoundTripTime])); end; end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption := '目前尚未進行連線Ping中.....'; Label3.Caption := '歡迎使用位址連結Ping程式' end; procedure TForm1.Timer1Timer(Sender: TObject); //跟onTimer一樣的函式 begin IdTimeServer1.Active := true; //自行建立的一個TimeServer變成true才能讓IDTimerCline接受的到系統時間 IdTime1.Host := '127.0.0.1'; //ping自己的IdTimerServer EDIT1.Text := '系統時間:' DateTimeToStr(IdTime1.DateTime); end; procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = 13 then //Key = 13 的意思跟C 中的VK_Enter一樣 begin ComboBox1.Items.Add(ComboBox1.Text); //將自行輸入的位址加到comboBox中 BTN1.Click;//假如在comboBox按下了Enter鍵則就BTN1的按鍵按下 end; end; end. |
joana
一般會員 發表:35 回覆:70 積分:21 註冊:2005-09-02 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |