六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

获取本地 ip (多网卡的情况下)

[复制链接]

升级  10%

17

主题

17

主题

17

主题

秀才

Rank: 2

积分
65
 楼主| 发表于 2013-2-7 03:32:56 | 显示全部楼层 |阅读模式
public String getLocalHostName() {
        String hostName;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            hostName = addr.getHostAddress();
        } catch (Exception ex) {
            hostName = "";
        }
        return hostName;
    }

public String[] getAllLocalHostIP() {
    String[] ret = null;
    try {
        String hostName = getLocalHostName();
        if (hostName.length() > 0) {
            InetAddress[] addrs = InetAddress.getAllByName(hostName);
            if (addrs.length > 0) {
                ret = new String[addrs.length];
                for (int i = 0; i < addrs.length; i++) {
                    ret[i] = addrs[i].getHostAddress();
                }
            }
        }

        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表