IP address
You can use the following code:
InetAddress inet = Inetaddress.getlocalhost ();
System.out.println ("Ip= of the Machine" + inet.gethostaddress ());
You can work under window. Return to 127.0.0.1 under Linux. The main is to return under Linux is the localhost IP address configured in/etc/hosts, not the NIC's binding address. Later, using the binding address of the NIC, you can take the IP address of this machine:
The code is as follows:
//IP for native configuration based on the NIC
enumeration netinterfaces= Networkinterface.getnetworkinterfaces ();
inetaddress IP = null;
while (netinterfaces.hasmoreelements ())
{
networkinterface ni= (NetworkInterface) netinterfaces.nextelement ();
system.out.println (Ni.getname ());
ip= (inetaddress) ni.getinetaddresses (). Nextelement ();
if (!ip.issitelocaladdress ()
&&! Ip.isloopbackaddress ()
&& ip.gethostaddress (). IndexOf (":") ==-1)
{
system.out.println ("native ip=" + ip.gethostaddress ());
break;
}
else
{
Ip=null;
}
}