libnet进行编程时,libnet_init()无法找到网卡的解决方法。

来源:互联网 发布:网络机顶盒电视猫 编辑:程序博客网 时间:2024/06/04 18:15

本文为转载,本人的系统是Win8 64Bit + VisualStudio2010

我也遇到同样的问题,两台电脑中在比较早的电脑运行不了,程序无法自动找到网卡接口。

这种情况下,不能在libnet_init()时参数device就不能设置成NULL了,需要自己去先找到device。
在程序中加入以下一段代码试试……
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];

    /* Retrieve the device list */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    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);
cin.get();
    
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
cout << "press ENTER to Exit";
cin.get();
        return -1;
    }

    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);



char *device = NULL;//设备名字,此时为NULL

device=d->name;
l=libnet_init(LIBNET_LINK,device,error_inf);
这样就可以自己找到网卡接口了,注意选择网卡接口的时候选择当前上网的所用的网络接口。
0 0
原创粉丝点击