線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2430
推到 Plurk!
推到 Facebook!

IP scan

答題得分者是:wu-eric
cashyy
高階會員


發表:117
回覆:322
積分:212
註冊:2004-04-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-05-08 01:50:42 IP:59.104.xxx.xxx 未訂閱
各位大大,小弟參考站內的文章,修改成下列程式碼,但不知是那裡有問題,會run非常久,小弟是想做成像ip scan一樣,可以搜尋同一網段的電腦!
  AnsiString get,get1,get2,ret,aIP;
  int n,i;
  aIP = "192.168.1.3";
  n = aIP.Pos(".");
  get = aIP.SubString(1,n-1);
  ret = get;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get1 = aIP.SubString(1,n-1);
  ret = ret "." get1;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get2 = aIP.SubString(1,n-1);
  ret = ret "." get2;      WORD wVersionRequested=MAKEWORD(1,1);
  WSADATA wsaData;      struct hostent *host;
  struct in_addr *myaddr;
  char ip_address[256];      TListItem  *NewItem;
  ListView1->Items->Clear();
  for(i=0;i<=255;i  )
  {
    ret = get "." get1 "." get2 "." IntToStr(i);
    WSAStartup(wVersionRequested, &wsaData);
    lstrcpy(ip_address, ret.c_str());
    myaddr=(struct in_addr*)malloc(sizeof(struct in_addr));
    myaddr->s_addr=inet_addr(ip_address);
    //小弟用debug的方式找到會停留很久的地方
    host=gethostbyaddr((char *)myaddr, 4, AF_INET);
    if(host!=NULL)
    {
      NewItem=ListView1->Items->Add();
      NewItem->Caption=host->h_name;
      ListView1->LargeImages = ImageList1;
      ListView1->Refresh();
    }
  }
  free(myaddr);
  WSACleanup();
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-05-08 13:04:27 IP:211.22.xxx.xxx 未訂閱
那一行應該不是問題,IP的計算請參考下列站內網址: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=68586 WSAStartup只要起始Winsock一次即可,不用放在迴圈裡,還有建議算出IP的數量後,分成不同的RANGE由THREAD去分別掃瞄,會快很多! ----------------------------------------------- Creation is the fundation of promotion.
cashyy
高階會員


發表:117
回覆:322
積分:212
註冊:2004-04-30

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-05-08 13:27:15 IP:59.104.xxx.xxx 未訂閱
引言: 那一行應該不是問題,IP的計算請參考下列站內網址: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=68586 WSAStartup只要起始Winsock一次即可,不用放在迴圈裡,還有建議算出IP的數量後,分成不同的RANGE由THREAD去分別掃瞄,會快很多! ----------------------------------------------- Creation is the fundation of promotion.
可以解釋一下這一行嗎?小弟不太懂!謝謝!
wu-eric
中階會員


發表:14
回覆:82
積分:64
註冊:2004-03-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-05-08 17:05:38 IP:210.66.xxx.xxx 未訂閱
引言: WSAStartup只要起始Winsock一次即可,不用放在迴圈裡,還有建議算出IP的數量後,分成不同的RANGE由THREAD去分別掃瞄,會快很多!
1.他的意思是分成不用的範圍,每一個範圍用不同的執行緒去執行啦 2.因你在執行gethostbyaddr時,cpu等待遠端回應,所以若用多執行緒TThread去作,效果應會更好
yhchu
一般會員


發表:2
回覆:70
積分:24
註冊:2004-01-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-05-09 10:51:05 IP:220.130.xxx.xxx 未訂閱
gethostbyaddr 會向 DNS server 要求該 IP 所代表主機的資訊 (主要是 hostname),你的區網內如果沒有 DNS server,或是掃描的 IP 根本不存在,那 delay 很久就很正常了。解決的方法還是如 Stallion 所說,用 multi-thread 的方式就好了。
cashyy
高階會員


發表:117
回覆:322
積分:212
註冊:2004-04-30

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-05-10 02:15:32 IP:211.74.xxx.xxx 未訂閱
感謝大大們的回應,小弟目前在測試Stallion大大所講的方法!不過執行緒真的很難寫!站裡面的sample code 又很難懂!所以正在努力中!!
wu-eric
中階會員


發表:14
回覆:82
積分:64
註冊:2004-03-17

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-05-10 16:16:34 IP:210.66.xxx.xxx 未訂閱
這個範例,你參考看看 unit1.h 內容如下
 
