关于WinPcap封包抓取范例的执行问题

typer486 typer486 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdlib.h>
#include <stdio.h>
#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->deforbiddenion)
ComboBox1->Items->Add(IntToStr(++i)+ ". " + (d->deforbiddenion));
else
ComboBox1->Items->Add(" (No deforbiddenion 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;
}
//---------------------------------------------------------------------------

请教各位先进~小弟将WinPcap的范例用Memo的元件显示封包数据为何程序执行时CPU使用率飙100%,可是范例在DOS下执行却没这问题?谢谢
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
typer486
网络及通讯开发 1,317 成员 8,874 内容