Android(kotlin)解析域名获取IP数组

Android 解析域名获取IP数组 kotlin

import java.net.InetAddress
import java.net.UnknownHostException

/**
 * 解析域名获取IP数组
 * @param host
 * @return
 */
private fun parseHostGetIPAddress(host: String = "www.baidu.com"): Array<String?>? {
	var ipAddressArr: Array<String?>? = null
    try {
    	val inetAddressArr: Array<InetAddress> = InetAddress.getAllByName(host)
        if (inetAddressArr.isNotEmpty()) {
        	ipAddressArr = arrayOfNulls(inetAddressArr.size)
            for (i in inetAddressArr.indices) {
            	ipAddressArr[i] = inetAddressArr[i].hostAddress
            }
        }
    } catch (e: UnknownHostException) {
    	e.printStackTrace()
    	return null
    }
    return ipAddressArr
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页