[Go] Java Get IP address from native

Source: Internet
Author: User
Tags get ip

[From] https://www.cnblogs.com/xiaoBlog2016/p/7076230.html

Discussed:

This blog is at work, need to get the current network under the correct IP address, online access to a lot of blogs, online a more popular argument is through Inetaddress.getlocalhost (). Gethostaddress (). But only can obtain the Simple network environment the IP address, then ignores the IP address in the present network environment more complex, for example has Lan,wifi, the Bluetooth hotspot, the virtual Machine network card ...
That is, there are many network interfaces (networks interfaces), each network interface contains an IP address, not all IP addresses can be accessed by external or local area network, such as virtual machine card address and so on.
That is to say Inetaddress.getlocalhost (). gethostaddress () IP is not necessarily the correct IP. Therefore, the company's great God, write their own tests, and write a blog:http://www.cnblogs.com/starcrm/p/7071227.html, and this blog is only organized in this context

1. Identify some of the rules of the current network:

1.1, 127.xxx.xxx.xxx belongs to the "loopback" address, that is, only your own native visible, is the local address, more common has 127.0.0.1;
1.2, 192.168.xxx.xxx belongs to private address (site), belonging to the local organization internal access, can only be visible on the local area network. The same 10.xxx.xxx.xxx, from 172.16.xxx.xxx to 172.31.xxx.xxx are private addresses, but also belong to the organization internal access;

1.3. The 169.254.xxx.xxx belongs to the connection local address (link locally IP) and is available in a separate network segment

1.4, from 224.xxx.xxx.xxx to 239.xxx.xxx.xxx belong to the multicast address

1.5, the more special 255.255.255.255 belong to broadcast address

1.6, in addition to the address is the point-to-point available public IPv4 address

2. Get the IP address in simple cases: first, when you search for "Java get native IP address" in "Baidu" or "Bing"; in general, the articles or blogs we search for can only get the native IP address exactly in a single case. For example: For example, you can get it in the following blog:

2.1: Use only WiFi condition;
2.2: Only use the network cable condition

Url:http://www.cnblogs.com/zrui-xyu/p/5039551.html3. Get the IP address in the complex network environment The source code is as follows:

Public inetaddressGetlocalhostlanaddress()Throws Exception {try {inetaddress candidateaddress =NullTraverse all the network interfacesFor (Enumeration ifaces = Networkinterface.getnetworkinterfaces (); ifaces.hasmoreelements ();) {NetworkInterface iface = (networkinterface) ifaces.nextelement ();Traverse IP on all interfacesFor (Enumeration Inetaddrs = Iface.getinetaddresses (); inetaddrs.hasmoreelements ();) {inetaddress inetaddr = (inetaddress) inetaddrs.nextelement ();if (!inetaddr.isloopbackaddress ()) {//exclude loopback type address if (inetaddr.issitelocaladdress ()) {//if it is the site-local address, it is return inetaddr;} else if (candidateaddress = null {//site-local type Address not found, first record candidate address candidateaddress = InetAddr;}}} } if (candidateaddress! = null) { return candidateaddress; } //if no Non-loopback address is found. Use only the most selected scheme inetaddress jdksuppliedaddress = Inetaddress.getlocalhost (); return jdksuppliedaddress;} catch (Exception e) {e.printstacktrace ();} return null;}        

[Go] Java gets the IP address from the computer

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.