Java method for obtaining IP addresses and network interfaces, javaip

Source: Internet
Author: User

Java method for obtaining IP addresses and network interfaces, javaip

Java.net package

Everyone should know that the network-related objects are in the java.net package, and the classes under the Java net package are as follows:

1. Get the Host Object InetAddress

// Obtain the local host object InetAddress host = InetAddress. getLocalHost (); // obtain the host Object Based on the IP address or host name. When obtaining the host from the host name, DNS resolution is required for InetAddress host = InetAddress. getByName ("192.168.100.124"); InetAddress host = InetAddress. getByName (www.baidu.com );

2. Obtain the IP address and Host Name of the Host Object (dns resolution is required)

host.getHostAddress();host.getHostName();

3. obtain all the interfaces of the Local Machine NetworkInterface and traverse

// The returned data type is EnumerationEnumeration <NetworkInterface> enu = NetworkInterface. getNetworkInterfaces (); while (enu. hasMoreElements) {NetworkInterface inet = enu. nextElement (); String intName = inet. getName ();}

Because an interface may have multiple sub-interfaces (secondary ip addresses, such as eth0: 1, you can obtain all IP address enumeration sets for this interface (including both Ipv4 and ipv6 interfaces ).

Enumeration<InetAddress> net_list = inet.getInetAddresses();while(net_list.hasMoreElements){ InetAddress net = net_list.nextElement(); String ip = net.getHostAddress();}

You can use the Collections. list () method to convert the Enumeration type to the data structure of the ArrayList set, and then use the Itreator traversal tool to traverse.

The following describes how to obtain the names of all local interfaces and the ipv4 addresses on these interfaces (applicable to Windows and Linux ).

Import java.net. *; import java. util. *; public class EnumDemo {public static void main (String [] args) {try {// get all interfaces, put them in the enumeration set, and then use Collections. list () converts an Enumeration set to an ArrayList set Enumeration <NetworkInterface> enu = NetworkInterface. getNetworkInterfaces (); ArrayList <NetworkInterface> arr = Collections. list (enu); for (Iterator <NetworkInterface> it = arr. iterator (); it. hasNext ();) {NetworkInterface ni = it. next (); String intName = ni. getName (); // obtain the interface name // obtain all the IP network interfaces in each interface, because there may be sub-interfaces ArrayList <InetAddress> inets = Collections. list (ni. getInetAddresses (); for (Iterator <InetAddress> it1 = inets. iterator (); it1.hasNext ();) {InetAddress inet = it1.next (); // filter only ipv4 addresses; otherwise, the Ipv6 address if (inet instanceof Inet4Address) is obtained simultaneously) {String ip = inet. getHostAddress (); System. out. printf ("%-10 s %-5 s %-6 s %-15s \ n", "InetfaceName:", intName, "| IPv4 :", ip) ;}}} catch (SocketException s) {s. printStackTrace ();}}}

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.