在eclipse中配置JnetPCAP,Windows环境

来源:互联网 发布:协同过滤属于什么算法 编辑:程序博客网 时间:2024/05/22 05:00

配置主要参考的是这个链接:

参考1:jnetpcap在Win8.1上的配置:http://blog.csdn.net/u012968045/article/details/46623985

参考2:java程序引用jNetPcap抓包的方法:http://www.6san.com/1136/

 

jNetPcap官网:http://www.jnetpcap.com/

winpcap官网:http://www.winpcap.org/

 

1、  首先安装winpcap/libcap,windows系统安装winpcap,unix为基础的系统安装libcap。

2、  到http://jnetpcap.com/(jnetpcap的官网)上下载jnetpcap,我下载的是:

jnetpcap-1.3.0,解压之后会有两个重要的文件,jnetpcap.dll和jnetpcap.jar

3、  将jnetpcap.dll复制到下面的任意一个目录(当时以防万一,所有目录下面都拷了一份):

i) c:\Windows或c:\Windows\System32

ii) ecllipse或myecllipse所用的jre的bin目录

在eclipse中通过:Windows-->Preference-->Java-->InstalledJREs找到jre 的目录。


4、  接下来,把jnetpcap.jar放在jre安装目录的lib/ext文件夹中。

5、  此时在eclipse中创建工程后,将参考二中的demo代码粘过去,发现程序中还是有错误,jnetpcap中的文件没有引进来,所以又参考了参考2中的方法:

通过Add External JARs添加jnetpcap.jar到工程中,代码终于没有红线了。

运行代码之后,发现程序没有报错,但是发现一直找不到设备:


代码直接进入这个if判断里面,找不到任何设备。调试发现返回值r=0; alldevs 是空的,errbuf中没有任何错误信息。

后来通过查找资料发现函数findAllDevs()返回值是0,表示success,返回值是-1,表示failure,如果没有找到任何设备,也是返回0,认为是success的。如果返回-1,那么errbuf中会有错误信息。英文原文是:

pcap_findalldevs() returns 0 on success and-1 on failure; as indicated, finding no devices is considered success, ratherthan failure, so 0 will be returned in that case. If -1 is returned, errbuf isfilled in with an appropriate error message. errbuf is assumed to be able tohold at least PCAP_ERRBUF_SIZE chars. 

后来又找到了pcap_findalldevs() 的一个解释:

pcap_findalldevs() constructs a list ofnetwork devices that can be opened withpcap_create()and pcap_activate() or with pcap_open_live(). (Note that there may benetwork devices that cannot be opened by the process callingpcap_findalldevs(), because, for example, that process

doesnot have sufficient privileges to open them forcapturing; if so, those devices will not appear on the list.)

于是猜想可能使程序运行的时候没有权限,于是就采用了“以管理员的身份打开eclipse”的方法,demo程序终于运行成功了。


 

0 0