Java 向Apache TelnetClient发送字节命令

Java 向Apache TelnetClient发送字节命令,java,api,telnet,apache-commons,Java,Api,Telnet,Apache Commons,看看: 我想发送一个字符LF,十进制值10,还有一个CHR1,十进制字节值49。接着又是一个LF。(我认为这是rainmaker.wunderground.com 3000上telnet服务器所期望的顺序。)这是从标准输入捕获的键盘输入。我没有使用ApacheIOUtil 有必要基于线程对ApacheTelnetClient执行读/写操作。TelnetClient.sendCommand()表示它接受一个字节参数,我认为这是我正在发送的。但它是正确的字节吗?发送正确吗 telnet气象服务器(

看看:

我想发送一个字符LF,十进制值10,还有一个CHR1,十进制字节值49。接着又是一个LF。(我认为这是rainmaker.wunderground.com 3000上telnet服务器所期望的顺序。)这是从标准输入捕获的键盘输入。我没有使用Apache
IOUtil

有必要基于线程对Apache
TelnetClient
执行读/写操作。
TelnetClient.sendCommand()
表示它接受一个字节参数,我认为这是我正在发送的。但它是正确的字节吗?发送正确吗

telnet气象服务器(见下文)需要的第一个命令是按enter键,我认为它是LF,我认为它是
Byte by=10

看起来其他命令,比如整数,都是作为命令发送的,至少就我所知。如果您能帮助解释此输出并对其进行调试,我们将不胜感激:

执行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:~$ 
代码:


我是否应该使用:maybe?复制的或类似的:参见
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: 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();
    }
}