ObjectInputStream / ObjectOutputStream |接收和发送大量对象的客户端(Java)

时间:2013-11-03 20:05:04

标签: java swing sockets objectinputstream objectoutputstream

我的多线程服务器/客户端项目出了问题。

服务器端很好,但问题出在客户端。

我可以在每次需要时发送和接收对象我需要在类上声明ObjectInputStreamObjectOutputStream作为属性,然后在构造函数上实例化它们

但问题是代码在ObjectInputStream实例化时阻塞。

这是我的代码:

客户端:

 public class agency_auth_gui extends javax.swing.JFrame {

    Socket s_service;
    ObjectOutputStream out;
    ObjectInputStream in;

    public agency_auth_gui() {
        try {
            initComponents();
            System.out.println("#Connexion en cours avec le Serveur Bancaire.");
            s_service = new Socket("127.0.0.1", 6789);

            out = new ObjectOutputStream(new BufferedOutputStream(s_service.getOutputStream()));
            in= new ObjectInputStream(new BufferedInputStream(s_service.getInputStream()));
            //Authentification du Client GAB
            out.writeObject((String) "type:agence");
            out.flush();

        } catch (UnknownHostException ex) {
            Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
...
Some automaticaly generated swing code
...
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            // Envoi d'une arraylist exec contenant l'authentification

            ArrayList exec = new ArrayList();
            exec.add("auth_agent");
            exec.add(jTextField1.getText());
            exec.add(jTextField2.getText());
            out.writeObject((ArrayList) exec);
            out.flush();

            // Reception de la reponse du serveur pour la fonction authentification


            Agent agent = (Agent) in.readObject();
            if(agent.getId()==0)
            {
                System.out.println("null");
            }else
            {
                System.out.println(agent.getId());
            }

        } catch (IOException ex) {
            Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }                                        


    public static void main(String args[]) {
        try {

            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }

        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new agency_auth_gui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
..

2 个答案:

答案 0 :(得分:1)

根据JavaDoc for ObjectInputStream

  

此构造函数将阻塞,直到相应的ObjectOutputStream已写入并刷新标头。

修改

因此,您似乎没有从另一端InputStream收到任何内容。

答案 1 :(得分:0)

显然,当接受连接时,您的服务器不会构造ObjectOutputStream。不要推迟这一步:它将在构造ObjectInputStream时阻止客户端。见Javadoc。