English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I know that there are several APIs available to do this but I want to know whether is there any way to carry out telnet / ftp using Runtime.exec() command in java. Because what I am doing is not working-
I created the process as follow:
Runtime rt=Runtime.getRuntime();
Process p=rt.exec("telnet 12.42.28.123 25");

I got the input stream of this process by:
InputStreamReader isr1=new InputStreamReader
(p.getInputStream());

I got the error stream of this process by:
InputStreamReader isr2=new InputStreamReader
(p.getErrorStream());

I got the output stream of this process by:
PrintWriter pw=new PrintWriter
( p.getOutputStream() );

Now I should be able to read the input stream as well as error stream of this process. Ishould also be able to write further commands on the output stream of this process but neither i/p,err stream, nor o/p stream is working and the function p.waitFor() always returns -1. I am using Windows XP operating system with java version

2007-07-09 02:36:49 · 3 answers · asked by Mickey 1 in Computers & Internet Programming & Design

3 answers

Telnet (at least on Unix where I'm more familiar with it) is not really amenable to running in a sub-process, as it doesn't use stdin/stdout -- it opens a connection direct to the terminal to do I/O.

Assuming that Microsoft borrowed the Unix implementation, which I'd guess they did, the Windows version might well behave similarly. I note that I can use "FTP" with rt.exec() and it works fine. It's just telnet that doesn't behave well.

2007-07-09 03:29:43 · answer #1 · answered by McFate 7 · 0 0

Have you tried your command on the run menu ?
what do you see ? Do you see a window waiting for you to enter more commands?if not then how woould you expect java to do that ? java here is only providing the interface and means to acheive the job done. Probably you should first check if the IP address is valid and you able to connect using a normal routine.
if you are not then that is why the commands are not working for you.
p.waitfor is a Blocking mehtod and will wait untill the process returns with an Exit code. Do you want to do that ? Since you say you want the process to read in more commands , then the code you have written is wrong.

First get the input stream and check if you have any success connecttion to the server. if yes then step it up with more commands by pushing it in the Output stream. error stream and input stream will be same here and you will get the results in both ..so no need to open 2 streams here.
waitfor as i explained will wait for the process to exit. In your current case since telnet or FTP is not working you are getting an exit code of -1 Always. you get a exit code of 0 from the process if its a normal termination.

try it again now, with this knowledge i think you can do a better job.

Good Luck,

2007-07-09 06:17:08 · answer #2 · answered by Satya 3 · 0 0

the attention of a needle isn't a stitching needle. In historical situations walled cities had entrances referred to as eye of a needle. those have been low narrow entrances that for the duration of basic terms allowed human beings to circulate into the city one by one. This became performed to thwart invading armies via proscribing the quantity of invading troops which could enter at the same time. retailers constantly had to go away their camels exterior the city partitions because of the fact camels have been too great to bypass for the duration of the attention of a needle. So, you are able to bypass an entire carton of camel cigarettes for the duration of the attention of a needle with none issues. Your quandary is resolved.

2016-10-20 09:57:39 · answer #3 · answered by matchett 4 · 0 0

fedest.com, questions and answers