Use the TidicMpClient control in Delphi to implement the ping of the graphical interface very simple! New project, named pinggui.dpr, the window is named "frmping", add the following components: lstract: tidicmpclient; panel1: tipclite; btnping: tbutton; edthost: tedit; spnping: tspinedit; label1: TLabel; complete The source code is as follows: Unit main;
Interface
Uses Windows, Messages, Graphics, Controls, Forms, Dialogs, Stdctrls, ExtCtrls, Sysutils, Classes, IDICMPClient, iDBaseComponent, IDComponent, iDrawbase, iDrawClient, Spin
type TfrmPing = class (TForm) lstReplies: TListBox; ICMP: TIdIcmpClient; Panel1: TPanel; btnPing: TButton; edtHost: TEdit; spnPing: TSpinEdit; Label1: TLabel; procedure btnPingClick (Sender: TObject); procedure ICMPReply (ASender: TComponent; Const replystatus: TreplyStatus; private public end;
Var frmping: tfrmping;
Implementation {$ r * .dfm}
procedure TfrmPing.btnPingClick (Sender: TObject); var i: integer; begin ICMP.OnReply: = ICMPReply; ICMP.ReceiveTimeout: = 1000; btnPing.Enabled: = False; try ICMP.Host: = edtHost.Text; for i: = 1 to spnping.Value do begin icmp.Ping; Application.ProcessMessages; end; finally btnping.enabled: = true;
procedure TfrmPing.ICMPReply (ASender: TComponent; const ReplyStatus: TReplyStatus); var sTime: string; begin // TODO: check for error on ping reply (? ReplyStatus.MsgType) if (ReplyStatus.MsRoundTripTime = 0) then sTime: = ' <1 'else stime: =' = ';
lstReplies.Items.Add (Format ( '% d bytes from% s: icmp_seq =% d ttl =% d time% s% d ms', [ReplyStatus.BytesReceived, ReplyStatus.FromIpAddress, ReplyStatus.SequenceId, ReplyStatus.TimeToLive, sTime , Replystatus.msroundtriptime])); end; end.