2

I'm currently attempting to make an application that will connect to a telnet server on startup. Currently I am attempting code similar to:

String ss = null;
Process p = Runtime.getRuntime().exec("cmd /c telnet localhost 4445");
BufferedWriter writeer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writeer.write("telnet localhost 4445");
writeer.flush();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
while ((ss = stdInput.readLine()) != null) {
    System.out.println(ss);
}

However, this does not open a terminal, though I assume it runs the desired command. I would like a terminal to open, a command to be done, shown in the 2nd line, and then leave the terminal open for user use.

Is there a way to do this? I have been trying for a while now and no solutions I can find actually give the desired output.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
F Mckinnon
  • 367
  • 1
  • 4
  • 9
  • Have just tried a plain socket? https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html – Paul Bastide May 10 '19 at 11:35
  • @PaulBastide A regular socket is a good idea, unless the server is actually a telnet server. Telnet is a protocol which supports many communication options using the 0xff byte value as an escape character. – VGR May 10 '19 at 11:46
  • https://stackoverflow.com/q/5738259/238704 – President James K. Polk May 10 '19 at 12:54

1 Answers1

1

Hi what about using start ,

 Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"ping localhost && telnet localhost 4445\"");

Will open a new window. But you'll not have access to the process since it is diffrent process initiated by exec.Still you can execute multip commands.

Srinivasan Sekar
  • 2,049
  • 13
  • 22