作者: tearell

  • Java工具-端口检测

    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;
    }