时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 返回最有可能是外部地址的地址。
Java 返回最有可能是外部地址的地址。
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
490
查看 / 运行 实例源码
返回最有可能是外部地址的地址。
实例源码:
源代码:
执行
执行中...
/* * GNU GENERAL PUBLIC LICENSE * Version 2, June 1991 * * ADDITIONAL REQUIREMENT * * 1. If PeerfactSim.KOM is used for the evaluation in research publications, * the following should be referenced: * * @techreport{ PWLS07, * author = {Konstantin Pussep and Matthias Weinert and Nicolas Liebau and Ralf Steinmetz}, * title = {Flexible Framework for NAT Traversal in Peer-to-Peer Applications}, * month = {Nov}, * year = {2007}, * institution = {KOM - Multimedia Communications Lab, Technische Universit{\"a}t Darmstadt}, * address = {Merckstra{\ss}e 25, 64283 Darmstadt, Germany}, * number = {KOM-TR-2007-06}, * } */ //package com.nowjava;//来 自 n o w j a v a . c o m import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; public class Main { public static void main(String[] argv) throws Exception { /* from nowjava - 时 代 Java */ System.out.println(getMostProbableExternalAddress()); } /** * Returns the address which is most likely the external address. The result does not have * to be valid in the external network, since this method does not check for NAT-environments * etc. * * @param addresses list of addresses * @return the address which is most probably the external address */ public static InetAddress getMostProbableExternalAddress() { // get network interfaces Collection
activeIfs = getNetworkInterfaces(); // get non-internal addresses ArrayList
localAddr = new ArrayList
(); ArrayList
nonLocalAddr = new ArrayList
(); for (NetworkInterface ni : activeIfs) { Enumeration
addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (hasLocalScope(addr)) localAddr.add(addr); else nonLocalAddr.add(addr); } } // have nonlocal addresses? then return the "best" if (!nonLocalAddr.isEmpty()) { String removeAddrPrefixes[] = { "5.", "10.", "192." }; for (String s : removeAddrPrefixes) { InetAddress last = nonLocalAddr.get(0); if (nonLocalAddr.size() == 1) return last; removeAddressesWith(nonLocalAddr, s); if (nonLocalAddr.size() == 0) return last; else if (nonLocalAddr.size() == 1) return nonLocalAddr.get(0); } // we tried to get the right one, but more than one are left.. return first entree return nonLocalAddr.get(0); } // we dont want to return (link-)local addresses, throw exception throw new IllegalStateException("No external address found!"); } /** * Returns all network interfaces of this host. * @return all network interfaces of this host */ public static Collection
getNetworkInterfaces() { // get network interfaces ArrayList
result = new ArrayList
(); Enumeration
ifs = null; try { ifs = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e1) { throw new IllegalStateException( "Could not enumerate network interfaces!"); } while (ifs.hasMoreElements()) { result.add(ifs.nextElement()); } return result; } /** * Returns true when the given address is a local address. * @param addr address /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
/172.21.0.13
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部