日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

获取本地的IP地址(内网)

發(fā)布時間:2025/4/16 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取本地的IP地址(内网) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

方法一

public static String getLocalIpAddress() {try {for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {NetworkInterface intf = en.nextElement();for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {InetAddress inetAddress = enumIpAddr.nextElement();Log.e("XXX", inetAddress.getHostAddress());if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {return inetAddress.getHostAddress();}}}} catch (SocketException ex) {ex.printStackTrace();}return null;}

方法二

private static void getIpAddress() {new Thread() {@Overridepublic void run() {super.run();Socket socket = null;try {socket = new Socket("www.baidu.com", 80);// 只有這個可以// 但是這個內網(wǎng)地址有沒有外網(wǎng)的地址呢?Log.e("XXX", "socket : "+ socket.getLocalAddress().toString() + "\n");} catch (Exception e) {e.printStackTrace();} finally {if (socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}}}.start();}

以上方法獲取到的地址是大都是內網(wǎng)的IP地址,如需獲取公網(wǎng)的IP地址,需要訪問:

www.ip138.com才可以,然后解析文本即可。

?

總結

以上是生活随笔為你收集整理的获取本地的IP地址(内网)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。