0

I want to send a String to a Telnet Connection using TelnetConnection from Apache

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();
        }
    }
}

However, I don't get a result. How do I use Input and Output Stream in this case? The command ("start") should start the recording of METUS INGEST 5.6.

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

Thanks to Some programmer dude(https://stackoverflow.com/users/440558/some-programmer-dude). This totally did the job.

Here is the complete code:

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();
        }
    }
}

You can stop the recording of all sources with:

String stop = "stop\r\n";
telnet.getOutputStream().write(stop.getBytes());
telnet.getOutputStream().flush();