It seems difficult to obtain Mac information in Java.
But I saw a Java library, and it feels pretty good. Write it down first. Who has studied how to give me a speech and promote each other's progress ;)
1) third-party Java library. (Try this: http://netresearch.ics.uci.edu/kfujii/jpcap/doc/index.html. If raw socket is supported in Java, the problem can be solved)
======================
If you want to capture network packages in Java programs, you need some auxiliary tools because the core Java APIs cannot access the underlying network data. However, jpcap is a Java API that provides such access on Windows or UNIX systems.
Jpcap is not a pure Java solution; it depends on the use of local libraries. On Windows or UNIX, you must use a third-party library, Winpcap or Libpcap.
How jpcap works
Jpcap uses an event model for you to process packets. First, you must create a class that executes the jpcap. jpcaphandler interface.
Public class jpcaptip implements jpcaphandler {
Public void handlepacket (packet ){
System. Out. println (packet );
}
}
To capture packets, you need to tell jpcap which network device you want to use to listen. The API provides the jpcap. jpcap. Getdevicelist () method for this purpose. This method returns a string, which can be used as follows:
String [] devices = jpcap. Getdevicelist ();
Once you have a directory with the device name, you must select one for listening:
String devicename = devices [0];
Select a device and open it through jpcap. opendevice. The opendevice () method requires four parameters: the name of the device to be opened, the maximum number of bytes read from the device last time, indicating whether to set the device to a Boolean value in the mixed mode, and the timeout value to be used to call the processpacket () method later.
Jpcapjpcap = jpcap. opendevice (devicename, 1028, false, 10000 );
The opendevice () method returns a parameter to the jpcap object to be captured. Now that you have a jpcap instance, you can call processpacket () or looppacket () to start listening. Both methods have two parameters: the maximum number of captured packets can be-1 (there is no limit); an instance of a class that executes jpcaphandler.
If you call processpacket (), jpcap will capture the package until it exceeds the time limit specified in opendevice or reaches the maximum number of packets. Looppacket () will capture the package until it reaches the maximum number of packages. If there is no maximum number limit, it will run forever. It is called as follows:
Jpcap. looppacket (-1, new jpcaptip ());