关于WinPcap封包抓取范例的执行问题
//---------------------------------------------------------------------------
#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下执行却没这问题?谢谢
#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下执行却没这问题?谢谢
...全文
相关推荐
网络封包强力抓取工具WinPcap4.1.3
网络封包强力抓取工具WinPcap4.1.3
基于Winpcap实现抓取数据包
在vs2010上做的一个计算机网路课设,实现抓取数据包,并重点对TCP协议进行分析,附有详细报告
进程封包抓取
可以绕过某些检测机制抓取进程封包
WPE封包拦截工具
适合抓取网络数据,解析网络封包格式,小巧易用,入手较快
WinPcap 网络封包抓取
WinPcap 网络封包抓取(包含示例Demo) 1.捕获原始数据包,包括在共享网络上各主机发送/接收的以及相互之间交换的数据包; 2.在数据包发往应用程序之前,按照自定义的规则将某些特殊的数据包过滤掉; 3.在网络上发送原始的数据包; 4.收集网络通信过程中的统计信息。


网络及通讯开发
