詳談再論JAVA擷取本機IP地址

來源:互聯網
上載者:User

標籤:import   expected   nts   一個   簡單   color   ati   ssi   turn   

首先,你如果搜尋“JAVA擷取本機IP地址”,基本上搜到的資料全是無用的。
比如這篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html
實際上的代碼在複雜環境下是不準的


網上一個比較普遍的說法是InetAddress.getLocalHost().getHostAddress()
似乎很簡單,但忽略了一個問題,即IP地址在現在的網路環境更加複雜了,比如有Lan,WIFI,藍芽熱點,虛擬機器網卡...
即存在很多的網路介面(network interfaces),每個網路介面就包含一個IP地址,並不是所有的IP地址能被外部或區域網路訪問,比如說虛擬機器網卡地址等等。
也就是說InetAddress.getLocalHost().getHostAddress()的IP不一定是正確的IP。

寫代碼前,先明確一些規則:

  • 127.xxx.xxx.xxx 屬於"loopback" 地址,即只能你自己的本機可見,就是本機地址,比較常見的有127.0.0.1;
  • 192.168.xxx.xxx 屬於private 私人地址(site local address),屬於本機群組織內部訪問,只能在本地區域網路可見。同樣10.xxx.xxx.xxx、從172.16.xxx.xxx 到 172.31.xxx.xxx都是私人地址,也是屬於組織內部訪問;
  • 169.254.xxx.xxx 屬於串連本地地址(link local IP),在單獨網段可用
  • 從224.xxx.xxx.xxx 到 239.xxx.xxx.xxx 屬於組播地址
  • 比較特殊的255.255.255.255 屬於廣播位址
  • 除此之外的地址就是點對點的可用的公開IPv4地址

 

擷取本機IP地址的正確姿勢:

package ipTest;import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;import java.util.Enumeration;public class Test {    public Test() {        // TODO Auto-generated constructor stub    }    public static void main(String[] args) {        // TODO Auto-generated method stub        InetAddress ip;        try {            // 這種IP容易拿錯            // System.out.println("Current IP address : " +            // InetAddress.getLocalHost().getHostAddress());            // 不一定準確的IP拿法            // 出自比如這篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html            System.out.println("get LocalHost Address : " + getLocalHostAddress().getHostAddress());            // 正確的IP拿法            System.out.println("get LocalHost LAN Address : " + getLocalHostLANAddress().getHostAddress());        } catch (UnknownHostException e) {            e.printStackTrace();        }    }    // 正確的IP拿法,即優先拿site-local地址    private static InetAddress getLocalHostLANAddress() throws UnknownHostException {        try {            InetAddress candidateAddress = null;            // 遍曆所有的網路介面            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();                // 在所有的介面下再遍曆IP                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();                    if (!inetAddr.isLoopbackAddress()) {// 排除loopback類型地址                        if (inetAddr.isSiteLocalAddress()) {                            // 如果是site-local地址,就是它了                            return inetAddr;                        } else if (candidateAddress == null) {                            // site-local類型的地址未被發現,先記錄候選地址                            candidateAddress = inetAddr;                        }                    }                }            }            if (candidateAddress != null) {                return candidateAddress;            }            // 如果沒有發現 non-loopback地址.只能用最次選的方案            InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();            if (jdkSuppliedAddress == null) {                throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");            }            return jdkSuppliedAddress;        } catch (Exception e) {            UnknownHostException unknownHostException = new UnknownHostException(                    "Failed to determine LAN address: " + e);            unknownHostException.initCause(e);            throw unknownHostException;        }    }    //出自這篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html    //實際上的代碼是不準的    private static InetAddress getLocalHostAddress() throws UnknownHostException {        Enumeration allNetInterfaces;        try {            allNetInterfaces = NetworkInterface.getNetworkInterfaces();            InetAddress ip = null;            while (allNetInterfaces.hasMoreElements()) {                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();                Enumeration addresses = netInterface.getInetAddresses();                while (addresses.hasMoreElements()) {                    ip = (InetAddress) addresses.nextElement();                    if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {                        if (ip != null && ip instanceof Inet4Address) {                            return ip;                        }                    }                }            }        } catch (SocketException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();        if (jdkSuppliedAddress == null) {            throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");        }        return jdkSuppliedAddress;    }}

 

詳談再論JAVA擷取本機IP地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.