1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| 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. |
Partager