getAllLocalIPAddress

      public String getAllLocalIPAddress(){
        StringBuilder  sbIpaddress = new StringBuilder();
        try {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements()) {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements()) {
                    InetAddress ip = inet.nextElement();
                    sbIpaddress.append(ip.getHostAddress());
                    sbIpaddress.append("\r\n");
                }
            }
        }
        catch(SocketException e)
        {
             sbIpaddress.append(e.toString());
             sbIpaddress.append("\r\n");
        }
        return sbIpaddress.toString();
    }

 

posted @ 2017-10-26 18:30  sky20080101  阅读(150)  评论(0)    收藏  举报