【问题标题】:How to send a String via Telnet?如何通过 Telnet 发送字符串?
【发布时间】:2020-08-16 22:55:46
【问题描述】:

我想使用 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();
        }
    }
}

但是,我没有得到结果。在这种情况下如何使用输入和输出流? 命令 ("start") 应该开始记录 METUS INGEST 5.6。

【问题讨论】:

  • Telnet 之上的许多协议都是基于 line 的。也许您也应该发送换行符组合 ("\r\n")?

标签: java inputstream telnet outputstream


【解决方案1】:

感谢一些程序员老兄(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();

【讨论】:

    猜你喜欢
    • 2014-04-05
    • 2020-09-08
    • 2011-05-08
    • 2010-11-22
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2017-07-12
    • 2017-10-30
    相关资源
    最近更新 更多