Problem using org.apache.commons.net.telnet.* with android
<p>I'm having a problem using the <code>org.apache.commons.net.telnet.*</code> library in my Android application and I was hoping someone could help me.</p>

<p>I have implemented an app which uses telnet to communicate with a remote server and all works fine.  The problem I have is when I call <code>TelnetClient.disconnect()</code> the method does not return. When calling the method when running a test case (meaning sans Android, just through Eclipse) it returns immediately, but on Android something is screwing it up. My test case looked something like:</p>

<pre><code>TelnetClient telnet = new TelnetClient();
telnet.connect(ipAddress, port); 
telnet.disconnect(); //HERE
</code></pre>

<p>Could anyone enlighten me as to why this may be?</p>

<p>If I obtain an <code>InputStream</code> from the connected telnet object and some info is sent from the telnet server after disconnect has been called, it can cause the method to return and closes the comm thread which was created by calling <code>.connect(..)</code>, which makes me think there is some sort or lock being held somewhere?</p>

<p>What would the fundamental difference be between the Dalvik VM or Android OS and my Java VM and XP machine to cause the behavior?</p>

<p>Thanks for any help, stack traces below!</p>

<p>I have a stack trace from the comm thread created by the <code>TelnetClient.connect()</code> at the time the ANR appears (as a result of the disconnect call indefinitely hanging).</p>

<pre><code>"Thread-15" daemon prio=6 tid=17 NATIVE | group="main" sCount=1 dsCount=0 s=N                obj=0x43812d90 self=0x1be528 | sysTid=1403 nice=-2 sched=0/0 handle=2703408 
  at org.apache.harmony.luni.platform.OSNetworkSystem.receiveStreamImpl(Native Method) 
  at org.apache.harmony.luni.platform.OSNetworkSystem.receiveStream(OSNetworkSystem.java:236) 
  at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:550) 
  at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:87) 
  at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:67) 
  at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:177) 
  at java.io.BufferedInputStream.read(BufferedInputStream.java:259) 
  at java.io.PushbackInputStream.read(PushbackInputStream.java:160) 
  at org.apache.commons.net.io.FromNetASCIIInputStream._read(FromNetASCIIInputStream.java:77) 
  at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:175) 
  at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:138) 
  at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:177) 
  at java.io.BufferedInputStream.read(BufferedInputStream.java:259) 
  at org.apache.commons.net.telnet.TelnetInputStream._read(TelnetInputStream.java:122) 
  at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:564) 
  at java.lang.Thread.run(Thread.java:1060)
</code></pre>

<p>And also from the thread calling <code>.disconnect()</code> (which for my test case is just on the main thread):</p>

<pre><code>DALVIK THREADS: "main" prio=5 tid=3 MONITOR | group="main" sCount=1 dsCount=0 s=N   obj=0x4001ab08 self=0xbc60 | sysTid=1390 nice=0 sched=0/0 handle=-1343996920 
  at java.io.BufferedInputStream.close(BufferedInputStream.java:~166) 
  at org.apache.commons.net.telnet.TelnetInputStream.close(TelnetInputStream.java:535) 
  at java.io.FilterInputStream.close(FilterInputStream.java:81) 
  at java.io.BufferedInputStream.close(BufferedInputStream.java:167) 
  at couk.mypackage.comm.TelnetClient.closeTelnet(IGSTelnetClient.java:441) 
  at couk.myoackage.comm.Translator$1.handleMessage(IGSTranslator.java:65) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:123) 
  at android.app.ActivityThread.main(ActivityThread.java:4203) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:521) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<h3>Update:</h3>

<p>If I close the output and input streams explicitly (which the javadoc says NOT to do), but to call <code>disconnect()</code> instead, then I can close the connection, i.e.,</p>

<pre><code>out.close();
in.close();
telnet.disconnect();
</code></pre>

<p>I'm confused!</p>
