这两天我开始编写一些使用Delphi XE5 ping其他设备的代码。令我高兴的是,我发现有一个名为IcmpClient的组件可用于ping。这是代码,主要来自一些网络资源。
unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
Vcl.ExtCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient,
IdIcmpClient;
type
TfrmPing = class(TForm)
edtHost: TEdit;
btnPing: TButton;
ICMP: TIdIcmpClient;
Label1: TLabel;
Panel1: TPanel;
spnPing: TSpinEdit;
lstReplies: TListBox;
procedure btnPingClick(Sender: TObject);
procedure ICMPReply(ASender: TComponent; const ReplyStatus: TReplyStatus);
private
{ Private declarations }
public
{ Public declarations }
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; end;
end;
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.
在解决了错误#10013的问题后(通过授予管理员权限),我遇到了第二个错误#10040。
根据这里的一篇文章,有人说在从xe3升级到xe4后发生了,而Remy Lebeau说它正在修复。但几个月后,我们仍然遇到了XE5的错误。
我应该放弃Indy 10并寻找PING的其他方式,还是等待修复?