java - 如何通过 Telnet 发送字符串?
问题描述
我想使用来自 Apache 的 TelnetConnection 将字符串发送到 Telnet 连接
import java.io.IOException;
import org.apache.commons.net.telnet.TelnetClient;
public class TestClass {
public static void main(String[] args) throws IOException, InterruptedException {
String telnetServer = "123.456.789.123";
int telnetPort = 32106;
TelnetClient telnet = new TelnetClient();
try {
telnet.connect(telnetServer, telnetPort);
String start = "start";
telnet.getOutputStream().write(start.getBytes());
telnet.getOutputStream().flush();
System.out.println(telnet.getInputStream());
} catch (Exception e) {
System.out.println(e);
}finally {
telnet.disconnect();
}
}
}
但是,我没有得到结果。在这种情况下如何使用输入和输出流?命令(“开始”)应该开始记录 METUS INGEST 5.6。
解决方案
感谢一些程序员老兄(https://stackoverflow.com/users/440558/some-programmer-dude)。这完全完成了工作。
这是完整的代码:
import java.io.IOException;
import org.apache.commons.net.telnet.TelnetClient;
public class TestClass {
public static void main(String[] args) throws IOException, InterruptedException {
String telnetServer = "123.456.789.123";
int telnetPort = 32106;
TelnetClient telnet = new TelnetClient();
try {
telnet.connect(telnetServer, telnetPort);
String start = "start\r\n";
telnet.getOutputStream().write(start.getBytes());
telnet.getOutputStream().flush();
System.out.println(telnet.getInputStream());
} catch (Exception e) {
System.out.println(e);
}finally {
telnet.disconnect();
}
}
}
您可以通过以下方式停止所有源的录制:
String stop = "stop\r\n";
telnet.getOutputStream().write(stop.getBytes());
telnet.getOutputStream().flush();
推荐阅读
- php - 如何在 WooCommerce 中创建有效的自定义产品查询?
- python - tensorflow 2.1.0 的安装错误
- progressive-web-apps - 异步模块定义和渐进式 Web 应用程序之间有什么关系?
- python - 如何从python中的两个单独的文本文件中提取基于子字符串的行?
- javascript - 无法从从 Promise 获得的对象中检索值
- asp.net-core - 在 F# 中使用“IHttpClientFactory”的常规方法是什么?
- makefile - 是否预期 bitbake 立即分配行为?
- docx-wasm - 如何理解 wasm 中的 br_if?
- android - 要导入哪个类来声明工具栏变量 unsing androidx
- python - Scrapy:如何使用 CSS 和 XPath 获取地址?