获取本机的所有IP · Issue #3 · Sitrone/LeetcodeInJava · GitHub
Skip to content

获取本机的所有IP #3

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;
}  

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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