【问题标题】:Java and Telnet resposnse unreadbleJava 和 Telnet 响应不可读
【发布时间】:2014-06-11 12:30:51
【问题描述】:

我正在尝试使用 telnet 进行连接,但服务器的响应是一个不可读的字符串,有人可以帮助我吗? (telnet 服务器是 windows XP),我想通过 telnet 进行身份验证并执行一些命令。 以下代码如下

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TelnetClient {

public static void main(String args[]) throws Exception{
    // Create object of Socket
    Socket soc = new Socket("192.168.56.101", 23);
    String Command;
    // Create object of Input Stream to read from socket
    DataInputStream din = new DataInputStream(soc.getInputStream());
    // Create object of Output Stream to write on socket
    DataOutputStream dout = new DataOutputStream(soc.getOutputStream());
    // Object of Buffered Reader to read command from terminal
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Welcome to Telnet Client");
    System.out.println("< Telnet Prompt >");
    Command = br.readLine();// reads the command
    dout.writeUTF(Command);// sends command to server
    System.out.println(din.readLine()); // gets the response of server
    soc.close(); // close port
    din.close(); // close input stream
    dout.close(); // close output stream
    br.close(); // close buffered Reader
}

}

服务器的响应是:ÿý%ÿûÿûÿý'ÿýÿý 有很多“?”

【问题讨论】:

    标签: java sockets telnet


    【解决方案1】:

    Telnet 不仅仅是一个“连接即走”协议,您还必须在连接时进行一些基本的握手和协商选项,例如,能够传输 8 位数据。您看到的是服务器尝试协商连接选项。

    rfc854there are a bunch of RFCs 中描述了基本协议(包括如何进行协商),描述了您可以协商的选项。简而言之,您需要在连接时与服务器进行一些基本协商(至少拒绝所有选项请求)以获取真实数据。

    【讨论】:

    • @user1763778 在 Java 中,我以前使用 Apache Commons Net 进行远程登录,它为您完成了所有困难的部分,尽管您仍然可以控制您想要实际使用的所有选项(例如 8 -位清洁数据)。
    • +1 我要补充一点,因为 Telnet 包含一个二进制协议,其中 0xff 非常重要,您应该使用 InputStream,而不是 Reader.
    猜你喜欢
    • 2011-12-20
    • 2016-06-01
    • 2015-08-24
    • 2022-07-06
    • 2018-07-10
    • 2013-04-21
    • 2016-02-23
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多