Domanda

sto cercando di connettersi a server SSH Unix sul pulsante di scatto (il codice scritto in actionPerformed () metodo). Sto usando JSch per la connessione al server SSH. Il codice è scritto in classe SwingWorker in quanto è un funzionamento della rete.

private void testConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     

        SwingWorker<Boolean, Void> sw = new SwingWorker<Boolean, Void>(){

            @Override
            protected Boolean doInBackground() throws Exception {
                JSch jsch = new JSch();

                String host = "ServerHost";
                String username = "username";
                String password = "password";

                Session session = jsch.getSession(username, host);
                session.setPassword(password);

                session.setTimeout(20000);
                System.out.println("Connecting to server...");
                session.connect();

                return true;
            }

            @Override
            public void done(){
                try {
                    System.out.println(get().toString());
                } catch (Exception ex) {
                    System.out.err(ex);
                } 
            }
        };

        sw.execute();

    }  

Ma dopo l'esecuzione del corretto con host, username e password, ottengo l'errore qui sotto per tutto il tempo:

com.jcraft.jsch.JSchException: timeout: socket is not established
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

Ma ogni volta che esegue lo stesso codice in programma autonomo, voglio dire, invece, per la scrittura il metodo actionPerformed (), se lo scrivo in modo normale e chiamando da metodo main (). Funzionerà. Quando integro lo stesso codice con il metodo Button Click actionPerformed (), che mi darà sopra eccezione.

Qualcuno può suggerire che cosa sto facendo male qui o qualsiasi modifica dovrebbe essere fatto per il codice.

Ho cercato di connettersi a server SSH utilizzando implementazione "SSHJ", ma ho l'sotto l'errore:

java.net.SocketException: Connection reset
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

Può qualcuno aiutarlo? - come andare avanti

È stato utile?

Soluzione

Ho preso il vostro codice, avvolto in qualche codice della GUI (e convertito ai non generici per essere in grado di compilare con le stesse impostazioni come il resto degli esempi JSch). Per me funziona. Prova questo, e la relazione ciò che si ottiene un'eccezione (che ha un po 'più di registrazione eccezione).

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

import com.jcraft.jsch.*;



class SwingWorkerExample {

    JTextField hostField;
    JTextField userNameField;
    JTextField passwordField;
    JPanel panel;


    public SwingWorkerExample() {
        JPanel p = panel = new JPanel(new GridLayout(0,2));
        hostField = new JTextField(20);
        userNameField = new JTextField(20);
        passwordField = new JPasswordField(20);
        JButton testButton = new JButton("connect!");
        testButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                    testConnectionButtonActionPerformed(ev);
                }
            });
        p.add(new JLabel("host:"));
        p.add(hostField);
        p.add(new JLabel("user:"));
        p.add(userNameField);
        p.add(new JLabel("password:"));
        p.add(passwordField);
        p.add(testButton);
    }

    public JPanel getPanel() {
        return panel;
    }

    private void testConnectionButtonActionPerformed(ActionEvent evt) {

        SwingWorker sw = new SwingWorker(){

                protected Object doInBackground() throws Exception {
                    try {
                        JSch jsch = new JSch();

                        String host = hostField.getText();
                        String username = userNameField.getText();
                        String password = passwordField.getText();

                        Session session = jsch.getSession(username, host);
                        session.setPassword(password);
                        session.setConfig("StrictHostKeyChecking", "no");

                        session.setTimeout(20000);
                        System.out.println("Connecting to server...");
                        session.connect();

                        return session;
                    }
                    catch(Exception ex) {
                        ex.printStackTrace();
                        throw ex;
                    }
                }

                public void done(){
                    try {
                        System.out.println(get());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            };

        sw.execute();

    }


    public static void main(String[] egal) {
        EventQueue.invokeLater(new Runnable(){public void run() {
            SwingWorkerExample ex = new SwingWorkerExample();
            JFrame f = new JFrame("bla");
            f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            f.setContentPane(ex.getPanel());
            f.pack();
            f.setVisible(true);
        }});
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top