Step 1: Download the necessary tools.
Download the WinPcap program, unzip it and install it.
Download the WpdPack toolkit. After decompression, the folder contains
Include
andLib
two subfolders.
Step 2: Configure the development environment, there are two ways.
The first one:
create a newWpdPack
folder , and then copy theInclude
andLib
two folders to this folder. Add
the following to the project file.pro
INCLUDEPATH += C:\WpdPack\Include LIBS += C:/WpdPack/Lib/wpcap.lib
Note : The location of the folder here can be freely selected, as long as the file paths in the project file are consistent.
The second: copy all the contents of the folder
to the directory Copy all the contents of the folder to the directory Then just add to the project fileInclude
C:\Qt\Qt5.6.1\5.6\mingw49_32\include
Lib
C:\Qt\Qt5.6.1\5.6\mingw49_32\lib
.pro
LIBS += wpcap.lib
Step 3: Write the program and run it, add the following to the header file:
#define HAVE_REMOTE
#include <pcap.h>
#include <remote-ext.h>
Here is a test code.
#include <QCoreApplication>
#include <QDebug>
#define HAVE_REMOTE
#include <pcap.h>
#include <remote-ext.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list from the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */,
&alldevs, errbuf) == -1) {
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}
/* Print the list */
pcap_if_t *d;
int i=0;
for(d= alldevs; d != NULL; 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");
} else {
/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
}
return a.exec();
}
The running result is as follows: