41753

sending byte commands to Apache TelnetClient

Question:

Looking at:

<a href="http://www.asciitable.com/" rel="nofollow">http://www.asciitable.com/</a>

I want to send a CHAR LF, decimal value 10, and also a Chr 1, decimal byte value 49. Followed, again, by a LF. (I <strong>think</strong> that is the sequence which the telnet server at rainmaker.wunderground.com 3000 expects.) This is keyboard entry which is captured from standard input. I'm explicitly not using the Apache IOUtil <a href="http://svn.apache.org/repos/asf/commons/proper/net/trunk/src/main/java/examples/util/IOUtil.java" rel="nofollow">example</a>.

It's necessary to thread read/writes to Apache TelnetClient, based on <a href="http://svn.apache.org/repos/asf/commons/proper/net/tags/NET_1_0_0/src/java/examples/weatherTelnet.java" rel="nofollow">an example WeatherTelnet program</a>. The TelnetClient.sendCommand() <a href="http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetClient.html#sendCommand%28byte%29" rel="nofollow">API specifies</a> that it takes a byte argument, which, I <strong>think</strong> I'm sending. But is it the correct byte? And sent correctly?

The first command expected by the telnet weather server (see below) is to press enter, which I take to be a LF, which I take as Byte by = 10.

It <em>seems</em> like the other commands, like integers, are being sent as commands, at least so far as I can tell. Any help interpreting this output and debugging it would be greatly appreciated:

Executing the JAR:

thufir@dur:~$ thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/SSCCE.jar print.. ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * ------------------------------------------------------------------------------ Press Return to continue: Press Return for menu or enter 3 letter forecast city code-- WEATHER UNDERGROUND MAIN MENU ****************************** 1) U.S. forecasts and climate data 2) Canadian forecasts 3) Current weather observations 4) Ski conditions 5) Long-range forecasts 6) Latest earthquake reports 7) Severe weather 8) Hurricane advisories 9) Weather summary for the past month 10) International data 11) Marine forecasts and observations 12) Ultraviolet light forecast X) Exit program C) Change scrolling to screen H) Help and information for new users ?) Answers to all your questions Selection:1 sent 49 cmd 1 Not a valid option. Type a number 1 to 12. WEATHER UNDERGROUND MAIN MENU ****************************** 1) U.S. forecasts and climate data 2) Canadian forecasts 3) Current weather observations 4) Ski conditions 5) Long-range forecasts 6) Latest earthquake reports 7) Severe weather 8) Hurricane advisories 9) Weather summary for the past month 10) International data 11) Marine forecasts and observations 12) Ultraviolet light forecast X) Exit program C) Change scrolling to screen H) Help and information for new users ?) Answers to all your questions Selection:1 sent 49 cmd 1 Not a valid option. Type a number 1 to 12. WEATHER UNDERGROUND MAIN MENU ****************************** 1) U.S. forecasts and climate data 2) Canadian forecasts 3) Current weather observations 4) Ski conditions 5) Long-range forecasts 6) Latest earthquake reports 7) Severe weather 8) Hurricane advisories 9) Weather summary for the past month 10) International data 11) Marine forecasts and observations 12) Ultraviolet light forecast X) Exit program C) Change scrolling to screen H) Help and information for new users ?) Answers to all your questions Selection:^Cthufir@dur:~$ thufir@dur:~$ thufir@dur:~$ thufir@dur:~$

code:

package weathertelnet; import java.io.BufferedReader; import static java.lang.System.out; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.SocketException; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Logger; import org.apache.commons.net.telnet.TelnetClient; public final class Telnet { private final static Logger LOG = Logger.getLogger(Telnet.class.getName()); private TelnetClient telnetClient = new TelnetClient(); public Telnet() throws SocketException, IOException { InetAddress host = InetAddress.getByName("rainmaker.wunderground.com"); int port = 3000; telnetClient.connect(host, port); final InputStream inputStream = telnetClient.getInputStream(); final ConcurrentLinkedQueue<Character> clq = new ConcurrentLinkedQueue(); final StringBuilder sb = new StringBuilder(); Thread print = new Thread() { @Override public void run() { out.println("print.."); try { char ch = (char) inputStream.read(); while (255 > ch && ch >= 0) { clq.add(ch); out.print(ch); ch = (char) inputStream.read(); } } catch (IOException ex) { out.println("cannot read inputStream:\t" + ex); } } }; Thread read = new Thread() { @Override public void run() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { do { String command = in.readLine(); byte[] bytes = command.getBytes(); byte b = 0; for (int i = 0; i < bytes.length; i++) { b = bytes[i]; String cmd = new String(bytes); telnetClient.sendCommand(b); out.println("sent\t" + b + "\tcmd\t" + cmd); } b=10; telnetClient.sendCommand(b); } while (true); } catch (IOException ex) { } } }; print.start(); read.start(); } public static void main(String[] args) throws SocketException, IOException { new Telnet(); } }

