简体   繁体   中英

telnet connection inputstream blocking why?

package burak;
import java.io.*;

public class telcon {
    public static void main(String[] args) {

        try {
            String[] command=new String[2];
            command[0]="cmd /c start cmd.exe /k \"telnet\"";
            command[1]="92.44.0.60";
            Process p =Runtime.getRuntime().exec(command);  
            try {
                p.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e);
            }

            BufferedReader reader= new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=null;
            line=reader.readLine();
            File file =new File("rapor.txt");
            file.createNewFile();
            FileWriter writer=new FileWriter(file);
            StringBuilder responseData=new StringBuilder();

            while(line!=null) {
                System.out.println(line);
                responseData.append(line);
                writer.write(line);
                writer.close();
            }

            BufferedReader stdInput=new BufferedReader(new           InputStreamReader(p.getInputStream()) );
            BufferedReader stdError=new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String Error;

            while((Error=stdError.readLine())!=null) {
                System.out.println(Error);  
            }

            while((Error=stdInput.readLine())!=null) {
                System.out.println(Error);  
            }   
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

i want to run telnet execute some commands i have two problem first when i connect to telnet it ask me username and password how ı contineude execute commands by using code after the enter password and my second question inputstream is not working readline is empty all time how can ı fix this problems.thanks for hel

I recommend you the Apache Commons Net Java library ( http://commons.apache.org/proper/commons-net/ ) which contains various clients for many Internet protocols, including Telnet. I don't recommend you to use the embedded telnet client from the OS. Things will be cleaner with a library.

In addtion, in your first while loop, you're closing the writer object every iterations, and you don't read further with your reader .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM