Problem using org.apache.commons.net.telnet.* with 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!
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 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.
Answered by: Aida955 | Posted: 25-02-2022Similar questions
java - org.apache.commons.net.nntp crashing android
Hay Guy, I'm using org.apache.commons.net.nntp 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 org.apache.commons.net.nntp.*;
public class newsdroid extends Activity {
NNTP usenet; /** Called when the activity is fir...
java - The import org.apache.commons.net cannot be resolved
Why am I getting this error? I already imported the jar file in my eclipse project.
android - org.apache.commons.net.ftp.FTPClient.storeFile freezing interaction and animations
I'm using org.apache.commons.net.ftp.FTPClient 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: org.apache.commons.net.ftp.FTPClient Android Studio
I know that this question has some replies as in:
one
two
java - Error when logging into FTP from AsyncTask class using org.apache.commons.net.ftp.FTPClient
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 'org.apache.commons.net.ftp.FTPClient', 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 org.apache.commons.net FTPSClient connect very slow
I am using the org.apache.commons.net.FTPSClient 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: org.apache.commons.net.ftp.FTPClient
I'm trying to upload an image to ftp server using this AsyncTask:
import android.content.Context;
import android.os.AsyncTask;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.FileInputStream;
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) | Dev.io Android