Get the native IP address in Java

Source: Internet
Author: User

On the internet to find a few Java to get the native IP address of the code, found that there are a few imperfect, their own tidy up a bit. All of a sudden want to get their own IP address to obtain, although the system comes with commands can be obtained, but I want to write a program to get a bit, To the web search for Java to get the IP address of the method, the results found that no one is can be used, the gas of my old man vomiting blood,

These people write the program with the eyes closed, finished writing to the Internet, and do not test, harm I think their RP problem, always get the correct IP address, strongly condemned!!!
In order to show contempt, now the main two methods found on the Internet to point out the lack of
Method One (can only be used on Windows, Linux platform GEi fart):

Try
{
System.out.println ("IP of this machine =" + inetaddress.getlocalhost ());
} catch (Unknownhostexception e)
{
E.printstacktrace ();
}

The result of execution under Linux is: NATIVE IP = xxx/127.0.1.1 (where xxx is your computer name, I mosaic here)

Method Two (claims can be executed under Linux)

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 ("ip= of this Machine" + ip.gethostaddress ());
Break
}
Else
{
Ip=null;
}
}
From the red section of the code can be seen, the code for the acquisition of the first NetworkInterface IP address of the acquisition, no loop to get, just the first IP address is processed, which results in if the first IP address is not a
Inet4address address instead of a < span
Id= "Articlecontent1_articlecontent1_lblcontent" & gt;inet6address, this judgment ip.gethostaddress (). IndexOf (":") ==- 1 will always be false, this if condition into not go ah, many harm, strongly despise!

But the method of two ideas is right, there are some minor problems, let me modify a bit, the final version of Linux can be executed correctly under the code as follows:
Enumeration allnetinterfaces = Networkinterface.getnetworkinterfaces ();
inetaddress IP = null;
while (Allnetinterfaces.hasmoreelements ())
{
NetworkInterface netinterface = (networkinterface) allnetinterfaces.nextelement ();
System.out.println (Netinterface.getname ());
Enumeration addresses = Netinterface.getinetaddresses ();
while (Addresses.hasmoreelements ())
{
ip = (inetaddress) addresses.nextelement ();
if (IP! = null && IP instanceof inet4address)
{
System.out.println ("IP of this machine =" + ip.gethostaddress ());
}
}
}

Get the native IP address in Java

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.