#include "Func.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
        TButton *Button1;
        TListView *ListView1;
        TImageList *ImageList1;
        TEdit *Edit1;
        TEdit *Edit2;
        TLabel *Label1;
        TLabel *Label2;
        TComboBox *ComboBox1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall ListView1DblClick(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
        TLanComputerThread *LanComputerThread;
};
Unit1.cpp如下
  AnsiString get,get1,get2,ret,aIP;
  int n,i,iThreadCount;
  aIP = ComboBox1->Text;
  n = aIP.Pos(".");
  get = aIP.SubString(1,n-1);
  ret = get;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get1 = aIP.SubString(1,n-1);
  ret = ret "." get1;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get2 = aIP.SubString(1,n-1);
  ret = ret "." get2;      WORD wVersionRequested=MAKEWORD(1,1);
  WSADATA wsaData;
  WSAStartup(wVersionRequested, &wsaData);
  
  struct hostent host[256];
  struct hostent *tmpHost;
  TListItem  *NewItem;      ListView1->Items->Clear();
  iThreadCount=0;
  for(i=0;i<=255;i  )
  {
    ret = get "." get1 "." get2 "." IntToStr(i);
    while(iThreadCount>=64)
      Sleep((unsigned)1000);
    iThreadCount  ;
    host[i].h_name = (char *)malloc(30);
    memset(host[i].h_name,0x00,30);
    LanComputerThread = new TLanComputerThread(&ret,&host[i],&iThreadCount);
  }      while(iThreadCount>0)
    Sleep((unsigned)1000);
  for(i=0;i<=255;i  )
  {
    if(strlen(host[i].h_name)>0)
    {
      NewItem=ListView1->Items->Add();
      NewItem->Caption=host[i].h_name;
      ListView1->LargeImages = ImageList1;
      ListView1->Refresh();
    }
    free(host[i].h_name);
  }
  WSACleanup();
}
 
Func.h 如下
#include 
//===========================================================================
// 以下程式碼為 TLanComputer Class 之宣告
// TLanComputer Class 主要用執行緒搜尋網段電腦
//===========================================================================
class TLanComputerThread : public TThread
{
  private:
        AnsiString ret;
        char ip_address[256];
        struct in_addr *myaddr;
        int *iCount;
        struct hostent *thost;
        struct hostent *tmphost;
  protected:
        void __fastcall Execute();
  public:
          __fastcall TLanComputerThread(AnsiString *aIP,hostent *host,int *iThreadCount);    };
 
Func.cpp 內容如下
 
#include <math.h>
#include 
#include 
#include "Iphlpapi.h"
#include "IdGlobal.hpp"
#include "Func.h"
//---------------------------------------------------------------------------
// 以下為 TLan 函式之程式碼
// 功能:用執行緒搜尋網段電腦
// 傳入參數:IP(型態 AnsiString)            EX: IP = 192.168.1.3;
//---------------------------------------------------------------------------    //---------------------------------------------------------------------------
__fastcall TLanComputerThread::TLanComputerThread(AnsiString *aIP,hostent *host,int *iThreadCount)
                               :TThread(aIP)
{
  ret = *aIP;
  tmphost =  host;
  iCount = iThreadCount;
  Resume();
}
//---------------------------------------------------------------------------
void __fastcall TLanComputerThread::Execute()
{
  AnsiString astmp;
  int iPcNameLen;
  lstrcpy(ip_address,ret.c_str());
  myaddr=(struct in_addr*) malloc(sizeof(struct in_addr));
  myaddr->s_addr=inet_addr(ip_address);
  thost=gethostbyaddr((char *)myaddr, 4, AF_INET);
  if(thost != NULL)
    strcpy(tmphost->h_name,thost->h_name);
  free(myaddr);
  *iCount=*iCount-1;
}
//---------------------------------------------------------------------------
以上請您試試看吧,有問題再討論看看
cashyy
高階會員


發表:117
回覆:322
積分:212
註冊:2004-04-30

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-05-10 16:24:53 IP:59.104.xxx.xxx 未訂閱
引言: 這個範例,你參考看看 unit1.h 內容如下
 
