X Tutup
Skip to content

获取本机的所有IP #3

@Sitrone

Description

@Sitrone
/**
 * 获取本机的IP地址
 * @return  本机的IP地址(可能包含多个)
 */
private static List<String> getLocalIp() {
    List<String> listIp = new ArrayList<String>();
    Enumeration<NetworkInterface> n = null;

    try {
        n = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }

    while (n.hasMoreElements()) {
        NetworkInterface e = n.nextElement();

        Enumeration<InetAddress> ee = e.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress addr = ee.nextElement();
            listIp.add(addr.getHostAddress());
        }
    }

    return listIp;
}  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup