How to send Ethernet-Frames in Java without TCP/IP

2019-04-24 13:46发布

问题:

My Java application should control an external device (EtherCAT Bus technology) directly connected to the network interface of my computer(Ubuntu and Windows). No other network devices are connected. The communication has do be done on Standard IEEE 802.3 Ethernet Frames without IP stack.

Example for sending data:

int etherType =  0x88A4;  // the EtherType registered by IEEE
byte[] macBroadcast = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
byte[] macSource = new byte[] ... ;  // MAC Address of my network interface card
byte[] buffer = ... // the data to send

device.write(macSource, macBroadcast, etherType, buffer);

I tried JNetPcap, which uses the pcap native library. The given API was fine, but there were multithreading issues on heavy load, which forced me to give up.

netty.io was also a candidate. I am not sure, but a TCP/IP stack is mandatory. Am I right?

Are there other ideas to communicate with low level Ethernet Frames? I would prefer a pure java library like netty.io, if one exists.

Of course JNA/JNI is an option, too. But I don't want to write C code.

Other alternatives?

回答1:

These are the options I was able to find:

  • jNetPcap is a libpcap wrapper for Java - http://jnetpcap.com/
  • Jpcap is another libpcap wrapper for Java - https://github.com/mgodave/Jpcap
  • RockSaw is a library for sending and receiving IPv4 and IPv6 packets using "raw sockets" - http://www.savarese.org/software/rocksaw/. From what I can tell it is pretty limited, and hasn't been touched since 2007.
  • Netutils uses libpcap and libnet - http://code.google.com/p/netutils/

I've also seen comments to the effect that jNetPcap is supposed to be thread-safe but that in practice it is not; i.e. it is buggy when used with multiple threads.