0

SSH経由でサーバーに接続してデータを取得するAndroid用のプログラムに取り組んでいます。

問題は、コマンドがサーバーに送信され、何も返されない場合 (空のファイルの cat など)、プログラムがハングし、in.read() によってブロックされているように見えることです。

行にブレークポイントがあります

if ((read = in.read(buffer)) != -1){

そして、その下の then/else 行にあります。デバッグすると、プログラムはifステートメントで正常に中断しますが、続行を押すと、プログラムは再びハングアップし、次のブレークポイントに到達しません。

実際にサーバーから応答があればプログラムは正常に動作しますが、サーバーが正常に連携していない場合にプログラムがハングしないようにしたいと考えています。

J2SSH ライブラリを使用しています。

public String command(String command) {
    command = command + "\n";

    if (session.getSessionType().equals("Uninitialized") || session.isClosed()) {
        openShell();
    }

    OutputStream out = session.getOutputStream();
    InputStream in = session.getInputStream();


    byte buffer[] = new byte[255];
    int read;
    String in1 = null;
    String fullOutput = "";

    try {
        try {
            out.write(command.getBytes());
        } catch (IOException e){
            Log.e(TAG,"Error writing IO stream");
            e.printStackTrace();
        }
        boolean retrivingdata = true;
        while (retrivingdata){
            String iStreamAvail = "Input Stream Available "+ in.available();

            if ((read = in.read(buffer)) != -1){
                retrivingdata = true;
            } else {
                retrivingdata = false;
                return null;
            }

            in1 = new String(buffer, 0, read);
            fullOutput = fullOutput + in1;

            if (read < 255){
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fullOutput;
}
4

1 に答える 1

0

読み取りと書き込みは別のスレッドで行う必要があります。read() は、サーバーからデータが利用可能になるまで待機するブロッキング メソッドです。

于 2012-01-03T13:13:00.573 に答える