Update NetUtils.java

无法检测本地端口时候被占用
创建 ServerSocket 时 指定本地ip
端口被占用 异常为 java.net.BindException: Address already in use (Bind failed)
This commit is contained in:
1091234881
2022-04-20 14:10:11 +08:00
committed by GitHub
parent 2ef5616468
commit 67244e2b01
@@ -114,9 +114,12 @@ public class NetUtils {
// 给定的IP未在指定端口范围中
return false;
}
try (ServerSocket ss = new ServerSocket(port)) {
try {
InetAddress localHost = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(port, 50, localHost);
ss.close();
return true;
} catch (IOException e) {
} catch (Exception e) {
return false;
}
}