All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.linyimin0812.profiler.common.utils.IpUtil Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package io.github.linyimin0812.profiler.common.utils;

import java.net.*;
import java.util.Enumeration;

/**
 * @author linyimin
 **/
public class IpUtil {

    public static String getIp() {
        try {
            Enumeration interfaces = NetworkInterface.getNetworkInterfaces();

            while (interfaces.hasMoreElements()) {
                NetworkInterface ni = interfaces.nextElement();
                Enumeration addresses = ni.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = addresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress();
                    }
                }
            }

        } catch (SocketException ignored ) {
            return "127.0.0.1";
        }

        return "127.0.0.1";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy