ChatServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer{
   boolean stat;
   ServerSocket serverSocket;
   List<Client> clientArray = new ArrayList<Client>();           //ڴ洢ͻ
   public static void main(String[] args) {
       new ChatServer().start();
   }
 public void start(){
       try {
           serverSocket = new ServerSocket(8888);
           stat = true;
       }catch(BindException e){                               //SeverѾУظʱ쳣
           System.out.println("˿ʹ......");
           System.exit(0);
       }catch(IOException e) {
           e.printStackTrace();
       }
       try{
           while(stat){
    Socket s = serverSocket.accept();
    System.out.println("һѽ" ); 
    Client c = new Client(s);          //ÿһͻˣʹһͻ˶һ߳
    new Thread(c).start();
    clientArray.add(c);                                  //ÿͻӵList
           }
       }catch (IOException e) {
           e.printStackTrace();
      }finally {
           try {
               serverSocket.close();
           } catch (IOException e) {
               e.printStackTrace();
      }
    }
 }
 class Client implements Runnable {
    private Socket socket;
    private DataInputStream input;
    private DataOutputStream output;
    private boolean cont;
    public Client(Socket socket){
         this. socket = socket;  
         try {
               input = new DataInputStream(socket.getInputStream());        //ʼ
               output = new DataOutputStream(socket.getOutputStream());
               cont = true;
         }catch (IOException e) {
               e.printStackTrace();
          }
     }
     public void send(String str){                                       //ڷϢͻ
          try {
              output.writeUTF(str);
          }catch (IOException e) {
              clientArray.remove(this);                                 //Ƴ˳Ķ
              System.out.println("һͻ˳");
          }
     }
     public void run() {
   try{
       while(cont){
          String str = input.readUTF();                            //ȡϢ
          System.out.println(str);
          for(int i=0; i< clientArray.size(); i++){
              Client c = clientArray.get(i);                         //ȡͻ
              c.send(str);  
          }
       }
         }catch(EOFException e){                     //readUTF()ʽԹرտͻ˻쳣
             System.out.println("Client closed!");
         }catch(IOException e) {
             e.printStackTrace();
         }finally{
             try{
                 if(input != null) inout.close();
                 if(output != null) output.close();
                 if(socket != null) {
                       socket.close();
                       socket = null;                //Ϊnullɱռͻ
                 }
             }catch (IOException e) {
                  e.printStackTrace();
             } 
         }
      }
   }
}
ChatClient.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
 Socket socket = null;
 DataOutputStream  output = null;
 DataInputStream  input = null;
 private boolean con;
 TextField text = new TextField();
 TextArea area = new TextArea();
 Thread  t = new Thread(new Receive ());
 public static void main(String[] args) {
       new ChatClient().launch();
 }
 public void launch() {
 setBounds(200,200,200,200);
 add(text,BorderLayout.SOUTH);
 add(area,BorderLayout.NORTH);
 pack();                                                
 this.addWindowListener(new WindowAdapter(){
     public void windowClosing(WindowEvent e){
        disconnect();
        System.exit(0);
 }});
             text.addActionListener(new TListener());
             setVisible(true);
             connect();
             t.start();                                                      //߳
 }
 public void connect(){
  try {
   socket = new Socket("127.0.0.1",8888);  
   System.out.println("ӳɹ");
   output = new DataOutputStream(socket.getOutputStream());
   input = new DataInputStream(socket.getInputStream());
   cont = true;
  }catch (UnknownHostException e) {
     e.printStackTrace();
          }catch (IOException e) {
     e.printStackTrace();
  }
 }
 public void disconnect(){
 try{
   output.close(); 
   intput.close();
   socket.close();
  }catch(IOException e) {
       e.printStackTrace();
  } 
 }
 private class TListener implements ActionListener {
     public void actionPerformed(ActionEvent e) {
   String str = text.getText().trim();
   text.setText("");
   try{
       output.writeUTF(str);
       output.flush();
   }catch (IOException e1) {
       e1.printStackTrace();
   }
 }
 }
 private class Receive implements Runnable{
     public void run() {
        try{
            while(cont){
                String str = input.readUTF();
                area.setText(taContent.getText() + str + '\n');
            }
        }catch(SocketException e){
             System.out.println("˳ˣbye!");
        }catch(IOException e) {   
             e.printStackTrace();
        }
     }
   }
}