Forums Register Login

Telnet server / multiple readline(s)

+Pie Number of slices to send: Send
Hello,

My app has to connect to a telnet server.
When connection is on the app start sending some commands and need to read response with readline.

My problem is that app is sending a couple of request and I don't know how to read the answer to that specific command.

How can I be sure of that ??

this is the code:
+Pie Number of slices to send: Send
This looks like Android code, so I'll move it to the Android forum.

The first thing to do is to read this article: Don't println to a socket.

You should also check out the Apache Commons Net library, which has a Telnet client built in; that should work on Android just fine.
+Pie Number of slices to send: Send
I tried this too, but i didn't help ...

+Pie Number of slices to send: Send
Using a separate thread is likely not the answer. And the code still uses println in a way that's at odds with the article I mentioned - are the circumstances such that you can rule out that as a problem?

Also, can you not use a ready-to-use 3rd party library?
+Pie Number of slices to send: Send
Thanks.
I installed Common Net Library.

This is the only doc file:


This is a simple example of use of TelnetClient. An external option handler (SimpleTelnetOptionHandler) is used. Initial configuration requested by TelnetClient will be: WILL ECHO, WILL SUPPRESS-GA, DO SUPPRESS-GA. VT100 terminal type will be subnegotiated.

Also, use of the sendAYT(), getLocalOptionState(), getRemoteOptionState() is demonstrated. When connected, type AYT to send an AYT command to the server and see the result. Type OPT to see a report of the state of the first 25 options.

author: Bruno - D'Avanzo






How can I implement that ???




Tim Moores wrote:Using a separate thread is likely not the answer. And the code still uses println in a way that's at odds with the article I mentioned - are the circumstances such that you can rule out that as a problem?

Also, can you not use a ready-to-use 3rd party library?

 
+Pie Number of slices to send: Send
With Commons Net installed, I tried these simple test but I get an exception:





04-03 09:29:28.040 2754-2754/? W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
04-03 09:29:28.040 2754-2754/? W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
04-03 09:29:28.040 2754-2754/? W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
04-03 09:29:28.040 2754-2754/? W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
04-03 09:29:28.040 2754-2754/? W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
04-03 09:29:28.040 2754-2754/? W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
04-03 09:29:28.040 2754-2754/? W/System.err: at java.net.Socket.connect(Socket.java:882)
04-03 09:29:28.040 2754-2754/? W/System.err: at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
04-03 09:29:28.040 2754-2754/? W/System.err: at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
04-03 09:29:28.040 2754-2754/? W/System.err: at it.rockopera.scsremote.MainActivity$6.onClick(MainActivity.java:385)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.view.View.performClick(View.java:4756)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.view.View$PerformClick.run(View.java:19749)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.os.Looper.loop(Looper.java:135)
04-03 09:29:28.040 2754-2754/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5221)
04-03 09:29:28.040 2754-2754/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-03 09:29:28.040 2754-2754/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
04-03 09:29:28.040 2754-2754/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-03 09:29:28.040 2754-2754/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-03 09:29:28.040 2754-2754/? E/TELNET: no



Any hints ???
+Pie Number of slices to send: Send
 

simone giusti wrote:With Commons Net installed, I tried these simple test but I get an exception .. Any hints ???


Are you are running this code in the main/UI thread? If you are, then try running it in an AsyncTask instead - network operations aren't normally allowed on the main thread.

For testing though, you should be able to alter the thread policy as you did in your first two examples to permit network operations on the main thread - but this may cause your UI to become unresponsive.
+Pie Number of slices to send: Send
That's not the whole stacktrace - the exception itself is missing, so it's hard to tell what it's trying to tell you.

Does the app have network permission?
+Pie Number of slices to send: Send
I moved code out of main thread and it works (connection is ok).

I can't find information on using this library.

I have 3 simple questions:

1) once the connection is on, how can i send a command to server (plain text)
2) how can I read server response (plain text)
3) have I to implement multithreading to solve the problem I started from or this library offers this function builded-in ??


Thanks !!



Ron McLeod wrote:

simone giusti wrote:With Commons Net installed, I tried these simple test but I get an exception .. Any hints ???


Are you are running this code in the main/UI thread? If you are, then try running it in an AsyncTask instead - network operations aren't normally allowed on the main thread.

For testing though, you should be able to alter the thread policy as you did in your first two examples to permit network operations on the main thread - but this may cause your UI to become unresponsive.

 
+Pie Number of slices to send: Send
 

simone giusti wrote:... I can't find information on using this library.
... once the connection is on, how can i send a command to server (plain text)
... how can I read server response (plain text)


Have to looked at the Apache Commons Net documentation? It has a couple of example telnet client applications.


simone giusti wrote:... have I to implement multithreading to solve the problem I started from or this library offers this function builded-in ??


If you need your connection to be long-lived, staying connected and interacting with the server even when the UI is not visible, a Bound Service might be a good fit.
+Pie Number of slices to send: Send
 

Tim Moores wrote:

Also, can you not use a ready-to-use 3rd party library?



so ... which is the difference between using Commons Apache library and Java Socket I used before ??? is Commons multithread ?
+Pie Number of slices to send: Send
Socket provides a TCP-based communcation channel between endpoints. Apache Commons Net builds on top of that by providing the client-side implementation of various application-level protcols (Telnet, FTP, NTP, etc.). If you were to just use Socket, you would need to support the telnet protcol yourself.
+Pie Number of slices to send: Send
In your application - does the server-side actually use telnet protocol, or does it just send/receive raw data over the socket?
+Pie Number of slices to send: Send
 

Ron McLeod wrote:In your application - does the server-side actually use telnet protocol, or does it just send/receive raw data over the socket?



I think it is a telnet app. I tried my client with several server app, checking logs.

No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2322 times.
Similar Threads
Telnet Client
NullPointerException when calling connection Object
retrieve images from the web
problem comparing 2 arraylist values in android
Parsing XML <media:content> tag problem.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:56:36.