#include "Func.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
        TButton *Button1;
        TListView *ListView1;
        TImageList *ImageList1;
        TEdit *Edit1;
        TEdit *Edit2;
        TLabel *Label1;
        TLabel *Label2;
        TComboBox *ComboBox1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall ListView1DblClick(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
        TLanComputerThread *LanComputerThread;
};
Unit1.cpp如下
  AnsiString get,get1,get2,ret,aIP;
  int n,i,iThreadCount;
  aIP = ComboBox1->Text;
  n = aIP.Pos(".");
  get = aIP.SubString(1,n-1);
  ret = get;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get1 = aIP.SubString(1,n-1);
  ret = ret "." get1;      aIP = aIP.SubString(n 1,aIP.Length());
  n = aIP.Pos(".");
  get2 = aIP.SubString(1,n-1);
  ret = ret "." get2;      WORD wVersionRequested=MAKEWORD(1,1);
  WSADATA wsaData;
  WSAStartup(wVersionRequested, &wsaData);
  
  struct hostent host[256];
  struct hostent *tmpHost;
  TListItem  *NewItem;      ListView1->Items->Clear();
  iThreadCount=0;
  for(i=0;i<=255;i  )
  {
    ret = get "." get1 "." get2 "." IntToStr(i);
    while(iThreadCount>=64)
      Sleep((unsigned)1000);
    iThreadCount  ;
    host[i].h_name = (char *)malloc(30);
    memset(host[i].h_name,0x00,30);
    LanComputerThread = new TLanComputerThread(&ret,&host[i],&iThreadCount);
  }      while(iThreadCount>0)
    Sleep((unsigned)1000);
  for(i=0;i<=255;i  )
  {
    if(strlen(host[i].h_name)>0)
    {
      NewItem=ListView1->Items->Add();
      NewItem->Caption=host[i].h_name;
      ListView1->LargeImages = ImageList1;
      ListView1->Refresh();
    }
    free(host[i].h_name);
  }
  WSACleanup();
}
 
Func.h 如下
#include 
//===========================================================================
// 以下程式碼為 TLanComputer Class 之宣告
// TLanComputer Class 主要用執行緒搜尋網段電腦
//===========================================================================
class TLanComputerThread : public TThread
{
  private:
        AnsiString ret;
        char ip_address[256];
        struct in_addr *myaddr;
        int *iCount;
        struct hostent *thost;
        struct hostent *tmphost;
  protected:
        void __fastcall Execute();
  public:
          __fastcall TLanComputerThread(AnsiString *aIP,hostent *host,int *iThreadCount);    };
 
Func.cpp 內容如下
 
#include <math.h>
#include 
#include 
#include "Iphlpapi.h"
#include "IdGlobal.hpp"
#include "Func.h"
//---------------------------------------------------------------------------
// 以下為 TLan 函式之程式碼
// 功能:用執行緒搜尋網段電腦
// 傳入參數:IP(型態 AnsiString)            EX: IP = 192.168.1.3;
//---------------------------------------------------------------------------    //---------------------------------------------------------------------------
__fastcall TLanComputerThread::TLanComputerThread(AnsiString *aIP,hostent *host,int *iThreadCount)
                               :TThread(aIP)
{
  ret = *aIP;
  tmphost =  host;
  iCount = iThreadCount;
  Resume();
}
//---------------------------------------------------------------------------
void __fastcall TLanComputerThread::Execute()
{
  AnsiString astmp;
  int iPcNameLen;
  lstrcpy(ip_address,ret.c_str());
  myaddr=(struct in_addr*) malloc(sizeof(struct in_addr));
  myaddr->s_addr=inet_addr(ip_address);
  thost=gethostbyaddr((char *)myaddr, 4, AF_INET);
  if(thost != NULL)
    strcpy(tmphost->h_name,thost->h_name);
  free(myaddr);
  *iCount=*iCount-1;
}
//---------------------------------------------------------------------------
以上請您試試看吧,有問題再討論看看
真的可以也!不過可不可以解釋一下上面紅色字在做什麼?
wu-eric
中階會員


發表:14
回覆:82
積分:64
註冊:2004-03-17

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-05-10 16:40:23 IP:210.66.xxx.xxx 未訂閱
host[i].h_name = (char *)malloc(30); ==> 規劃一個30byte的空間給它用 memset(host[i].h_name,0x00,30); ==>將這個空間內容設為 0x00 LanComputerThread = new TLanComputerThread(&ret,&host[i],&iThreadCount); ==> 呼叫多執行緒
cashyy
高階會員


發表:117
回覆:322
積分:212
註冊:2004-04-30

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-05-10 16:48:32 IP:59.104.xxx.xxx 未訂閱
引言: host[i].h_name = (char *)malloc(30); ==> 規劃一個30byte的空間給它用 memset(host[i].h_name,0x00,30); ==>將這個空間內容設為 0x00 LanComputerThread = new TLanComputerThread(&ret,&host[i],&iThreadCount); ==> 呼叫多執行緒
感謝各位大大回應,可惜只能給一分!希望各位大大見諒!
SuperRock
一般會員


發表:5
回覆:13
積分:3
註冊:2004-05-20

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-07-21 14:16:51 IP:61.218.xxx.xxx 未訂閱
我照著做,但是出現底下error,但是始終找不到'IDGLOBAL.HPP'這個檔案 [C Error] func.cpp(5): E2209 Unable to open include file 'IDGLOBAL.HPP' 我mark掉//#include "IdGlobal.hpp" ,卻出現底下error,能否幫我解決,thanks!! [C Error] func.cpp(15): E2285 Could not find a match for 'TThread::TThread(AnsiString *)'
系統時間:2024-05-06 8:15:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!