Question

I have a problem on my multithreaded server/client project.

The server side is good but the problem is on the client side.

I can send and receive objects every time I want I need to declare an ObjectInputStream and ObjectOutputStream as attributes on my Class and then instantiate them on the constructor

But the problem is that the code is blocking on the ObjectInputStream instantiation.

Here is my code:

Client:

 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                     
..
Était-ce utile?

La solution

According to the JavaDoc for ObjectInputStream:

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Edit

So it appears that you're not receiving anything on the InputStream from the other end.

Autres conseils

Clearly your server isn't constructing an ObjectOutputStream when the connection is accepted. Don't defer this step: it will block the client while constructing its ObjectInputStream. See the Javadoc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top