Java工具-端口检测

/***
 * 检查主机端口是否被使用
 * @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;
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注