如何通过Telnet发送字符串?

问题描述 投票:0回答:1

我想使用来自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。

java inputstream telnet outputstream
1个回答
0
投票

感谢某些程序员花花公子(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();
© www.soinside.com 2019 - 2024. All rights reserved.