发送字节命令到Apache TelnetClient [英] sending byte commands to Apache TelnetClient

查看:318
本文介绍了发送字节命令到Apache TelnetClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看:

http://www.asciitable.com/

我想送一个CHAR LF,十进制值10,也是一个CHR 1,其次,再次,通过LF十进制字节值49。 (我觉得这是在rainmaker.wunderground.com 3000 telnet服务器期望的顺序。)这是从标准输入捕获键盘输入。我没有明确使用Apache IOUtil 例如

要线程这是必要的读/写的Apache TelnetClient ,根据<一个href=\"http://svn.apache.org/repos/asf/commons/proper/net/tags/NET_1_0_0/src/java/examples/weatherTelnet.java\"相对=nofollow>为例WeatherTelnet计划。在 TelnetClient.sendCommand() API指定,它需要一个字节的说法,对此,我觉得我要送。但它是正确的字节?而正确发送?

第一个命令为Telnet天气服务器预期(见下文)是preSS进入,这我认为是一个LF,我拿的字节型通过= 10

它的看起来的像其他命令,比如整数,正在发送的命令,至少到目前为止,我可以告诉。任何帮助国米preting这个输出和调试这将是极大的AP preciated:

执行的JAR:

  thufir @杜尔:〜$
thufir @ DUR:〜$ Java的罐子的NetBeansProjects / SSCCE /距离/ SSCCE.jar
打印..
-------------------------------------------------- ----------------------------
*欢迎天气地下telnet服务! *
-------------------------------------------------- ----------------------------
* *
由奥尔登电子公司提供的*国家气象局信息*
*并报告了我们的数据饲料进来更新的每一分钟。 *
* *
* **注:如果你不能让过去这个开屏,你必须使用*
*不同版本的远程登录计划 - 一些药粥IBM的*
*兼容PC的有prevents正确连接的错误。 *
* *
*注释:jmasters@wunderground.com *
-------------------------------------------------- ----------------------------preSS Return键继续:preSS返回菜单
或者输入3个字母的预测城市code--                 地下气象MAIN MENU
                ******************************
                 1)美国天气预报和气候资料
                 2)加拿大预测
                 3)当前天气观测
                 4)滑雪条件
                 5)长期预报
                 6)最新报告地震
                 7)恶劣天气
                 8)飓风公告
                 9)在过去一个月天气摘要
                10)国际数据
                11)海洋预报和观测
                12)紫外线预报
                 X)退出程序
                 C)更改滚动屏幕
                 高)帮助和为新用户信息
                 ?)回答你所有的问题
                   选择:1
送49 CMD 1          没有一个有效的选项。键入一个数字1至12。                 地下气象MAIN MENU
                ******************************
                 1)美国天气预报和气候资料
                 2)加拿大预测
                 3)当前天气观测
                 4)滑雪条件
                 5)长期预报
                 6)最新报告地震
                 7)恶劣天气
                 8)飓风公告
                 9)在过去一个月天气摘要
                10)国际数据
                11)海洋预报和观测
                12)紫外线预报
                 X)退出程序
                 C)更改滚动屏幕
                 高)帮助和为新用户信息
                 ?)回答你所有的问题
                   选择:1
送49 CMD 1          没有一个有效的选项。键入一个数字1至12。                 地下气象MAIN MENU
                ******************************
                 1)美国天气预报和气候资料
                 2)加拿大预测
                 3)当前天气观测
                 4)滑雪条件
                 5)长期预报
                 6)最新报告地震
                 7)恶劣天气
                 8)飓风公告
                 9)在过去一个月天气摘要
                10)国际数据
                11)海洋预报和观测
                12)紫外线预报
                 X)退出程序
                 C)更改滚动屏幕
                 高)帮助和为新用户信息
                 ?)回答你所有的问题
                   选择:^ Cthufir @杜尔:〜$
thufir @ DUR:〜$
thufir @ DUR:〜$
thufir @ DUR:〜$

code:

 包weathertelnet;进口java.io.BufferedReader中;
引入静态java.lang.System.out;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.net.InetAddress中;
进口java.net.SocketException异常;
进口java.util.concurrent.ConcurrentLinkedQueue中;
进口java.util.logging.Logger中;
进口org.apache.commons.net.telnet.TelnetClient;公共final类的Telnet {    私人最终静态记录仪LOG = Logger.getLogger(Telnet.class.getName());
    私人TelnetClient telnetClient =新TelnetClient();    公众的Telnet()抛出SocketException,请IOException异常{
        InetAddress的主机= InetAddress.getByName(rainmaker.wunderground.com);
        INT端口= 3000;
        telnetClient.connect(主机,端口);        最终的InputStream的InputStream = telnetClient.getInputStream();
        最终的ConcurrentLinkedQueue&LT;性格&GT; CLQ =新的ConcurrentLinkedQueue();
        最后StringBuilder的SB =新的StringBuilder();        主题打印=新的Thread(){            @覆盖
            公共无效的run(){
                通过out.println(打印..);
                尝试{
                    焦炭CH =(char)的inputStream.read();
                    而(255&GT; CH&放大器;&安培; CH&GT; = 0){
                        clq.add(CH);
                        的out.print(CH);
                        CH =(char)的inputStream.read();
                    }
                }赶上(IOException异常前){
                    通过out.println(不能读取的InputStream:\\ t+ EX);
                }
            }
        };
        线程读取=新主题(){            @覆盖
            公共无效的run(){
                在的BufferedReader =新的BufferedReader(新的InputStreamReader(System.in));
                尝试{
                    做{
                        字符串命令= in.readLine();
                        字节[]字节= command.getBytes();
                        字节B = 0;
                        的for(int i = 0; I&LT; bytes.length;我++){
                            B =字节[I];
                            字符串CMD =新的String(字节);
                            telnetClient.sendCommand(二);
                            通过out.println(发送\\ t+ B +\\ TCMD \\ t+ CMD);
                        }
                        B = 10;
                        telnetClient.sendCommand(二);
                    }而(真);
                }赶上(IOException异常前){
                }
            }
        };
        print.start();
        read.start();
    }    公共静态无效的主要(字串[] args)抛出SocketException,请IOException异常{
        新的Telnet();
    }
}

