在網上查了不少資料,搞了差很少一天總算解決Qt5使用winPcap配置的問題了!記錄一下 以便後續忘記api
一、下載winpcap4.1.3,百度便可搜索到函數
二、下載winpCap開發者工具包http://www.winpcap.org/devel.htm 工具
三、新建Qt控制檯工程測試
四、在.pro文件中添加以下代碼 主要是配置頭文件路徑和庫路徑 因爲我是放在C盤的,因此路徑以下 路徑能夠本身定義spa
INCLUDEPATH+=C:/WpdPack_4_1_2/WpdPack/Include
LIBS+=C:/WpdPack_4_1_2/WpdPack/Lib/x64/wpcap.lib Packet.lib
LIBS+=C:\WpdPack_4_1_2\WpdPack\Lib\libwpcap.a libpacket.a
以前沒有配置好路徑 一直出現lnk2019的錯誤
注意:必定是x64下的lib文件 由於下載的開發者包在lib的路徑下有
補充:其實能夠將下載下來的winpcap開發者包的include和lib文件夾下面的全部內容複製到Qt安裝目錄下的include和lib文件夾下,在.pro文件中 只需添加以下代碼便可
LIBS+=wpcap.lib Packet.lib
LIBS+=libwpcap.a libpacket.a//通過測試 這句無關緊要
五、在CPP中添加以下代碼
#include <QCoreApplication>
#include<QDebug>
#include"pcap.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs(&alldevs,errbuf)==-1)
{
qDebug()<<errbuf;
}
for(d=alldevs;d;d=d->next)
{
qDebug()<<++i<<d->name;
if(d->description)
qDebug()<<d->description;
else
qDebug()<<"No description available ";
}
if(0==i)
{
qDebug()<<"No interfaces found!Make sure WinPcap is installed";
}
pcap_freealldevs(alldevs);
return a.exec();
}
六、截圖
七、提示
a.Qt Creator的編譯器很差!我在使用時先去刪掉以前編譯的debug文件,在使用絕對路徑 在使用
LIBS+=wpcap.lib Packet.lib(這是將winpcap的include和lib文件夾下的內容,Qt目錄下的的include和lib中)
八、winpcap獲取制定是配置的包
代碼以下:
#include <QCoreApplication>
#include<QDebug>
#define HAVE_REMOTE
#include"pcap.h"
//#include<remote-ext.h>
#pragma comment(lib,"Iphlpapi.lib")
#define TRACE(s) OutputDebugString(TEXT(s));
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[16];
struct pcap_pkthdr *header;
const u_char *pkt_data;
time_t local_tv_sec;
/* 獲取本機設備列表 */
if (pcap_findalldevs/*_ex*/(/*PCAP_SRC_IF_STRING, NULL,*/ &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳轉到已選中的適配器 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打開設備 */
if ( (adhandle= pcap_open(d->name, // 設備名
65536, // 要捕捉的數據包的部分
// 65535保證能捕獲到不一樣數據鏈路層上的每一個數據包的所有內容
PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式
1000, // 讀取超時時間
NULL, // 遠程機器驗證
errbuf // 錯誤緩衝池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 釋放設列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
/* 獲取數據包 */
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
if(res == 0)
{
/* 超時時間到 */
TRACE("Read Packet TimeOut!!");
continue;
}
static double m_Packet_Count = 0;
static DWORD m_PacketsLen = 0; //包的長度
static DWORD m_TickCount = 0;
static double m_Speed = 0.0;
m_PacketsLen+=header->len;//接受包的總長度
m_Packet_Count ++;
if(GetTickCount() - m_TickCount > 1000)//每秒讀取計算一次。GetTickCount()返回的是毫秒數
{
m_Speed = m_PacketsLen/1000.0;//speed .單位kbps
m_TickCount = GetTickCount();//返回從啓動到當前通過的毫秒數
m_PacketsLen = 0;
printf("Packets:%.0f/s Speed:%.3f Kbps\r",m_Packet_Count,m_Speed);
m_Packet_Count = 0;
}
}
if(res == -1)
{
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
return -1;
}
return a.exec();
}
說明:須要添加以下內容 才能夠在調用時,使用pacp_open,不然只能使用其餘的函數去打開適配器。