Indy IdICMPClient com laço FOR
18/06/2021
0
Meu problema é o seguinte: sempre que uso laço FOR para alimentar o idICMPclient.host ele me retorna o erro
Este foi o código que testei no delphi tokyo.
function TForm1.PingaIP(IP: String): boolean; begin with IdIcmpClient1 do begin Host := IP; ReceiveTimeout := 500; Ping; if ReplyStatus.BytesReceived > 0 then result := true else result := false; end; end;
E no botão:
procedure TForm1.Button1Click(Sender: TObject); Var i: integer; begin for I := 1 to 254 do begin if PingaIP('192.168.0.'+inttostr(i)) = True then Memo1.lines.add('192.168.0.'+inttostr(i)); end; end;
Por favor, me ajudem a solucionar isso. Não posso usar softwares de 3º por precisar desses dados dentro da minha aplicação.
Jean Ribeiro
Post mais votado
20/06/2021
interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, IdGlobal; type TForm3 = class(TForm) Button1: TButton; Edit1: TEdit; Memo1: TMemo; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } CancelaPing: boolean; function PingaIP(strIP: string): boolean; public { Public declarations } end; var Form3: TForm3; implementation {$R *.dfm} function TForm3.PingaIP(strIP: string): boolean; var ICMP : TIdIcmpClient; ABuffer: String; begin ICMP := TIdIcmpClient.Create(nil); // crio o componente em tempo de execução try try ICMP.PacketSize := 32; ICMP.Host := strIP; ICMP.ReceiveTimeout := 500; ICMP.Protocol := 1; ICMP.IPVersion := Id_IPv4; ABuffer := ICMP.Host + StringOfChar(' ', 255); ICMP.Ping(ABuffer); // envio um conteúdo para o ping if ICMP.ReplyStatus.BytesReceived > 0 then Result := True else Result := False; except Result := False; end; finally ICMP.Destroy; end; end; procedure TForm3.Button1Click(Sender: TObject); var i: integer; baseIP, IP: string; begin TWinControl(Sender).Enabled := False; Memo1.Lines.Clear; baseIP := Edit1.Text; CancelaPing := False; for I := 0 to 254 do begin IP := baseIP+inttostr(i); if PingaIP(IP) then Memo1.Lines.Add(IP + ': Ok') else Memo1.Lines.Add(IP + ': Falha'); Application.ProcessMessages; if CancelaPing then break; end; TWinControl(Sender).Enabled := True; end; procedure TForm3.Button2Click(Sender: TObject); begin CancelaPing := True; end; end.
Emerson Nascimento
Mais Posts
21/06/2021
Wanderson Cardoso
Já pesquisei muito só que até agora não achei uma solução!
Será que alguém pode me ajudar ?
Desde já agradeço!
25/06/2021
Jean Ribeiro
interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, IdGlobal; type TForm3 = class(TForm) Button1: TButton; Edit1: TEdit; Memo1: TMemo; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } CancelaPing: boolean; function PingaIP(strIP: string): boolean; public { Public declarations } end; var Form3: TForm3; implementation {$R *.dfm} function TForm3.PingaIP(strIP: string): boolean; var ICMP : TIdIcmpClient; ABuffer: String; begin ICMP := TIdIcmpClient.Create(nil); // crio o componente em tempo de execução try try ICMP.PacketSize := 32; ICMP.Host := strIP; ICMP.ReceiveTimeout := 500; ICMP.Protocol := 1; ICMP.IPVersion := Id_IPv4; ABuffer := ICMP.Host + StringOfChar(' ', 255); ICMP.Ping(ABuffer); // envio um conteúdo para o ping if ICMP.ReplyStatus.BytesReceived > 0 then Result := True else Result := False; except Result := False; end; finally ICMP.Destroy; end; end; procedure TForm3.Button1Click(Sender: TObject); var i: integer; baseIP, IP: string; begin TWinControl(Sender).Enabled := False; Memo1.Lines.Clear; baseIP := Edit1.Text; CancelaPing := False; for I := 0 to 254 do begin IP := baseIP+inttostr(i); if PingaIP(IP) then Memo1.Lines.Add(IP + ': Ok') else Memo1.Lines.Add(IP + ': Falha'); Application.ProcessMessages; if CancelaPing then break; end; TWinControl(Sender).Enabled := True; end; procedure TForm3.Button2Click(Sender: TObject); begin CancelaPing := True; end; end.
Perfeito, simplesmente perfeito.
Clique aqui para fazer login e interagir na Comunidade :)