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();
        }
     }
   }
}