mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix local port bug
This commit is contained in:
parent
3c1060769e
commit
fb4af337ea
@ -10,6 +10,7 @@
|
|||||||
### Bug修复
|
### Bug修复
|
||||||
* 【setting】 修复Props.toBean方法null的问题
|
* 【setting】 修复Props.toBean方法null的问题
|
||||||
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee)
|
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题(issue#I1B18H@Gitee)
|
||||||
|
* 【core 】 修复NetUtil.isUsableLocalPort()判断问题(issue#765@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,13 +1,25 @@
|
|||||||
package cn.hutool.core.net;
|
package cn.hutool.core.net;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import cn.hutool.core.lang.Filter;
|
||||||
|
import cn.hutool.core.lang.Validator;
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import java.net.IDN;
|
import java.net.IDN;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -21,31 +33,22 @@ import java.util.LinkedHashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
import javax.net.ServerSocketFactory;
|
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
|
||||||
import cn.hutool.core.lang.Filter;
|
|
||||||
import cn.hutool.core.lang.Validator;
|
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络相关工具
|
* 网络相关工具
|
||||||
*
|
*
|
||||||
* @author xiaoleilu
|
* @author xiaoleilu
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class NetUtil {
|
public class NetUtil {
|
||||||
|
|
||||||
public final static String LOCAL_IP = "127.0.0.1";
|
public final static String LOCAL_IP = "127.0.0.1";
|
||||||
|
|
||||||
/** 默认最小端口,1024 */
|
/**
|
||||||
|
* 默认最小端口,1024
|
||||||
|
*/
|
||||||
public static final int PORT_RANGE_MIN = 1024;
|
public static final int PORT_RANGE_MIN = 1024;
|
||||||
/** 默认最大端口,65535 */
|
/**
|
||||||
|
* 默认最大端口,65535
|
||||||
|
*/
|
||||||
public static final int PORT_RANGE_MAX = 0xFFFF;
|
public static final int PORT_RANGE_MAX = 0xFFFF;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -103,12 +106,21 @@ public class NetUtil {
|
|||||||
// 给定的IP未在指定端口范围中
|
// 给定的IP未在指定端口范围中
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName(LOCAL_IP)).close();
|
// issue#765@Github, 某些绑定非127.0.0.1的端口无法被检测到
|
||||||
return true;
|
try (ServerSocket ss = new ServerSocket(port)) {
|
||||||
} catch (Exception e) {
|
ss.setReuseAddress(true);
|
||||||
|
} catch (IOException ignored) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try (DatagramSocket ds = new DatagramSocket(port)) {
|
||||||
|
ds.setReuseAddress(true);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -432,7 +444,7 @@ public class NetUtil {
|
|||||||
* 获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个<br>
|
* 获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个<br>
|
||||||
* 如果获取失败调用 {@link InetAddress#getLocalHost()}方法获取。<br>
|
* 如果获取失败调用 {@link InetAddress#getLocalHost()}方法获取。<br>
|
||||||
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
|
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
|
||||||
*
|
* <p>
|
||||||
* 参考:http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
|
* 参考:http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
|
||||||
*
|
*
|
||||||
* @return 本机网卡IP地址,获取失败返回<code>null</code>
|
* @return 本机网卡IP地址,获取失败返回<code>null</code>
|
||||||
@ -453,9 +465,9 @@ public class NetUtil {
|
|||||||
* 1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
|
* 1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
|
||||||
* 2. 如果无满足要求的地址,调用 {@link InetAddress#getLocalHost()} 获取地址
|
* 2. 如果无满足要求的地址,调用 {@link InetAddress#getLocalHost()} 获取地址
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
* <p>
|
||||||
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
|
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
|
||||||
*
|
* <p>
|
||||||
* 见:https://github.com/looly/hutool/issues/428
|
* 见:https://github.com/looly/hutool/issues/428
|
||||||
*
|
*
|
||||||
* @return 本机网卡IP地址,获取失败返回<code>null</code>
|
* @return 本机网卡IP地址,获取失败返回<code>null</code>
|
||||||
@ -553,7 +565,6 @@ public class NetUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* 简易的使用Socket发送数据
|
* 简易的使用Socket发送数据
|
||||||
*
|
*
|
||||||
* @param host Server主机
|
* @param host Server主机
|
||||||
@ -573,7 +584,6 @@ public class NetUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* 使用普通Socket发送数据
|
* 使用普通Socket发送数据
|
||||||
*
|
*
|
||||||
* @param host Server主机
|
* @param host Server主机
|
||||||
@ -684,6 +694,7 @@ public class NetUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------- Private method start
|
// ----------------------------------------------------------------------------------------- Private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定IP的long是否在指定范围内
|
* 指定IP的long是否在指定范围内
|
||||||
*
|
*
|
||||||
|
@ -379,35 +379,17 @@ public class RandomUtil {
|
|||||||
*
|
*
|
||||||
* @param length 长度
|
* @param length 长度
|
||||||
* @return 随机索引
|
* @return 随机索引
|
||||||
|
* @since 5.2.1
|
||||||
*/
|
*/
|
||||||
public static int[] createRandomList(int length){
|
public static int[] randomInts(int length){
|
||||||
int[] list = ArrayUtil.range(length);
|
final int[] range = ArrayUtil.range(length);
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
int random = randomInt(i,length);
|
int random = randomInt(i,length);
|
||||||
ArrayUtil.swap(list,i,random);
|
ArrayUtil.swap(range,i,random);
|
||||||
}
|
}
|
||||||
return list;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 随机获得列表中的一定量的元素,返回List
|
|
||||||
*
|
|
||||||
* @param source 列表
|
|
||||||
* @param count 随机取出的个数
|
|
||||||
* @param <T> 元素类型
|
|
||||||
* @return 随机列表
|
|
||||||
*/
|
|
||||||
public static <T> List<T> randomEleList(List<T> source, int count){
|
|
||||||
if(count >= source.size()){
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
int[] randomList = ArrayUtil.sub(createRandomList(source.size()),0,count);
|
|
||||||
List<T> result = new ArrayList<>();
|
|
||||||
for (int e: randomList){
|
|
||||||
result.add(source.get(e));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* 获得一个随机的字符串(只包含数字和字符)
|
* 获得一个随机的字符串(只包含数字和字符)
|
||||||
*
|
*
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package cn.hutool.core.net;
|
package cn.hutool.core.net;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import cn.hutool.core.lang.PatternPool;
|
||||||
|
import cn.hutool.core.util.ReUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import cn.hutool.core.lang.PatternPool;
|
import java.net.InetAddress;
|
||||||
import cn.hutool.core.net.NetUtil;
|
|
||||||
import cn.hutool.core.util.ReUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NetUtil单元测试
|
* NetUtil单元测试
|
||||||
@ -54,4 +52,9 @@ public class NetUtilTest {
|
|||||||
long ipLong = NetUtil.ipv4ToLong("127.0.0.1");
|
long ipLong = NetUtil.ipv4ToLong("127.0.0.1");
|
||||||
Assert.assertEquals(2130706433L, ipLong);
|
Assert.assertEquals(2130706433L, ipLong);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isUsableLocalPortTest(){
|
||||||
|
Assert.assertTrue(NetUtil.isUsableLocalPort(80));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user