时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 比较两个主机名,以检查它们是否为同一主机,而不进行DNS解析。
Java 比较两个主机名,以检查它们是否为同一主机,而不进行DNS解析。
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
447
查看 / 运行 实例源码
比较两个主机名,以检查它们是否为同一主机,而不进行DNS解析。
实例源码:
源代码:
执行
执行中...
//package com.nowjava; /* 来自 *nowjava.com*/ import sun.net.util.IPAddressUtil; public class Main { public static void main(String[] argv) throws Exception { String hostA = "nowjava.com"; String hostB = "nowjava.com"; System.out.println(ifSameHost(hostA, hostB)); } /** * This method is to compare two host name * to check if they are the same host without DNS resolving. * If the host name is IP address string, e.g., '2001:1b70:82a9:6100::9' * the comparison supports to compare different format of same IP address. * * If the host name is just an host name instead of IP address, * it just compare the String by using * String.equals(...) * * @param hostA * @param hostB * @return */ /* from 时代Java公众号 - N o w J a v a . c o m */ public static boolean ifSameHost(String hostA, String hostB) { boolean same = false; if (hostA == null && hostB == null) { return true; } byte[] addr1 = parseIpAddress(hostA); byte[] addr2 = parseIpAddress(hostB); //If this IP address string, compare the IP address if (addr1 != null && addr2 != null) { same = ifSameIpAddress(addr1, addr2); } //If this is host mame, just compare the host name string if (!same && hostA != null) same = hostA.equals(hostB); return same; } /** * This function parse the IP address string as a byte array * If the IP address is IPv4 address, the byte array length is 4 * If the IP address is IPv6 address, the byte array length is 16 * If the host is not an IP address string, will return null; * @param ipAddress * @return if this is valid IPv4 address or IPv6 address, return byte array for the address * otherwise return null. * So, this method can be used to check if the ipAddress string is valid or not */ public static byte[] parseIpAddress(String ipAddress) { if (ipAddress == null) return null; boolean ipv6Expected = false; if (ipAddress.charAt(0) == '[') { // This is supposed to be an IPv6 literal if (ipAddress.length() > 2 && ipAddress.charAt(ipAddress.length() - 1) == ']') { ipAddress = ipAddress.substring(1, ipAddress.length() - 1); ipv6Expected = true; } else { // This was supposed to be a IPv6 address, but it's not! //throw new UnknownHostException(host + ": invalid IPv6 address"); return null; } } // if host is an IP address, we won't do further lookup if (Character.digit(ipAddress.charAt(0), 16) != -1 || (ipAddress.charAt(0) == ':')) { byte[] addressBytes = null; // int numericZone = -1; // String ifname = null; // see if it is IPv4 address addressBytes = IPAddressUtil.textToNumericFormatV4(ipAddress); if (addressBytes == null) { addressBytes = IPAddressUtil .textToNumericFormatV6(ipAddress); } else if (ipv6Expected) { // Means an IPv4 litteral between brackets! //throw new UnknownHostException("["+host+"]"); return null; } return addressBytes; } else if (ipv6Expected) { // We were expecting an IPv6 Litteral, but got something else //throw new UnknownHostException("["+host+"]"); return null; } return null; } /** * Compare two IP address byte array, the length must be 4 or 16. * @param address1 * @param address2 * @return */ private static boolean ifSameIpAddress(byte[] address1, byte[] address2) { if (address1 == null && address2 == null) return true; if (address1 != null && address2 != null && address1.length == address2.length) { //this IPv4 address if (address1.length == 4) { /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
true
本文系作者在时代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
返回顶部