mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-24 17:35:45 +00:00
Merge pull request #11 from 1091234881/patch-2
fix[net]: 无法检测本地端口时候被占用,创建 ServerSocket 时 指定本地ip,端口被占用抛出异常为 java.net.BindException: Address already in use (Bind failed)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user