Use a third-party Java library to obtain the max address

Source: Internet
Author: User

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 ());

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.