The identification example of Java network programming get Host network interface list _java

Source: Internet
Author: User


Get host address information

In Java we use the InetAddress class to represent the destination network address, including the host name and numeric type address information, and the InetAddress instance is immutable, and each instance always points to an address. The InetAddress class contains two subclasses, corresponding to the two version of the IP address:

Copy Code code as follows:

Inet4address
Inet6address

We know from the previous notes that the IP address is actually assigned to the connection between the host and the network, rather than the host itself, the NetworkInterface class provides the ability to access information about all the interfaces of the host. Here's a simple example program to learn how to get address information for a network host:

Copy Code code as follows:

importjava.net.*;
Importjava.util.Enumeration;

publicclassinetaddressexample{

Publicstaticvoidmain (String[]args) {
Todoauto-generatedmethodstub
try{
Get a list of host network interfaces
Enumeration<networkinterface>interfacelist=networkinterface
. Getnetworkinterfaces ();
Detects if the interface list is empty, and the loopback interface (loopback) should exist even if the host does not have any other network connections
if (interfacelist==null) {
System.out.println ("--no interface found--");
}else{
while (Interfacelist.hasmoreelements ()) {
Get and print the address of each interface
Networkinterfaceiface=interfacelist.nextelement ();
Print Interface Name
System.out.println ("Interface" +iface.getname () + ";");
Gets the address associated with the interface
Enumeration<inetaddress>addresslist=iface
. getinetaddresses ();
is empty
if (!addresslist.hasmoreelements ()) {
System.out.println ("t (no address associated with this interface)");
}
Iteration of the list, print out each address
while (Addresslist.hasmoreelements ()) {
Inetaddressaddress=addresslist.nextelement ();
System.out
. Print ("\taddress"
+ ((addressinstanceofinet4address?) (v4) "
: Addressinstanceofinet6address? " V6 "
:"(?)")));
System.out.println (":" +address.gethostaddress ());
}
}
}
}catch (socketexceptionse) {
SYSTEM.OUT.PRINTLN ("Get Network Interface Error:" +se.getmessage ());
}
Gets the host name and address of each parameter entered from the command line, iterates over the list, and prints
for (Stringhost:args) {
try{
System.out.println (host+ ":");
Inetaddress[]addresslist=inetaddress.getallbyname (host);
for (inetaddressaddress:addresslist) {
System.out.println ("T" +address.gethostname () + "/"
+address.gethostaddress ());
}
}catch (Unknownhostexceptione) {
System.out.println ("\ t cannot find address:" +host);
}
}
}
}

Related Article

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.