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

问题描述

看着:

http://www.asciitable.com/

我想发送一个 CHAR LF,十进制值 10,以及一个 Chr 1,十进制字节值 49.再次跟随一个 LF.(我认为这是rainmaker.wunderground.com 3000 上的telnet 服务器所期望的序列.)这是从标准输入中捕获的键盘输入.我明确没有使用 Apache IOUtil 示例.

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.

有必要基于 .TelnetClient.sendCommand() API 指定它需要一个字节参数,我认为我正在发送.但它是正确的字节吗?并正确发送?

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?

telnet 天气服务器期望的第一个命令(见下文)是按 Enter,我认为这是一个 LF,我认为它是 Byte by = 10.

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:

执行 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: [email protected]                              *
------------------------------------------------------------------------------

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:~$

代码:

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

普通电话

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: [email protected]                              *
------------------------------------------------------------------------------

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 发送字节命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:00