求教java高手,关于JPCAP的问题
我的winpcap和jpcap都装好了,但是从网上找了个测试程序importjpcap.JpcapHandler;importjpcap.Jpcap;importjpca...
我的winpcap和jpcap都装好了,但是从网上找了个测试程序
import jpcap.JpcapHandler;
import jpcap.Jpcap;
import jpcap.Packet;
public class JpcapTip implements JpcapHandler {
public void handlePacket(Packet packet){
System.out.println(packet);
}
public static void main(String[] args) throws java.io.IOException{
String[] devices = Jpcap.getDeviceList();
for (int i = 0; i < devices.length; i++) {
System.out.println(devices[i]);
}
String deviceName = devices[0];
Jpcap jpcap = Jpcap.openDevice(deviceName, 1028, false, 1);
jpcap.loopPacket(-1, new JpcapTip());
}
}
却显示import jpcap.JpcapHandler;
import jpcap.Jpcap;
import jpcap.Packet;这三行找不到符号。
我测试别的一些程序引入包为import jpcap.*;
import jpcap.packet.*;之类的都可以运行。
不知道为什么涉及到了具体的类就找不到,请高手讲解下。 展开
import jpcap.JpcapHandler;
import jpcap.Jpcap;
import jpcap.Packet;
public class JpcapTip implements JpcapHandler {
public void handlePacket(Packet packet){
System.out.println(packet);
}
public static void main(String[] args) throws java.io.IOException{
String[] devices = Jpcap.getDeviceList();
for (int i = 0; i < devices.length; i++) {
System.out.println(devices[i]);
}
String deviceName = devices[0];
Jpcap jpcap = Jpcap.openDevice(deviceName, 1028, false, 1);
jpcap.loopPacket(-1, new JpcapTip());
}
}
却显示import jpcap.JpcapHandler;
import jpcap.Jpcap;
import jpcap.Packet;这三行找不到符号。
我测试别的一些程序引入包为import jpcap.*;
import jpcap.packet.*;之类的都可以运行。
不知道为什么涉及到了具体的类就找不到,请高手讲解下。 展开
2个回答
展开全部
估计你下的是新版jpcap
代码不一样了
用这个试试吧:
package com.yaowei.jpcap;
import jpcap.*;
import jpcap.packet.*;
public class Jpcap {
private NetworkInterface [] devices;
private NetworkInterface device;
private JpcapCaptor jpcap;
private JpcapSender sender;
private NetworkInterface getDevice() throws Exception{
devices = JpcapCaptor.getDeviceList();
device = devices[0];
jpcap = JpcapCaptor.openDevice(device, 2000, false, 10000);
jpcap.setFilter("ip", true);
//sender = jpcap.getJpcapSenderInstance();
jpcap.loopPacket(-1, new PacketReceiverImpl());
return device;
}
public Jpcap(){
try{
this.getDevice();
}catch(Exception e){
e.printStackTrace();
}
//getDevice();
}
public static void main(String []args){
Jpcap j = new Jpcap();
}
}
class PacketReceiverImpl implements PacketReceiver{
public void receivePacket(Packet packet) {
try{
System.out.println(new String(packet.data,"gb2312"));
}catch(Exception e){
e.printStackTrace();
}
}
}
代码不一样了
用这个试试吧:
package com.yaowei.jpcap;
import jpcap.*;
import jpcap.packet.*;
public class Jpcap {
private NetworkInterface [] devices;
private NetworkInterface device;
private JpcapCaptor jpcap;
private JpcapSender sender;
private NetworkInterface getDevice() throws Exception{
devices = JpcapCaptor.getDeviceList();
device = devices[0];
jpcap = JpcapCaptor.openDevice(device, 2000, false, 10000);
jpcap.setFilter("ip", true);
//sender = jpcap.getJpcapSenderInstance();
jpcap.loopPacket(-1, new PacketReceiverImpl());
return device;
}
public Jpcap(){
try{
this.getDevice();
}catch(Exception e){
e.printStackTrace();
}
//getDevice();
}
public static void main(String []args){
Jpcap j = new Jpcap();
}
}
class PacketReceiverImpl implements PacketReceiver{
public void receivePacket(Packet packet) {
try{
System.out.println(new String(packet.data,"gb2312"));
}catch(Exception e){
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询