Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem using org.apache.commons.net.telnet.* with android

Tags:

java

android

I'm having a problem using the org.apache.commons.net.telnet.* library in my Android application and I was hoping someone could help me.

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 TelnetClient.disconnect() 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:

TelnetClient telnet = new TelnetClient();
telnet.connect(ipAddress, port); 
telnet.disconnect(); //HERE

Could anyone enlighten me as to why this may be?

If I obtain an InputStream 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 .connect(..), which makes me think there is some sort or lock being held somewhere?

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

Thanks for any help, stack traces below!

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

"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)

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

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)

Update:

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

out.close();
in.close();
telnet.disconnect();

I'm confused!

like image 633
Dori Avatar asked Jan 07 '10 10:01

Dori


1 Answers

TelentClient extends Telnet and Telnet extends SocketClient. If you see these classes, it will create VT100 connection. See the below code from TelnetClient.java.

 public TelnetClient() {
   /* TERMINAL-TYPE option (start)*/
   super ("VT100");
   /* TERMINAL-TYPE option (end)*/
   __input = null;
   __output = null;
 }

This will now create the super classes. See the code from Telnet.java class.

Telnet() {
      setDefaultPort(DEFAULT_PORT);
      _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
      _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
      _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];
      optionHandlers = new TelnetOptionHandler[TelnetOption.MAX_OPTION_VALUE + 1];
}

The DEFAULT_PORT here is 23.

Now, see the connect method from SocketClient.java.

        public void connect(InetAddress host, int port)
                throws SocketException, IOException {
            _socket_ = _socketFactory_.createSocket(host, port);
            _connectAction_();
        }

This is creating a new Socket using DefaultSocketFactory, which is a TCP socket to specified Host and Port. In the connectAction method, its setting the timeout for the new socket.

Now, when we call disconnect, its first trying to close the socket.

public void disconnect() throws IOException {
            _socket_.close();
            _input_.close();
            _output_.close();
            _socket_ = null;
            _input_ = null;
            _output_ = null;
            _isConnected_ = false;
        }

Because you are able to close the Streams directly, so seems the problem with the socket itself.

This will be a TCP socket, and i don't know how Android behaves with TCP. Can you please check on that? Because this method is doing closing of 3 private members. OR may be the socket is already closed so that it can't close it again?

Hope this helps.

Thanks.

like image 110
Parth Avatar answered Oct 21 '22 17:10

Parth