I am trying to use the Aapche Commons Net library to implement a telnet connection in Android. I used the example to build an AsyncTask
, but there seems to be no data received at all.
This is the doInBackground
of my AsyncTask
:
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;
}
I think I copied all the code from the example, still I only get:
Connecting to 192.168.3.101
negcode=1, optcode=24 (this is from the notifhandler)
+++ Disconnecting from thread
+++ End of AsyncTask
When I run the example code on my laptop, it works, I do get the login
from the telnet as expected.
Update It does work if I wait 1500 msecs after tc.connect()
. But that's quite an ugly hack.