我org.apache.commons.net.telnet.*
在Android应用程序中使用该库时遇到问题,我希望有人可以帮助我.
我已经实现了一个使用telnet与远程服务器通信的应用程序,一切正常.我遇到的问题是当我调用TelnetClient.disconnect()
方法时没有返回.在运行测试用例时调用该方法(意思是没有Android,只需通过Eclipse),它会立即返回,但在Android上有些东西搞砸了.我的测试用例看起来像:
TelnetClient telnet = new TelnetClient(); telnet.connect(ipAddress, port); telnet.disconnect(); //HERE
有人可以告诉我为什么会这样吗?
如果我InputStream
从连接的telnet对象获取一个并且在调用disconnect之后从telnet服务器发送了一些信息,它可能导致该方法返回并关闭通过调用创建的comm线程.connect(..)
,这让我觉得有一些排序还是锁在某个地方?
Dalvik VM或Android操作系统与我的Java VM和XP机器之间的根本区别是什么?
感谢您的帮助,下面的堆栈跟踪!
我有一个来自在TelnetClient.connect()
ANR出现时创建的comm线程的堆栈跟踪(由于断开连接调用无限期挂起).
"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)
并且还从线程调用.disconnect()
(对于我的测试用例仅在主线程上):
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)
如果我显式关闭输出和输入流(javadoc说不要做),而是调用disconnect()
,那么我可以关闭连接,即,
out.close(); in.close(); telnet.disconnect();
我糊涂了!