1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android 获取局域网内网IP地址

Android 获取局域网内网IP地址

时间:2024-01-05 14:01:25

相关推荐

Android 获取局域网内网IP地址

Android 获取局域网内网IP地址

废话上代码完事

废话

拿局域网IP一般用在tcp通讯或udp通讯上,下次再整理下这类通讯的框架

上代码

/*** 网络工具* 获得局域网IP地址* @author dlong* created at /3/8 2:46 PM*/public class NetworkUtils {/*** 获取内网IP地址* @return* @throws SocketException*/public static String getLocalIPAddress() throws SocketException {for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){NetworkInterface intf = en.nextElement();for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){InetAddress inetAddress = enumIpAddr.nextElement();if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){return inetAddress.getHostAddress().toString();}}}return "null";}}

kotlin版

import .Inet4Addressimport .InetAddressimport workInterfaceimport java.util.*/*** @author D10NG* @date on -12-10 14:37*/object NetUtils {/*** 获取内网IP地址*/val localIPAddress: Stringget() {val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()while (en.hasMoreElements()) {val intf: NetworkInterface = en.nextElement()val enumIpAddr: Enumeration<InetAddress> = intf.inetAddresseswhile (enumIpAddr.hasMoreElements()) {val inetAddress: InetAddress = enumIpAddr.nextElement()if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {return inetAddress.hostAddress.toString()}}}return "null"}}

完事

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。