全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2854
推到 Plurk!
推到 Facebook!

關於WinPcap封包抓取範例的執行問題

答題得分者是:ikk
typer486
一般會員


發表:9
回覆:16
積分:5
註冊:2008-11-26

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-02-16 09:37:15 IP:220.130.xxx.xxx 訂閱

[code cpp]
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include

#include
#define LINE_LEN 16
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"packet.lib")
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
pcap_if_t *alldevs, *d;
pcap_t *fp;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
int res;
struct pcap_pkthdr *header;
const u_char *pkt_data;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}

/* Print the list */
for(d=alldevs; d; d=d->next)
{
if (d->description)
ComboBox1->Items->Add(IntToStr( i) ". " (d->description));
else
ComboBox1->Items->Add(" (No description available)\n");
}

if (i==0)
{
ComboBox1->Items->Add("No interfaces found! Make sure WinPcap is installed.\n");
return ;
}
ComboBox1->ItemIndex=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (d=alldevs, i=0; i< ComboBox1->ItemIndex ;d=d->next, i );

/* Open the adapter */
if ((fp = pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nError opening adapter\n");
return ;
}
while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
{

if(res == 0)
/* Timeout elapsed */
continue;

/* print pkt timestamp and pkt len */
Memo1->Lines->Add((header->ts.tv_sec) (header->ts.tv_usec) (header->len));

/* Print the packet */
for (i=1; (i < header->caplen 1 ) ; i )
{
Memo1->Text = Memo1->Text pkt_data[i-1];
if ( (i % LINE_LEN) == 0)
Memo1->Lines->Add("\n");
}

Memo1->Lines->Add("\n\n");
}
if(res == -1)
{
Memo1->Lines->Add("Error reading the packets: ");
Memo1->Lines->Add(pcap_geterr(fp));
return ;
}

pcap_close(fp);
return;
}
//---------------------------------------------------------------------------

[/code]

請教各位先進~小弟將WinPcap的範例用Memo的元件顯示封包數據為何程式執行時CPU使用率飆100%,可是範例在DOS下執行卻沒這問題?謝謝
ikk
尊榮會員


發表:4
回覆:413
積分:768
註冊:2003-06-30

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-02-20 10:00:09 IP:163.28.xxx.xxx 訂閱
CPU使用率飆100%, 是有執行完畢還是block住?
------
FPGA驗證, FPGA開發平台, http://smims.com
typer486
一般會員


發表:9
回覆:16
積分:5
註冊:2008-11-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-02-20 10:52:16 IP:220.130.xxx.xxx 訂閱
是在等待顯示封包的執行過程中程式會當在那裏,可是執行完畢之後就會恢復正常。
===================引 用 ikk 文 章===================
CPU使用率飆100%, 是有執行完畢還是block住?
ikk
尊榮會員


發表:4
回覆:413
積分:768
註冊:2003-06-30

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-02-20 10:58:42 IP:163.28.xxx.xxx 訂閱
那是因為訊息一直進入Memo, 所以Memo的UI一直refresh.
會感覺一直block住....
好一點的處理方式, 是先記錄一段, 在放進Memo
------
FPGA驗證, FPGA開發平台, http://smims.com
typer486
一般會員


發表:9
回覆:16
積分:5
註冊:2008-11-26

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-02-20 11:26:19 IP:220.130.xxx.xxx 訂閱
ikk大~請問先記錄一段, 在放進Memo意思是先把訊息存到外部的檔案再從外部讀回Memo當中嗎?小弟剛進這行才快兩個月還很嫩,請不吝指教...謝謝

===================引 用 ikk 文 章===================
那是因為訊息一直進入Memo, 所以Memo的UI一直refresh.
會感覺一直block住....
好一點的處理方式, 是先記錄一段, 在放進Memo
ikk
尊榮會員


發表:4
回覆:413
積分:768
註冊:2003-06-30

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-02-20 11:34:47 IP:163.28.xxx.xxx 訂閱
放在記憶體就可以了...
------
FPGA驗證, FPGA開發平台, http://smims.com
系統時間:2024-04-27 4:41:09
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!