zoukankan      html  css  js  c++  java
  • java获取本地地址

     
        // 正确的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;
            }
        }
        public static void main(String[] args) throws Exception {
    //        remotetest();
    //        localtest();
            try {
              
    
                // 正确的IP拿法
                System.out.println("get LocalHost LAN Address : " + getLocalHostLANAddress().getHostAddress());
    
            } catch (UnknownHostException e) {
    
                e.printStackTrace();
    
            }
    
        }

    上一个比较普遍的说法是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地址
  • 相关阅读:
    后台线程处理数据,如何实时更新UI(datagridview)多线程同步问题
    DataGridView设置行高
    C#打开外部文件,如txt文件
    20120621第一天_复习与测试\04continue
    关于C#正则表达式MatchCollection类的总结
    关于sqlite中的一个错误 “database is locked"
    UI中 加个timer 写个while true的方法 不断获取run的对象
    最近帮公司开发一个邮件营销软件 用到XPTable 但找了很多方法 摸索了很久都不知道如何更新进度条 不过在国外的一个网站 终于找到答案了
    C# 简单的往txt中写日志,调试时很有用 【转】
    输入要匹配的内容和正则表达式规则 返来单个匹配的内容 正则表达式方法 常用
  • 原文地址:https://www.cnblogs.com/caoshouling/p/14325841.html
Copyright © 2011-2022 走看看