regular tel

net: thufir@dur:~$ thufir@dur:~$ thufir@dur:~$ telnet rainmaker.wunderground.com 3000 Trying 38.102.137.140... Connected to rainmaker.wunderground.com. Escape character is '^]'. ------------------------------------------------------------------------------ * Welcome to THE WEATHER UNDERGROUND telnet service! * ------------------------------------------------------------------------------ * * * National Weather Service information provided by Alden Electronics, Inc. * * and updated each minute as reports come in over our data feed. * * * * **Note: If you cannot get past this opening screen, you must use a * * different version of the "telnet" program--some of the ones for IBM * * compatible PC's have a bug that prevents proper connection. * * * * comments: jmasters@wunderground.com * ------------------------------------------------------------------------------ Press Return to continue: Press Return for menu or enter 3 letter forecast city code-- WEATHER UNDERGROUND MAIN MENU ****************************** 1) U.S. forecasts and climate data 2) Canadian forecasts 3) Current weather observations 4) Ski conditions 5) Long-range forecasts 6) Latest earthquake reports 7) Severe weather 8) Hurricane advisories 9) Weather summary for the past month 10) International data 11) Marine forecasts and observations 12) Ultraviolet light forecast X) Exit program C) Change scrolling to screen H) Help and information for new users ?) Answers to all your questions Selection:1 CITY FORECAST MENU --------------------------------------------------- 1) Print forecast for selected city 2) Print climatic data for selected city 3) Display 3-letter city codes for a selected state 4) Display all 2-letter state codes M) Return to main menu X) Exit program ?) Help Selection:x Connection closed by foreign host. thufir@dur:~$ thufir@dur:~$

Answer1:

package sscce; import java.io.BufferedReader; import java.io.OutputStream; import static java.lang.System.out; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.SocketException; import java.util.logging.Logger; import org.apache.commons.net.telnet.TelnetClient; public final class Telnet { private final static Logger LOG = Logger.getLogger(Telnet.class.getName()); private TelnetClient telnetClient = new TelnetClient(); public Telnet() throws SocketException, IOException { InetAddress host = InetAddress.getByName("rainmaker.wunderground.com"); int port = 3000; telnetClient.connect(host, port); final InputStream inputStream = telnetClient.getInputStream(); final OutputStream outputStream = telnetClient.getOutputStream(); Thread print = new Thread() { @Override public void run() { out.println("print.."); try { char ch = (char) inputStream.read(); while (255 > ch && ch >= 0) { out.print(ch); ch = (char) inputStream.read(); } } catch (IOException ex) { out.println("cannot read inputStream:\t" + ex); } } }; Thread read = new Thread() { @Override public void run() { BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(System.in)); try { do { byte b = 10; outputStream.write(10); outputStream.flush(); String command = bufferedInput.readLine(); byte[] bytes = command.getBytes(); outputStream.write(bytes); outputStream.flush(); } while (true); } catch (IOException ex) { } } }; print.start(); read.start(); } public static void main(String[] args) throws SocketException, IOException { new Telnet(); } }

Recommend

  • java.lang.NoClassDefFoundError while running jar
  • Moq and setting up DB Context
  • How to compute the result of 8086 IDIV instruction?
  • getting the error “ERROR: EOF” when i execute a Heroku command “heroku login” in ubuntu?
  • php studio
  • Creating foreign key in MySQL
  • RAdwords error (“server certificate verification failed”)
  • Can I run mySQL when my computer is off?
  • My Produce Consumer Hangs
  • get logs of login and logout of windows users
  • How to make local USB port to work with Azure virtual machine
  • How to set repeating alarm using setExact and how to cancel the same?
  • InputDispatcher Error
  • Background image does not show on iPhone/iPad
  • how to display data by using AJAX?
  • How can I write a where clause in SQL to filter a DATETIME column by the time of day?
  • Timer once a minute on the minute
  • Firestore - Checking The Connection Status Of The Module To The Server
  • xpages partial - full search
  • how to automatically enter password when using ssh?
  • Can you use DataSet and DataTables in a Portable Class Library
  • Checkpointing In ALS Spark Scala
  • How to make SASS put relative paths in its output
  • Consuming a web service with the Netbeans Platform
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • Classic ASP URL Rewriting
  • Count New Lines in Text File
  • Sybase Error Implicit Conversion from datatype 'VARCHAR' to 'INT' not allowed
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Trouble saving JSON data for use by multiple UIViews [duplicate]
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Is there a amazon webstore API for customers?
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • How to get next/previous record number?
  • Confusion with PayPal's monthly billing cycle
  • Memory offsets in inline assembly
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app