0

DSP send raw ethernet packets to PC than PC capturing these packets and send ack response. They are messaging MAC to MAC so there are no ip layer. I want to make real-time messaging in every 1ms. DSP send message every 1 milisecond but PC can not capture the messages and send reply under 1 milisecond. Capturing and sending packet takes 15 - 30 ms. This result too slow for me. How can I make this faster. I am using jpcap library and my operating system 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);
        }

    }
Leo Eroz
  • 13
  • 4

2 Answers2

0

"Capturing and sending packet takes 15 - 30 ms." -- This is normal. You cannot make this much faster. Windows is not a real-time operating system. Use a microcontroller instead (there are better options, like FPGA and ARM, but I have no experience with these).

kol
  • 27,881
  • 12
  • 83
  • 120
  • kol ok but how wireshark can capture lots of different packets microsecond time level? Do you have an idea? – Leo Eroz Jun 11 '12 at 23:56
  • The interval between captured packets can easily be around microseconds. But capturing a packet *and* sending a reply can never be that fast. There are lots of processes and lots of threads running concurrently on Windows, and each gets its time slice. That's why your app's accuracy cannot be better than 15-30 ms. If you don't believe me, you can easily reimplement your code in C (use QueryPerformanceCounter for timing and time measurement) -- the result won't be much better. – kol Jun 12 '12 at 05:14
0

Use a libpcap filter, which you can set via JPcap, instead of your Java filter. That way non-matching packets won't even be captured, let alone cause callbacks into JPcap and then your Java code.

Also use a much bigger buffer than 100.

user207421
  • 305,947
  • 44
  • 307
  • 483