首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ubuntu上的Jnetpcap

ubuntu上的Jnetpcap
EN

Stack Overflow用户
提问于 2012-12-06 19:54:11
回答 2查看 1.7K关注 0票数 0

好了,这就是我的问题所在,我正在尝试让jnetpcap在ubuntu中工作,但我遇到了一些问题。我尝试使用jnetpcap网站上的说明进行安装,可以将jar添加到构建路径,但我似乎无法在Eclipse上运行任何程序。我的代码如下。我收到的错误消息是“找不到任何设备,错误是”。除了将jar添加到构建路径之外,我在安装时还应该做些什么,或者这是另一个问题吗?

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;


public class apples {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    List<PcapIf> devices = new ArrayList<PcapIf>();
    StringBuilder error = new StringBuilder();

    int x = Pcap.findAllDevs(devices, error);

    if(x==Pcap.NOT_OK||devices.isEmpty()){
        System.err.printf("Can't find any devices, error is %s", error.toString());
        return;
    }

    System.out.println("Network devices found");

    PcapIf device = devices.get(0);

    int snaplen = 64*1024;
    int flags = Pcap.MODE_PROMISCUOUS;
    int timeout = 10*1000;
    Pcap open = Pcap.openLive(device.getName(), snaplen, flags, timeout, error);

    if(open==null){
        System.err.printf("Error while opening device for capture:"+error.toString());
        return;
    }
    PcapPacketHandler<String> printSummaryHandler = new PcapPacketHandler<String>(){

        public void nextPacket(PcapPacket packet, String user){
            Date timestamp = new Date(packet.getCaptureHeader().timestampInMillis());
            int caplen = packet.getCaptureHeader().caplen();
            int len = packet.getCaptureHeader().wirelen();

            System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n", timestamp.toString(), caplen, len, user);
        }
    };

    open.loop(10, printSummaryHandler, "Message!" );

    open.close();
}

}

EN

回答 2

Stack Overflow用户

发布于 2013-08-22 18:00:02

错误可能与jar或.so文件有关。我相信,您必须在eclipse中手动加载jar,并将共享目标文件放入项目中。希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2014-06-13 19:01:37

请先以root身份运行eclipse来运行您的代码。我曾经在这个库中遇到过同样的问题,当我以root身份运行eclipse时,它得到了修复。您可以打开终端并键入以下命令来运行eclipse:

代码语言:javascript
复制
                       sudo /your/eclipse/directory/eclipse
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13743082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档