Problem using* with android

I'm having a problem using the* 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( 

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 couk.mypackage.comm.TelnetClient.closeTelnet( 
  at couk.myoackage.comm.Translator$1.handleMessage( 
  at android.os.Handler.dispatchMessage( 
  at android.os.Looper.loop( 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke( 
  at dalvik.system.NativeStart.main(Native Method)


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.,


I'm confused!

Asked by: Melissa651 | Posted: 24-01-2022

Answer 1

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

 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 class.

Telnet() {
      _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

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

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_ = 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.


Answered by: Aida955 | Posted: 25-02-2022

Similar questions

java - crashing android

Hay Guy, I'm using to connect to a nntp server, however running a simple nntp.connect(host, port) crashes the android. Anyone got any ideas? Do java packages work with android straight out of the box? or do they need editing? Thanks import*; public class newsdroid extends Activity { NNTP usenet; /** Called when the activity is fir...

java - The import cannot be resolved

Why am I getting this error? I already imported the jar file in my eclipse project.

android - freezing interaction and animations

I'm using to communicate with an ftp server via an android app I'm making that records video and then uploads it to the ftp server. Everything is fine until I call storeFile at which point the app prevents any interaction until the uploading is completed. Is there any way around this? I'm currently developing for API lvl 12. My set up is as follows I have a class that calls a service in...

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: Android Studio

I know that this question has some replies as in: one two

java - Error when logging into FTP from AsyncTask class using

I am logging into the ftp account using AsyncTask but I keep getting this error, 12-31 15:26:19.637: E/dalvikvm(5986): Could not find class '', referenced from method com.example.testme.Processing.connnectingwithFTP I can understand that my method inside Asynctask is not able to find the FTPClient class eventhough it is imported fine. But I don't how to ...

android - Apache FTPSClient connect very slow

I am using the in Android and I am trying to connect to a FTP Server via FTPS. The connect method of the FTPSClient is very slow and this seems to depend on the Android Version. On a Nexus 6 with Android 6.0.1 the connect call needs 5 sec in average. On a Galaxy Nexus with Android 4.3 it only needs 1-2 sec in average. SSLContext sslContext; try ...

android - java.lang.NoClassDefFoundError:

I'm trying to upload an image to ftp server using this AsyncTask: import android.content.Context; import android.os.AsyncTask; import; import; import; import; public class my_ftp_uploader extends AsyncTask<String, Void, String> { public String file_name; private Context con...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android