简体   繁体   English

我如何在Android上保持Jsch Session存活

[英]How can i keep a Jsch Session alive on android

I am writing an app to connect over SSH to a server. 我正在编写一个通过SSH连接到服务器的应用程序。 My intention is to give the user of the app an internet connection as long as they are connected to the server (The SSH Script runs as Android Service). 我的意图是为应用程序的用户提供互联网连接,只要他们已连接到服务器(SSH脚本作为Android服务运行)。 The Problem is, when I start a session and create a channel everything works fine. 问题是,当我开始一个会话并创建一个频道时,一切正常。 But after about 20-30 minutes (sometimes up to several hours) the channel and the session closes. 但是在大约20-30分钟(有时长达几个小时)之后,频道和会话关闭。

Connect function: 连接功能:

 public String connecting(
        String username,
        final String password,
        String hostname,
        int port) {

    try {
        Log.d("MainActivity", "Start JSch session and connect");
        jsch = new JSch();
        session = jsch.getSession(username, hostname, port);

        session.setPassword(password);

        // Avoid asking for key confirmation
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);
        session.connect();
        session.setServerAliveInterval(15);
        session.setServerAliveCountMax(100);


        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();

        InputStream in = channel.getInputStream();
        serviceStatus = true;
        streamtext = "";
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) break;
                streamtext = new String(tmp, 0, i);

                }
            }
            if (channel.isClosed()) {
                if (in.available() > 0) continue;
                Log.d(TAG, "exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }

        return streamtext;
    } catch (Exception except){
        except.printStackTrace();
        passErrorToActivity("Error: Connection error");
        return "Error: Connection error";
    }
}

Start function: 启动功能:

public void start(){
        try {
            new AsyncTask<Integer, Void, Void>() {
                @Override
                protected Void doInBackground(Integer... params) {
                    try {
                    passMessageToActivity(connecting(user, password, host, port));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    return null;
                }
            }.execute(1);
        } catch (Exception exc) {
            exc.printStackTrace();
        }

"passMessageToActivity" Just creats an intent and sends the "streamtext" to the MainActivity “ passMessageToActivity”仅创建一个意图并将“ streamtext”发送给MainActivity

I've already tryed it with Session#setServerAliveInterval(int milliseconds) but it didn't work. 我已经用Session#setServerAliveInterval(int milliseconds)尝试过,但是没有用。 Is there any posibility to keep the session and channel alive? 是否有保持会话和频道有效的可能性? I've seen the solution of this user but this doesn't work for me because it's important that the connection between server and service is always up. 我已经看到了该用户的解决方案,但这对我来说不起作用,因为服务器与服务之间的连接始终保持连接很重要。

  • If setting keepalives only does not help, you need to keep the session busy with more high-level actions. 如果仅设置保持活动没有帮助,则需要使会话忙于更多高级操作。 Like sending a dummy command, like pwd . 就像发送伪指令一样,如pwd Though you may need to explain, why you have "shell" session open, that look suspicious. 尽管您可能需要解释,但是为什么打开“ shell”会话却看起来很可疑。
  • Anyway, there's no way you can guarantee that a connection stays alive. 无论如何,您无法保证连接保持活动状态。 You would have to deal with losing connection occasionally anyway. 无论如何,您将不得不偶尔处理失去连接的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM