java telnet端口
时间: 2023-11-08 14:04:53 浏览: 100
要在Java中使用Telnet连接到端口,可以使用Java的Socket类。以下是一个简单的示例代码:
```java
import java.io.*;
import java.net.*;
public class TelnetExample {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 23); // 连接到本地的23端口(Telnet端口)
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello, Telnet!"); // 向Telnet服务器发送消息
String response = in.readLine(); // 读取Telnet服务器的响应
System.out.println("Response from Telnet server: " + response);
socket.close(); // 关闭连接
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文