title: a simple client/server chat program
body: <p>I'm trying to create a simple chat program using java, which contains to forms, a client form and a server form, the client form contains a TextField and a Button (send button), and the server form contains a TextArea.</p>
  
  <p>When I click on the send button it should send the wrote text in the TextField to the TextArea in the server form.</p>
  
  <p>for the first time it works but when I click on the button in the second time it won't work.</p>
  
  <p>this is the code I used in the Server Form :</p>
  
  <pre><code>public class Server extends javax.swing.JFrame implements Runnable {
  
      private Thread th;
  
      public Server() {
          initComponents();
          th = new Thread(this);
          th.start();
      }
  
      // The main method was here
  
      @Override
      public void run() {
  
          // Etablir la connexion
          try {
              ServerSocket ecoute;
              ecoute = new ServerSocket(1111);
              Socket service = null;
              System.out.println("Serveur en attente d'un client !");
              while (true) {
  
                  service = ecoute.accept();
                  System.out.println("Client connécté !");
                  DataInputStream is = new DataInputStream(service.getInputStream());
                  jTextArea1.setText("Client dit : "+ is.readUTF().toUpperCase());
                  service.close();
              }
          } catch (IOException e) {
              e.printStackTrace();
  
          }
      }
  }
  </code></pre>
  
  <p>and this is the code of the client form:</p>
  
  <pre><code>    public class Client extends javax.swing.JFrame {
  
      DataOutputStream os;
  
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
          try {
              os.writeUTF(jTextField1.getText());
          } catch (IOException ex) {
              Logger.getLogger(Client.class.getName()).log(Level.SEVERE,null, ex);
          }
      }
  
      public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
  
              public void run() {
                  Client c = new Client();
                  c.setVisible(true);
  
                  try {
                      Socket s = new Socket("localhost", 1111);
                      c.os = new DataOutputStream(s.getOutputStream());
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          });
      }
  
  }
  </code></pre>