Java Network Programming from entry to mastery (11): using the NetworkInterface class to obtain Network Interface Information

Source: Internet
Author: User

SlaveJDK1.4Start,JavaProvidesNetworkInterfaceClass. This class obtains information about the logical network interfaces created by all the physical network interfaces and virtual machines of the local machine using the physical network interfaces of the local machine.

I,CreateNetworkInterfaceTwo static methods of the object

NetworkInerfaceClass andInetAddressNo.Public. Therefore, it must be created using two static methods.NetworkInterfaceObject. Two methods can be used to createNetworkInterfaceObject: Network Interface Name (GetByNameMethod) andIPAddress(GetByInetAddressMethod).

1. getByNameMethod

This method can be created through the network interface NameNetworkInterfaceObject. This network interface name is not a computer name, but is used to identify the physical or logical network interface name, which is generally set by the operating system. Network Interface names are in most operating systems (includingWindows,LinuxAndUnix) IsEthThe index number of the network interface, starting from0Start. If the local machine has three NICs, the network interface names areEth0,Eth1AndEth2.NetworkInterfaceObjectToStringThe method returns the name, display name, and all the characters bound to the network interface.IPAddress and other information. When the network interface name does not exist,GetByNameReturnNull.GetByNameThe method is defined as follows:

Public   Static NetworkInterface getByName (String name) Throws SocketException

The following codeIs a program that displays information about a specified network interface. The Network Interface Name is passed in through the 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 ) ?   " The Network Interface does not exist! " : Ni );
}
}

  • Test1

Run the following command:

Java mynet. MyNetworkInterface1 eth0

Running result:

Name: eth0 (Realtek RTL8139 Family PCI Fast Ethernet NIC) index: 4 Addresses:
/ 192.168.18.10 ;
/ 192.168.18.20 ;
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.