⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanthread.pas

📁 delphi,scanport duoxiancheng
💻 PAS
字号:
//Example of Multi threading to Port Scan - by Rik Barker

unit ScanThread;

interface

uses
  Windows,Messages,Forms,SysUtils,Classes,ScktComp;

type
  TPortScannerThread = class(TThread)
  private
    { Private declarations }
    FWnd: HWND;             //Window Handle to report back to
    FID:Integer;            //Identifies the thread
    FStartPort:Integer;     //Port to start scanning from
    FEndPort:Integer;       //Port to finish scanning on
    FHost:string;           //Host to scan
    FSocket:TClientSocket;
  protected
    procedure Execute; override;
    procedure SocketConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure SocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
  public
  published
    constructor Create(ID:Integer;Wnd:HWND;Host:string;StartPort,EndPort:integer);
    destructor Destroy; override;
  end;

implementation

uses Unit1;

{ TPortScannnerThread }

constructor TPortScannerThread.Create(ID:Integer;Wnd:HWND;Host:string;StartPort,EndPort:integer);
begin
  inherited Create(True);     //Create Suspended
  FreeOnTerminate:=true;      //Let the thread free itself when complete
  FID:=ID;                    //A unique id for the thread (offset into our thread array)
  FWnd:=Wnd;                  //The Window to report back to when ports are checked
  FHost:=Host;                //The machine to scan
  FStartPort:=StartPort;      //Starting Port
  FEndPort:=EndPort;          //Ending Port
  Suspended:=False;           //Start the Execution of the Thread
end;

destructor TPortScannerThread.Destroy;
begin
  //This posts a message to the main form, tells us which thread
  //is done executing.
  PostMessage(FWnd,WM_THREADDONEMSG,FID,0);
  inherited destroy;
end;

procedure TPortScannerThread.Execute;
var
  CurrPort:Integer;
begin
  CurrPort:=FStartPort;
  FSocket:=TClientSocket.Create(nil);
  try
    FSocket.Address := FHost;
    FSocket.Port := CurrPort;
    //Assign the events
    FSocket.OnConnect:=SocketConnect;
    FSocket.OnError:=SocketError;
    FSocket.Active := true;
    while not Terminated do
      Application.ProcessMessages;
  finally
    FSocket.Free;
  end;
end;

procedure TPortScannerThread.SocketConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  //socket connection Connected - port is open.
  PostMessage(FWnd,WM_PORTSTATUS,FSocket.Port,1);
  //now try the next port...
  FSocket.Active := false;
  FSocket.Port := FSocket.Port +1;
  if FSocket.Port > FEndPort then
  begin
    Terminate;
    Exit;
  end;
  FSocket.Active := true;
end;

procedure TPortScannerThread.SocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  //connection couldn't be made....
  ErrorCode:=0;
  PostMessage(FrmMain.handle,WM_PORTSTATUS,FSocket.Port,0);
  FSocket.Active := false;
  FSocket.Port := FSocket.Port +1;
  if FSocket.Port > FEndPort then
  begin
    Terminate;
    Exit;
  end;
  FSocket.Active := true;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -