Java工具-端口检测

更新于
4,778
/***
 * 检查主机端口是否被使用
 * @param host IP/域名/Host
 * @param port 端口
 * @return 是否被使用
 */
public static boolean checkPort(String host, int port) {
    boolean flag = false;
    InetAddress address = null;
    try {
        address = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
    try (Socket socket = new Socket(address, port)) {
        flag = true;
    } catch (IOException ignore) {
    }
    return flag;
}
我的会员
加入会员后,您将获得更多权益!
0 点赞
0 收藏
分享
0 讨论
反馈