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