• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Telnet server / multiple readline(s)

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this too, but i didn't help ...

 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your application - does the server-side actually use telnet protocol, or does it just send/receive raw data over the socket?
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic