1.下載winpcap及開發包。
- winpcap下載地址:http://www.winpcap.org/install/default.htm
- winpcap開發包: http://www.winpcap.org/devel.htm
2. 安裝winpcap
3. 解壓winpcap開發包,將得到一個WpdPack目錄,該目錄中包含了5個子目錄:docs、Examples-pcap、Examples-remote、Include和Lib。
- docs中是winpcap的幫助文檔,比較通俗易懂。
- Examples的是一些例子
- include和lib則分別爲C++項目的頭文件和鏈接庫
4.配置上inlucde和lib引用
5.在進行編程前還要注意一點,找到pcap.h文件,打開它,在#include <pcap/pcap.h>這句之前加上#define HAVE_REMOTE ,否則可能會報錯說上面的結構體和函數都沒有定義,看到網上有人說要加入#include "remote-ext.h" ,但其實加上以後會出現#error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h 意思是說不要包含這個頭文件,而要定義HAVE_REMOTE宏
6.
#include "stdio.h"
{
pcap_if_t *alldevs; //define the list of the Network adapter device
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE]; //PCAP_ERRBUF_SIZE is 256
/*Get the list of the device in loacal host*/
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf))
{
fprintf(stderr,"Error in pcap_findalldevs_ex",errbuf); /
exit(1);
}
for(d=alldevs;d;d=d->next)
{
printf("%d.%s/t",++i,d->name);
if(d->description)
{
printf("%s/n",d->description);
}
else
{
printf("No description available");
}
}
{
printf("No interface found!! Make sure Winpcap is installed/n");
return -1;
}
/*release the device*/
pcap_freealldevs(alldevs);
return 0;
}