【发布时间】:2012-01-03 21:13:00
【问题描述】:
我正在开发一个 Android 程序,它通过 SSH 连接到服务器以获取一些数据。
问题是,如果一个命令被发送到服务器,它不返回任何东西(例如空文件上的 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;
}
【问题讨论】:
-
我认为这是stackoverflow.com/questions/804951/…的复制品
标签: java android ssh inputstream j2ssh