我正在尝试使用 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
}
}
服务器的响应是:ÿý%ÿûÿûÿý'ÿýÿý,带有很多“?”
最佳答案
Telnet 不仅仅是一个“连接即可运行”协议(protocol),您必须在连接时进行一些基本的握手和协商选项,例如,能够传输 8 位数据。您所看到的是服务器尝试协商连接选项。
基本协议(protocol) - 包括如何完成协商 - 在 rfc854 中描述。和 there are a bunch of RFCs描述您可以协商的选项。简而言之,您需要在连接时与服务器进行一些基本协商(至少拒绝所有选项请求)才能获取真实数据。
关于Java 和 Telnet 响应不可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24154557/