package com.xty.common

import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.*

/**
 * 获取Ip地址
 */
class IpUtils {
    companion object{
        fun getIpAddress(): String? {
            try {
                val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
                while (en.hasMoreElements()) {
                    val intf: NetworkInterface = en.nextElement()
                    val enumIpAddr: Enumeration<InetAddress> = intf.getInetAddresses()
                    while (enumIpAddr.hasMoreElements()) {
                        val inetAddress: InetAddress = enumIpAddr.nextElement()
                        if (!inetAddress.isLoopbackAddress() && inetAddress is Inet4Address) {
                            //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {
                            return inetAddress.getHostAddress().toString()
                        }
                    }
                }
            } catch (e: Exception) {
            }
            return ""
        }
    }


}