简体   繁体   English

如何使用jpcap加速数据包捕获和数据包发送

[英]How can I speed up packet capturing and packet send with jpcap

DSP send raw ethernet packets to PC than PC capturing these packets and send ack response. DSP将原始以太网数据包发送到PC,而不是PC捕获这些数据包并发送ack响应。 They are messaging MAC to MAC so there are no ip layer. 他们正在向MAC发送消息MAC,因此没有IP层。 I want to make real-time messaging in every 1ms. 我想每1毫秒进行一次实时消息传递。 DSP send message every 1 milisecond but PC can not capture the messages and send reply under 1 milisecond. DSP每1毫秒发送一次消息,但PC无法捕获消息并在1毫秒内发送答复。 Capturing and sending packet takes 15 - 30 ms. 捕获和发送数据包需要15到30毫秒。 This result too slow for me. 这个结果对我来说太慢了。 How can I make this faster. 我怎样才能使它更快。 I am using jpcap library and my operating system Win XP x32. 我正在使用jpcap库,并且我的操作系统是Win XP x32。

Capturing Code: 捕获代码:

    private void captor() {
                try {
                    captor = JpcapCaptor.openDevice(cf.getDevice(), 100, true, 1);
                } catch (Exception ex) {
                    Logger.getLogger(CapturingPacket.class.getName()).log(Level.SEVERE, null, ex);
                }
            }



    private void capturing() {
            Packet packet = captor.getPacket();
            if (packet != null) {
                if (packet.data.length > 0) {
                    EthernetPacket ethernetPacket = (EthernetPacket) packet.datalink;                
                    String receivedDestMac = Common.byteToMacStringFormat(ethernetPacket.src_mac);

                    if (definedDestMac.equals(receivedDestMac)) {
                        captured(packet.data);
                    }

                }
            }

}

private class captureRunner implements Runnable {

        public void run() {
            captor();
            while (running) {
                capturing();
                try {
                    Thread.sleep(0);
                } catch (InterruptedException err) {
                }
            }
        }
    }

Sending Code: 发送代码:

private void send(byte[] message) {
        try {

            JpcapSender send = JpcapSender.openDevice(cf.getDevice());

            Packet packet = new Packet();
            //ethernet frame
            EthernetPacket ethernetPacket = new EthernetPacket();

            // #dst_mac
            ethernetPacket.dst_mac = getDestMac();
            // #src_mac
            ethernetPacket.src_mac = cf.getDevice().mac_address;
            // #frametype
            ethernetPacket.frametype = Common.ETHERNET_FRAME_TYPE;
            // #data
            packet.data = message;
            // datalink
            packet.datalink = ethernetPacket;
            send.sendPacket(packet);
            send.close();

        } catch (Exception ex) {
            Common.showErrorMessage("Send Error");
            Logger.getLogger(MessagingPacket.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

"Capturing and sending packet takes 15 - 30 ms." “捕获和发送数据包需要15到30毫秒。” -- This is normal. - 这很正常。 You cannot make this much faster. 您不能使速度更快。 Windows is not a real-time operating system. Windows不是实时操作系统。 Use a microcontroller instead (there are better options, like FPGA and ARM, but I have no experience with these). 改用微控制器(有更好的选择,如FPGA和ARM,但我对这些没有经验)。

Use a libpcap filter, which you can set via JPcap, instead of your Java filter. 使用可以通过JPcap设置的libpcap过滤器代替Java过滤器。 That way non-matching packets won't even be captured, let alone cause callbacks into JPcap and then your Java code. 这样,不匹配的数据包甚至都不会被捕获,更不用说导致回调到JPcap以及您的Java代码了。

Also use a much bigger buffer than 100. 还使用比100大得多的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM