Bonjour,

En faite, je. suis entrain de développer un petit programme de chat (client/serveur).
mon problème se résume que une fois le client établis la connexion et commence à lire le flux d'entrée, l'interface swing se plante, et les champs de texte ne me permettent ni d'y ecrire ni aussi de visualiser le flux d'entrée de la socket.

NB: quand j'affiche le texte arrivant via system.out.println je le reçoit, donc le problème réside dans swing et son interface qui se plante une fois la lecture de flux commence.

aidez moi svp...


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181


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

public class ClientInt extends javax.swing.JFrame {
    private Socket client;
    BufferedReader br;
    String message;
    Timer timer;
    /** Creates new form ClientInt */
    public ClientInt() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        Connect = new javax.swing.JButton();
        port = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        host = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Sended = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        toSend = new javax.swing.JTextArea();
        Send = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Connect.setText("connect");
        Connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectActionPerformed(evt);
            }
        });

        port.setFont(new java.awt.Font("Tahoma", 1, 11));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Port");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Host");

        host.setFont(new java.awt.Font("Tahoma", 1, 11));

        Sended.setColumns(20);
        Sended.setRows(5);
        jScrollPane1.setViewportView(Sended);

        toSend.setColumns(20);
        toSend.setRows(5);
        jScrollPane2.setViewportView(toSend);

        Send.setText("Send");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(host, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
                                .addComponent(Connect))
                            .addComponent(jLabel2)))
                    .addComponent(Send, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Connect)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(host, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Send)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void ConnectActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        try{
        client = new Socket(host.getText(),Integer.parseInt(port.getText()));   // l'etablissement de la connexion
        JOptionPane.showMessageDialog(null, "connexion etablie");  //une fois etablie on aura le message
        br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        
      timer = new Timer(1000, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          try{
            message =br.readLine();
          }
          catch(Exception e2)
          {
              
          }
           if(message == null)
               System.exit(0);
           message = Sended.getText()+ message +"\n";
            Sended.setText(message);
            
      }
    });
    timer.start();
        /*while (true) {
           message =br.readLine();
           if(message == null)
               break;
           message = Sended.getText()+ message +"\n";
            Sended.setText(message);
            //tt = new TextModifier(message);         // thread de lecture du message
            
           //break;
          System.out.println("ECHO = " + message);
        }*/
        } 
        catch(Exception e){
             JOptionPane.showMessageDialog(null,"probleme de connexion, verifier " +
                     "la validité et la disponibilité du numéro du port et la nom du serveur"); ///en cas d'erreur 
        }
    }                                       
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientInt().setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton Connect;
    private javax.swing.JButton Send;
    private javax.swing.JTextArea Sended;
    private javax.swing.JTextField host;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField port;
    private javax.swing.JTextArea toSend;
    // End of variables declaration                   
    
   
}