This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Apache TelnetClient() ignores OutputStream.write(byte[])  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

First time poster, long time reader.

I am using Apache's Telnet library to communicate with a telnet server, send commands, and print the entire communication to file.

I can connect to a server (always fails first time, I need a delay somewhere after client.connect() I guess), print a line break, and print the whole thing to file. However, sending any command other than line break seems to have no effect. I'm not sure if I'm sending the wrong command or if my problem is more fundamental.

Problem line is:
outputStream.write( //some byte array or byte );

This works to create line break:
outputStream.write( 10 );

No other valid command elicits any response.

I've already logged into telnet from terminal in MacOS and confirmed that everything works there.

Code:


Any feedback or direction is much appreciated. I'm at hair pulling stage.
 
Bartender
Posts: 20578
121
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're using a binary protocol for telnet, which is primarily a text protocol. So if I were doing it, I'd be using a PrintWriter output stream to the telnet server. And avoid binary/hex/octal bytes char(10) when a perfectly serviceable alternative ('\n') would do just as well. Or, in fact, when using a PrintWriter, any println() method would send the newline as the terminating character for the line.

Somewhat related to that - and probably your primary problem - is that likely nothing is being sent because you're using a buffered stream and buffered streams only actually transmit when either the buffer has filled or an explicit force command (flush()) has been used to initiate transmission of what's in the buffer. Another reason for using print streams - a "println()" carries an implicit flush() operation.
 
andrew mcnamara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, Tim.

Staying away from binary is a good idea. I gave this a try:

       OutputStream outputStream = client.getOutputStream();
       PrintWriter printWriter = new PrintWriter( outputStream );

then, in the read thread(), I modify my write command. I tried this variants:

       printWriter.println( "401" );
       printWriter.print( "401\n" );
       printWriter.write( "401" );

I tried each of these with and without a manual printWriter.flush() with no success.

I also tried:
       
       PrintWriter printWriter = new PrintWriter( new OutputStreamWriter(client.getOutputStream()) );

No go here either. I'll keep working on things. If anything else occurs to you, please do add! Thanks.

 
Tim Holloway
Bartender
Posts: 20578
121
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A) Why are you doing this in Threads? Telnet is a sequential protocol and generally synchronous. You either write or read, but not both.

B) I would expect that the read thread would read from the telnet reply port and print to the console (or something like that). That doesn't appear to be what you're trying to do.
 
andrew mcnamara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

Yes, threads are not really necessary here. I played with sequential reading of chars after connection (which works), writing to the server (which does not work, except line break), and reading again for any possible response (to which there was none).

At the moment, I have things printing to system and to file. Both of these prints are successful. System print now is just for debugging.

Since your post, I've tried OutputStream(), OutputStreamWriter(), PrintWriter(), and DataOutputStream(). None of these seem to work to send commands! Thanks for your help so far, much appreciated.
 
andrew mcnamara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved this problem.

Before writing a command to the server, one must check that the final character or string prompt has arrived.

For those out there trying to write a user name or password as a command (for example), first check for that string on the input stream before sending the response on the output stream. I guess this is especially relevant if using threads.



Thanks for your help, Tim!
 
Squanch that. And squanch this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!