Recherche des adresses IP sur un reseaux avec DELPHI 5

djibrail Messages postés 1 Date d'inscription dimanche 26 juillet 2015 Statut Membre Dernière intervention 26 juillet 2015 - 26 juil. 2015 à 18:57
medloulouchene Messages postés 1 Date d'inscription mercredi 22 juillet 2015 Statut Membre Dernière intervention 1 août 2015 - 1 août 2015 à 18:44
Bonjour tout le monde;
Je suis à la recherche d`un code qui me permettra de faire un Ping de l'ensemble des adresses IP d'un réseau.
Étant débutant sur Delphi, je ne sais pas comment m'y prendre. Ce code en dessus me semble bon, mais je suis bloqué sur le premier boucle for (for n := 0 to Length(PingThreads) - 1 do). Qui pourra m'aider par conséquence afin avancer ce code ou en l'occurrence me proposer un autre code qui fonctionne bien ?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;

TYPE
PingThreads = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

var
Form1: TForm1;

implementation


{$R *.DFM}



procedure PingThreads.Execute;
var
n: integer;
begin
for n := 0 to Length(PingThreads) - 1 do
PostThreadMessage(PingThreads[n], WM_QUIT, 0, 0); // terminate all threads
PingThreads := nil; 

and this will execute the threaded ping to scan all ips from 127.0.0.0 to 127.0.0.255:
var
i:integer;
begin
listbox1.clear;
for i:=0 to 255 do begin
with TPingThread.Create(True) do
begin
SetLength(PingThreads, Length(PingThreads) + 1);
PingThreads[Length(PingThreads) - 1] := ThreadId;
FreeOnTerminate := True;
OnTerminate := PingThreadTerminate;
Addr := '127.0.0.'+inttostr(i);
Resume;
end;
end;

// Here's the code for a threaded ping
type
TPingThread = class(TThread)
private
FPing: TPing;
procedure FPingDnsLookupDone(Sender: TObject; Error: Word);
procedure FPingEchoReply(Sender, Icmp: TObject; Error: Integer);
public
Addr: string;
PingResult: string;
procedure Execute; override;
end;

//------------------------------------------------------------------------------
// TPingThread
//------------------------------------------------------------------------------
procedure TPingThread.Execute;
var
Msg: TagMsg;
begin

FPing := TPing.Create(nil); // create in thread context
FPing.OnDnsLookupDone := FPingDnsLookupDone;
FPing.DnsLookup(Addr);

while not Terminated do
begin
//if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin

GetMessage(Msg, 0, 0, 0); // create a message queue
if Msg.message = WM_QUIT then
Terminate;
TranslateMessage(Msg);
DispatchMessage(Msg);
end;

FPing.Destroy;
end;

//------------------------------------------------------------------------------
procedure TPingThread.FPingDnsLookupDone(Sender: TObject; Error: Word);
begin
if Error <> 0 then
begin
PingResult := 'Unknown host ''' + Addr + '''';
Terminate;
Exit;
end;

with Sender as TPing do
begin
Address := DnsResult;
OnEchoReply := FPingEchoReply;
Ping;
end;
end;

//------------------------------------------------------------------------------
procedure TPingThread.FPingEchoReply(Sender, Icmp: TObject; Error: Integer);
begin
with Sender as TPing do
if Error = 0 then
PingResult := 'Cannot ping host (' + HostIP + ') : ' + ErrorString
else begin
form1.listbox1.items.add(HostIP);
PingResult := 'Received ' + IntToStr(Reply.DataSize) + ' bytes from ' + HostIP + ' in ' + IntToStr(Reply.RTT) + ' ms';
end;
Terminate;
end;

//------------------------------------------------------------------------------
// TForm1
//------------------------------------------------------------------------------
procedure TForm1.PingThreadTerminate(Sender: TObject);
begin
if not Application.Terminated then
with Sender as TPingThread do
Memo1.Lines.Add(PingResult);
end;

//------------------------------------------------------------------------------
procedure TForm1.PingBtnClick(Sender: TObject);
begin
with TPingThread.Create(True) do
begin
SetLength(PingThreads, Length(PingThreads) + 1);
PingThreads[Length(PingThreads) - 1] := ThreadId;
FreeOnTerminate := True;
OnTerminate := PingThreadTerminate;
Addr := Edit1.Text;
Resume;
end;
end;
end.

end.

2 réponses

cs_AccessToYou Messages postés 34 Date d'inscription mardi 21 novembre 2000 Statut Membre Dernière intervention 16 juin 2016 1
29 juil. 2015 à 03:47
0
medloulouchene Messages postés 1 Date d'inscription mercredi 22 juillet 2015 Statut Membre Dernière intervention 1 août 2015
1 août 2015 à 18:44
DJAWAB63063
0
Rejoignez-nous