普通电话

 网​​:thufir @ DUR:〜$
thufir @ DUR:〜$
thufir @ DUR:〜$远程登录rainmaker.wunderground.com 3000
尝试38.102.137.140 ...
连接到rainmaker.wunderground.com。
转义字符是'^]'。
-------------------------------------------------- ----------------------------
*欢迎天气地下telnet服务! *
-------------------------------------------------- ----------------------------
* *
由奥尔登电子公司提供的*国家气象局信息*
*并报告了我们的数据饲料进来更新的每一分钟。 *
* *
* **注:如果你不能让过去这个开屏,你必须使用*
*不同版本的远程登录计划 - 一些药粥IBM的*
*兼容PC的有prevents正确连接的错误。 *
* *
*注释:jmasters@wunderground.com *
-------------------------------------------------- ----------------------------preSS Return键继续:preSS返回菜单
或者输入3个字母的预测城市code--                 地下气象MAIN MENU
                ******************************
                 1)美国天气预报和气候资料
                 2)加拿大预测
                 3)当前天气观测
                 4)滑雪条件
                 5)长期预报
                 6)最新报告地震
                 7)恶劣天气
                 8)飓风公告
                 9)在过去一个月天气摘要
                10)国际数据
                11)海洋预报和观测
                12)紫外线预报
                 X)退出程序
                 C)更改滚动屏幕
                 高)帮助和为新用户信息
                 ?)回答你所有的问题
                   选择:1                         城市的天气预测MENU
                -------------------------------------------------- -
                1)选择城市打印预测
                2)打印所选城市的气象数据
                3)显示3个字母的城市codeS为选中状态
                4)显示所有2个字母的状态codeS
                M)返回主菜单
                X)退出程序
                ?) 帮帮我
                   选择:X
连接国外主机关闭。
thufir @ DUR:〜$
thufir @ DUR:〜$


解决方案

 包SSCCE;进口java.io.BufferedReader中;
进口java.io.OutputStream中;
引入静态java.lang.System.out;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.net.InetAddress中;
进口java.net.SocketException异常;
进口java.util.logging.Logger中;
进口org.apache.commons.net.telnet.TelnetClient;公共final类的Telnet {    私人最终静态记录仪LOG = Logger.getLogger(Telnet.class.getName());
    私人TelnetClient telnetClient =新TelnetClient();    公众的Telnet()抛出SocketException,请IOException异常{
        InetAddress的主机= InetAddress.getByName(rainmaker.wunderground.com);
        INT端口= 3000;
        telnetClient.connect(主机,端口);        最终的InputStream的InputStream = telnetClient.getInputStream();
        最终的OutputStream的OutputStream = telnetClient.getOutputStream();        主题打印=新的Thread(){            @覆盖
            公共无效的run(){
                通过out.println(打印..);
                尝试{
                    焦炭CH =(char)的inputStream.read();
                    而(255&GT; CH&放大器;&安培; CH&GT; = 0){
                        的out.print(CH);
                        CH =(char)的inputStream.read();
                    }
                }赶上(IOException异常前){
                    通过out.println(不能读取的InputStream:\\ t+ EX);
                }
            }
        };
        线程读取=新主题(){            @覆盖
            公共无效的run(){
                的BufferedReader bufferedInput =新的BufferedReader(新的InputStreamReader(System.in));
                尝试{
                    做{
                        字节B = 10;
                        outputStream.write(10);
                        outputStream.flush();
                        字符串命令= bufferedInput.readLine();
                        字节[]字节= command.getBytes();
                        outputStream.write(字节);
                        outputStream.flush();
                    }而(真);
                }赶上(IOException异常前){
                }
            }
        };
        print.start();
        read.start();
    }    公共静态无效的主要(字串[] args)抛出SocketException,请IOException异常{
        新的Telnet();
    }
}

Looking at:

http://www.asciitable.com/

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 think 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 example.

It's necessary to thread read/writes to Apache TelnetClient, based on an example WeatherTelnet program. The TelnetClient.sendCommand() API specifies that it takes a byte argument, which, I think 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 seems 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:~$ 

解决方案

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();
    }
}

这篇关于发送字节命令到Apache TelnetClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