Android TelnetClient没有收到任何数据
我正在尝试使用Aapche Commons网络库在Android中实现telnet连接。我使用该示例构建了一个Android TelnetClient没有收到任何数据,android,apache-commons-net,Android,Apache Commons Net,我正在尝试使用Aapche Commons网络库在Android中实现telnet连接。我使用该示例构建了一个异步任务,但似乎根本没有收到任何数据 这是我的AsyncTask的doInBackground: protected Long doInBackground(String... strings) { publishProgress("Connecting to "+target+"\n"); try { Thread reader = new Threa
异步任务
,但似乎根本没有收到任何数据
这是我的AsyncTask
的doInBackground
:
protected Long doInBackground(String... strings) {
publishProgress("Connecting to "+target+"\n");
try {
Thread reader = new Thread(new Runnable() {
@Override
public void run() {
InputStream in = tc.getInputStream();
byte[] buff = new byte[1024];
int r = 0;
try {
do {
r = in.read(buff);
if (r > 0) {
publishProgress("[R] " + new String(buff, 0, r));
}
} while (r >= 0);
} catch (Exception e) {
publishProgress(e.getClass()+": "+e.getMessage());
} finally {
publishProgress("\n ++++ Disconnecting from thread.\n");
try {
tc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
try
{
tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
}
catch (InvalidTelnetOptionException e)
{
System.err.println("Error registering option handlers: " + e.getMessage());
}
tc.connect(target, port);
tc.registerNotifHandler(act);
reader.start();
reader.join();
} catch (Exception e) {
publishProgress(e.getClass()+" "+e.getMessage());
} finally {
publishProgress("+++ End of AsyncTask");
}
return 0L;
}
我想我复制了示例中的所有代码,但我只得到:
Connecting to 192.168.3.101
negcode=1, optcode=24 (this is from the notifhandler)
+++ Disconnecting from thread
+++ End of AsyncTask
当我在我的笔记本电脑上运行示例代码时,它工作了,我确实像预期的那样从telnet获得了login
更新如果我在
tc.connect()
之后等待1500毫秒,它确实可以工作。但这是一个相当丑陋的黑客行为。不要将线程置于异步任务的后台。把代码放进去。是的,我知道。这是一些从示例中复制代码的实验。我在另一个部分也做了同样的实验。