#include <pcap.h>void main()
{
char error_content[PCAP_ERRBUF_SIZE]; //存储错误信息
struct in_addr net_ip_address; //网络地址
struct in_addr net_mask_address; //掩码地址
char *net_interface; //网络接口
char *net_ip_string; //网络地址
char *net_mask_string; //网络掩码
u_int32_t net_ip;
u_int32_t net_mask; net_interface=pcap_lookupdev(error_content); //获得可用网络接口
pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content);
//获得网络接口 的网络地址和网络掩码
net_ip_address.s_addr=net_ip;
net_ip_string=inet_ntoa(net_ip_address);
printf("网络地址:%s\n",net_ip_string);
net_mask_address.s_addr=net_mask;
net_mask_string=inet_ntoa(net_mask_address);
printf("网络掩码:%s\n",net_mask_string);
}上面的代码按理能得到本机的IP和网卡的mac,但是运行的结果却是得不到,这是为什么?我用的是wpcap3.0sdk,vc6.0编译,错在什么地方了?

解决方案 »

  1.   

    并不是所有问题都能指望别人帮你解决…………
    好好看一下winpcap附带的教学文档。
    我估计,pcap_lookupdev返回的第一个网络设备并不是你的网卡,而是系统用于拨号的设备,当然就无法得到你想得到的网卡的IP地址信息了。你可以用pcap_freealldevs()进行枚举,选择适当的网络设备。这些教程上都有。
    另外,winpcap已经有3.1或者更新的了
    char* pcap_lookupdev  (  char *  errbuf   )   
     
       Return the first valid device in the system. 
    Deprecated:
    Use pcap_findalldevs() or pcap_findalldevs_ex() instead. 
    pcap_lookupdev() returns a pointer to a network device suitable for use with pcap_open_live() and pcap_lookupnet(). If there is an error, NULL is returned and errbuf is filled in with an appropriate error message.