Qt 5 配置 WinPcap 开发环境

时间:2018-04-24 21:56:41   收藏:0   阅读:1602

第一步:下载必要的工具。

第二步:配置开发环境,有两种方式。

第三步:编写程序并运行,在头文件中添加以下内容:

#define HAVE_REMOTE
#include <pcap.h>
#include <remote-ext.h>

这里提供一份测试代码。

#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();
}

运行结果如下图:
技术分享图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!