Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Socket read from socket in real time
    text
    copied!<p>I'm writing a java applet to simulate a ssh connection to ubuntu machine, I set up the connection, excute the command, and get back response, everything looks fine. However, I had a small issue here, I want to receive server socket response in real time, so I can simulate on Applet in real time, but it seems I receive the response while the command excution is finished. For example, I have a test tcl file, I let the excution sleep 5 seconds between each statement, I should whoe the response on applet in every 5 seconds, instead of I get all of the response in the end. Any thoughts?</p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * ssh_applet.java * * Created on 9-Dec-2010, 11:10:06 AM */ /** * * @author xzhang */ //import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.InputStreamReader; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import javax.swing.*; public class ssh_applet extends javax.swing.JApplet{ private String host_name = null; private String user_name = null; private String pass_word = null; private Connection app_conn; private Session app_session; /** Initializes the applet ssh_applet */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } /** This method is called from within the init() method to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt; private void initComponents() { ssh_input = new javax.swing.JPanel(); connect = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); hostname = new javax.swing.JTextField(); username = new javax.swing.JTextField(); password = new javax.swing.JPasswordField(); ssh_command = new javax.swing.JTextField(); ssh_output_pannel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); ssh_output = new javax.swing.JTextArea(); clear_text = new javax.swing.JButton(); connect.setText("Connect"); connect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connectActionPerformed(evt); } }); jLabel1.setText("Host Name"); jLabel2.setText("User Name"); jLabel3.setText("Password"); hostname.setText("10.4.0.135"); username.setText("xzhang"); password.setText("Samboapple0827!"); ssh_command.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ssh_commandActionPerformed(evt); } }); javax.swing.GroupLayout ssh_inputLayout = new javax.swing.GroupLayout(ssh_input); ssh_input.setLayout(ssh_inputLayout); ssh_inputLayout.setHorizontalGroup( ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ssh_inputLayout.createSequentialGroup() .addContainerGap() .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(ssh_inputLayout.createSequentialGroup() .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jLabel3)) .addGap(18, 18, 18) .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(username, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(password) .addComponent(hostname, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ssh_inputLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(connect)) .addGroup(ssh_inputLayout.createSequentialGroup() .addGap(49, 49, 49) .addComponent(ssh_command, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(60, Short.MAX_VALUE)) ); ssh_inputLayout.setVerticalGroup( ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ssh_inputLayout.createSequentialGroup() .addGap(14, 14, 14) .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(hostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(connect)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(ssh_inputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ssh_command, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(18, Short.MAX_VALUE)) ); ssh_output.setColumns(20); ssh_output.setEditable(false); ssh_output.setLineWrap(true); ssh_output.setRows(5); jScrollPane1.setViewportView(ssh_output); clear_text.setText("Clear"); clear_text.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clear_textActionPerformed(evt); } }); javax.swing.GroupLayout ssh_output_pannelLayout = new javax.swing.GroupLayout(ssh_output_pannel); ssh_output_pannel.setLayout(ssh_output_pannelLayout); ssh_output_pannelLayout.setHorizontalGroup( ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ssh_output_pannelLayout.createSequentialGroup() .addContainerGap() .addGroup(ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE) .addComponent(clear_text, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap()) ); ssh_output_pannelLayout.setVerticalGroup( ssh_output_pannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(ssh_output_pannelLayout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(clear_text) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ssh_input, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(ssh_output_pannel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(ssh_input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(ssh_output_pannel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); }// &lt;/editor-fold&gt; private void connectActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: /*get host_name*/ host_name=hostname.getText(); user_name=username.getText(); pass_word=password.getText(); /* Create a connection instance */ app_conn=new Connection(host_name); try { app_conn.connect(); /* Authenticate. * If you get an IOException saying something like * "Authentication method password not supported by the server at this stage." * then please check the FAQ. */ boolean isAuthenticated = app_conn.authenticateWithPassword(user_name, pass_word); if (isAuthenticated == false) throw new IOException("Authentication failed."); JOptionPane.showMessageDialog(null, "You are connected to host "+host_name, "Connection", JOptionPane.INFORMATION_MESSAGE); /* Create a session */ } catch(IOException e) {} } private void ssh_commandActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String command=ssh_command.getText(); try { app_session=app_conn.openSession(); app_session.execCommand( command ); InputStream stdout = new StreamGobbler( app_session.getStdout() ); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); String line ; while( (line= br.readLine()) != null ) { ssh_output.append( line + "\n" ); //line = br.readLine(); } // DEBUG: dump the exit code System.out.println( "ExitCode: " + app_session.getExitStatus() ); // Close the session app_session.close(); } catch(Exception e) {} } private void clear_textActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: ssh_output.setText(""); } // Variables declaration - do not modify private javax.swing.JButton clear_text; private javax.swing.JButton connect; private javax.swing.JTextField hostname; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JPasswordField password; private javax.swing.JTextField ssh_command; private javax.swing.JPanel ssh_input; private javax.swing.JTextArea ssh_output; private javax.swing.JPanel ssh_output_pannel; private javax.swing.JTextField username; // End of variables declaration } </code></pre> <p>Here is my code,I'm using ganymed-ssh2 library, can't get where to flush</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload