简体   繁体   English

inputstream 使程序等待

[英]inputstream make the program to wait

I am using J2ssh library to connect to a Unix machine, run a command and get the result using input stream.我正在使用 J2ssh 库连接到 Unix 机器,运行命令并使用输入流获取结果。 But the programs runs into loop while reading the input stream.但是程序在读取输入流时会陷入循环。 I am able to get the result from the input steam, but the program struck there and not proceeding.我能够从输入的蒸汽中获得结果,但程序在那里发生并没有继续。 Why is the program stuck in that loop?为什么程序卡在那个循环中?

Here is the source code,这是源代码,

public void loginAndCheckProcess(String hostName, String userName, String password, String port) {
        try {
            SshConnector con = SshConnector.createInstance();

            SocketTransport transport = new SocketTransport(hostName, 22);
            transport.setTcpNoDelay(true);
            SshClient client = con.connect(transport, userName);

            Ssh2Client ssh2 = (Ssh2Client) client;

            PasswordAuthentication pwd = new PasswordAuthentication();
            do {
                pwd.setPassword(password);
            } while (ssh2.authenticate(pwd) != SshAuthentication.COMPLETE && client.isConnected());

            String command = "ps -ef | grep " + port + '\n';
            if (client.isAuthenticated()) {
                SshSession session = client.openSessionChannel();
                session.startShell();
                session.getOutputStream().write(command.getBytes());
                InputStream is = session.getInputStream();
                Scanner br = new Scanner(new InputStreamReader(is));

                String line = null;
                int isRunning = 0;
                while (br.hasNextLine()) {
                    line = br.nextLine();
                    isRunning++;
                    System.out.println(line);
                }
                session.close();
            }
        } catch (SshException | IOException | ChannelOpenException ex) {
            Logger.getLogger(Authenticator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

I tried to replace the above second while loop with the following loop but no luck,我试图用下面的循环替换上面的第二个 while 循环,但没有运气,

try (Reader in = new InputStreamReader(is, "UTF-8")) {
                    for (;;) {
                        int rsz = in.read(buffer, 0, buffer.length);
                        if (rsz < 0)
                            break;
                        out.append(buffer, 0, rsz);
                    }
                }
                System.out.println(out);

And with the following loop, but no luck,并使用以下循环,但没有运气,

byte[] tmp = new byte[1024];
                while (is.available() > 0) {
                    int i = is.read(tmp, 0, 1024);
                    if (i < 0) {
                        break;
                    }
                    System.out.print(new String(tmp, 0, i));
                }

At last I found the answer, thank you mangusta.最后我找到了答案,谢谢曼古斯塔。

I have added timeout on the socket.我在套接字上添加了超时。 It worked.有效。 I just added below line in the program,我刚刚在程序中添加了以下行,

transport.setSoTimeout(3000);

Your code will keep looping on the session InputStream until the session closes ie the InputStream returns EOF.您的代码将继续在会话 InputStream 上循环,直到会话关闭,即 InputStream 返回 EOF。 As you used startShell method, this spawns an interactive shell so the session will continue and present a new prompt after your command has executed, awaiting another command.当您使用 startShell 方法时,这会产生一个交互式 shell,因此会话将继续并在您的命令执行后显示一个新提示,等待另一个命令。

You could add a call to exit in your command您可以在命令中添加退出调用

String command = "ps -ef | grep " + port + ';exit\n';

Or you could use the alternative executeCommand method on the session rather than writing out the command to the OutputStream.或者您可以在会话上使用替代的 executeCommand 方法,而不是将命令写出到 OutputStream。

session.executeCommand(command);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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