Java network programming from getting started to mastering (11): Using the NetworkInterface class to obtain network interface information

Source: Internet
Author: User

Starting with JDK1.4, Java provides a NetworkInterface class. This class can get information about all the physical network interfaces of the machine and the logical network interfaces that the virtual machines and other software use to create the physical network interface of the machine.

First, Create NetworkInterface two static methods for an object

The Networkinerface class, like InetAddress, does not have a public construction method. Therefore, you must create a NetworkInterface object through its two static methods. There are two ways to create a NetworkInterface object: The network Interface Name (Getbyname method) and the IP address (getbyinetaddress method).

1. Getbyname Method

This method can be used to create a NetworkInterface object through the network interface name . This network interface name is not the computer name, but the name used to identify the physical or logical network interface, typically set by the operating system. The network interface name on most operating systems (including Windows, Linux, and Unix) begins with the ETH, followed by the index number of the network interface, starting with 0. If the machine is three LAN, then the network interface name is eth0, eth1 and eth2 in turn. The ToString method of the NetworkInterface object can return information such as the name of the network interface, the display name, and all IP addresses that are bound to the network interface. Getbyname returns NULL when the network interface name does not exist. The Getbyname method is defined as follows:

public static NetworkInterface Getbyname (String name) throws SocketException

The following code is a program that displays information about the specified network interface, and the network interface name is passed in through a command line parameter.

Package mynet;

Import java.net.*;

public class MyNetworkInterface1
{
public static void Main (string[] args) throws Exception
{
if (Args.length = = 0)
Return
NetworkInterface ni = Networkinterface.getbyname (args[0]);
System.out.println (NI = = null)? "Network interface does not exist!": NI);
}
}

    • Test 1

Execute the following command:

Java mynet. MyNetworkInterface1 eth0

Operation Result:

Name:eth0 (Realtek RTL8139 Family PCI Fast Ethernet NIC) Index:4 addresses:
/192.168.18.10;
/192.168.18.20;
    • Test 2

Execute the following command:

Java mynet. MyNetworkInterface1 ABCD

Operation Result:

The network interface does not exist!

2. Getbyinetaddress Method

In addition to using the network interface name to obtain information about the network interface, you can also use the Getbyinetaddress method to determine which network interface an IP address belongs to. Because the Getbyinetaddress method must use an IP address encapsulated by a InetAddress object as a parameter, you must first create a InetAddress object before using the Getbyinetaddress method. Note, however, that you cannot use the domain name of the remote IP to create the InetAddress object, otherwise getbyinetaddress will return null. The Getbyinetaddress method is defined as follows:

public static NetworkInterface getbyinetaddress (InetAddress addr) throws SocketException

The following code determines which network interface an IP address belongs to, and this IP address is passed in by command-line arguments.

Package mynet;

Import java.net.*;

public class MyNetworkInterface2
{

public static void Main (string[] args) throws Exception
{
if (Args.length = = 0) return;
InetAddress local = Inetaddress.getbyname (Args[0]);
NetworkInterface ni = networkinterface.getbyinetaddress (local);
System.out.println (NI = = null)? "This IP address does not exist in this machine!": NI);
}
}

    • Test 1

Execute the following command:

Java mynet. MyNetworkInterface2 127.0.0.1

Operation Result:

Name:lo (MS TCP Loopback Interface) Index:1 addresses:
/127.0.0.1;
/0:0:0:0:0:0:0:1;
    • Test 2

Execute the following command:

Java mynet. MyNetworkInterface2 218.61.151.22

Operation Result:

Name:ppp0 (WAN (ppp/slip) Interface) index:0 addresses:
/218.61.151.22;

Test 2 uses the IP address 218.61.151.22 is the ADSL connection temporarily assigned to the local IP address, therefore, the running result returned by the PPP0 is the ADSL network interface.

Second, Get all the network interfaces of the machine

NetworkInterface can enumerate all of the network interfaces of the machine by means of the Getnetworkinterfaces method . We can also enumerate all IP addresses of this machine using the network interface that Getnetworkinterfaces gets. Of course, you can also get all the IP addresses of this machine through the getallbyname of the InetAddress class. However, the Getnetworkinterfaces method can group these IP addresses by network interface, which is useful for just trying to get all the IP addresses on a network interface. The Getnetworkinterfaces method is defined as follows:

public static enumeration<networkinterface> Getnetworkinterfaces () throws SocketException

The following code shows how to use the Getnetworkinterfaces method to get all the network interfaces of the native computer.

Package mynet;

Import java.net.*;
Import java.util.*;

public class MyNetworkInterface3
{
public static void Main (string[] args) throws Exception
{
enumeration<networkinterface> NIS = networkinterface.getnetworkinterfaces ();
while (Nis.hasmoreelements ())
System.out.println (Nis.nextelement ());
}
}

Run results (partial):

Name:lo (MS TCP Loopback Interface) Index:1 addresses:
/127.0.0.1;
/0:0:0:0:0:0:0:1;
Name:eth0 (Realtek RTL8139 Family PCI Fast Ethernet NIC) Index:4 addresses:
/192.168.18.10;
/192.168.18.20;
Name:ppp0 (WAN (ppp/slip) Interface) index:0 addresses:
/218.61.151.22;

The result of the above operation is only a possible result, the reader will run the above program according to the hardware and software configuration of the machine, the results may be different.

Third, NetworkInterface Class of Getter Method

the NetworkInterface class provides three methods to obtain the network interface name (GetName method), the network interface Alias (GetDisplayName method), and all IP addresses bound to the network interface (Getinetaddresses method )。  

1. GetName Method

This method is used to get the name of a network interface. This name is the network interface name used when creating NetworkInterface objects using the Getbyname method, such as Eth0, Ppp0, and so on. The GetName method is defined as follows:

Public String GetName ()

2. GetDisplayName Method

This method can be used to obtain a more understandable network interface name, or the network interface name can be a network interface alias. In some operating systems, such as UNIX, the GetDisplayName method and the GetName method return the same value, but the GetDisplayName method in Windows typically returns a more friendly name, such as Realtek RTL8139 Family PCI Fast Ethernet NIC. The GetDisplayName method is defined as follows:

Public String GetDisplayName ()

3. Getinetaddresses Method

The NetworkInterface class can return all IP addresses that are bound to the network interface in the form of InetAddress objects through the Getinetaddresse method. The Getinetaddresses method is defined as follows:

Public enumeration<inetaddress> getinetaddresses ()

The above code demonstrates the use of the above three getter methods.

Package mynet;

Import java.net.*;
Import java.util.*;

public class MyNetworkInterface4
{
public static void Main (string[] args) throws Exception
{
if (Args.length = = 0)
Return
NetworkInterface ni = Networkinterface.getbyname (args[0]);
System.out.println ("Name:" + ni.getname ());
System.out.println ("DisplayName:" + ni.getdisplayname ());
Enumeration<inetaddress> addresses = ni.getinetaddresses ();
while (Addresses.hasmoreelements ())
System.out.println (Addresses.nextelement (). gethostaddress ());
}
}

1. Testing

Execute the following command:

Java mynet. MyNetworkInterface4 eth0

Operation Result:

Name:eth0
Displayname:realtek RTL8139 Family PCI Fast Ethernet NIC
192.168.18.10
192.168.18.20

Java network programming from getting started to mastering (11): Getting network interface information using the NetworkInterface class

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.