让Client 线程 睡眠 就可以正常关闭。。
下面是程序:
===============================================Client================================================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class ChatClient {
    public static void main(String[] args) {
        new ChatFrame().lauchFrame();
    }
}class ChatFrame extends Frame {
    TextField tf=null;
    TextArea ta =null;
    Socket s =null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    
    boolean isConnect = false;
    
    public void lauchFrame(){            
        ta = new TextArea();
        tf = new TextField();
        tf.addActionListener(new TfAction());
        setBounds(200,200,200,300);
        add(ta,BorderLayout.NORTH);
        add(tf,BorderLayout.SOUTH);
        pack();
        this.setResizable(false);
        setVisible(true);
        connect();
        
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                deConnect();
                setVisible(false);
                System.exit(-1);
            }                
        });        
    }
    
    public void deConnect(){
        try {
            br.close();
            bw.close();
            s.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    
    public void connect(){
        try {
            s = new Socket("127.0.0.1",6666);
            isConnect = true;
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            Client c = new Client()    ;
            new Thread(c).start();
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }
    
    class TfAction implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            TextField tf = (TextField)e.getSource();
            String line = tf.getText().trim();
            try {
                bw.write(line+"\n");
                bw.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            ta.setText(line);
            tf.setText("");
        }    
    }
    
    class Client implements Runnable {        public void run() {
            try {
                while(isConnect){                    
                    String s =br.readLine();
                    ta.setText("server"+s);        
//                    try {
//                        Thread.sleep(10000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                }
            } catch (SocketException e) {
                System.out.println("退出了,bye!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }    
    }
}
===============================================Server================================================================
import java.net.*;
import java.io.*;
import java.util.*;public class ChatServer {
    ServerSocket ss =null;
    boolean isStart =false;
    List <Runner> runners = new ArrayList<Runner>();
    
    public static void main(String[] args) {    
        new ChatServer().connect();
    }
    
    public  void connect(){
        try {
            ss = new ServerSocket(6666);
        }catch(IOException e){
            e.printStackTrace();
        }
        
        try{
            isStart = true;
            while(isStart){
                Socket s= ss.accept();
System.out.println("a client has connect!");
                Runner r = new Runner(s);
                runners.add(r);
                new Thread(r).start();
            }
        } catch (IOException e) {
            isStart= false;
            e.printStackTrace();
        }finally{
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        
        }
    }
    
    class Runner implements Runnable{
        BufferedReader br = null;
        BufferedWriter bw =null;
        private Socket s =null;
        
        Runner(Socket s){
            this.s = s;
            try {
                br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public void send(String str){
            try {
                bw.write(str+"\n");
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public void run() {
            try{
                boolean isConnect = false;
                
                isConnect = true;                while(isConnect){
                    String str = br.readLine();
                    if(str ==null){
                        isConnect = false;
                        System.out.println("客户端退出!");
                        break;
                    }
                    for(int i = 0;i<runners.size();i++){
                        Runner r = runners.get(i);
                        r.send(str);
                    }
                }                
            }catch(IOException e){        
                e.printStackTrace();
            }finally{
                try {
                    if(s==null) s.close();
                    if(br==null) br.close();
                    if(bw == null) bw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        
    }
}

解决方案 »

  1.   

    public static void setupClosing(JFrame frame) {
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
        System.exit(0);
       }
    });
    }
      

  2.   

    frame.addWindowListener(new WindowAdapter() { 
        public void windowClosing(WindowEvent e) { 
        System.exit(0); 
      } 
    }); 
      

  3.   


    class ChatFrame extends Frame { 
        TextField tf=null; 
        TextArea ta =null; 
        Socket s =null; 
        BufferedReader br = null; 
        BufferedWriter bw = null; 
        
        boolean isConnect = false; 
        
        public void lauchFrame(){            
            ta = new TextArea(); 
            tf = new TextField(); 
            tf.addActionListener(new TfAction()); 
            setBounds(200,200,200,300); 
            add(ta,BorderLayout.NORTH); 
            add(tf,BorderLayout.SOUTH); 
            pack(); 
            this.setResizable(false); 
            this.setDefaultCloseOperation(JFrame.EXIT_ON...);    
                   //这个方法可以退出,方法名和参数有点 不记得了^_^
            setVisible(true); 
            connect(); 
            
            this.addWindowListener(new WindowAdapter(){ 
                public void windowClosing(WindowEvent e) { 
                    deConnect(); 
                    setVisible(false); 
                    System.exit(-1); 
                }                
            });        
        } 
      

  4.   

     this.addWindowListener(new WindowAdapter(){ 
                public void windowClosing(WindowEvent e) { 
                    deConnect(); 
                    setVisible(false); 
                    System.exit(-1); 
               }                
     }); 
    就这样就可以了 不就是给window上加一个监听嘛  匿名类是最合适的   
      

  5.   

    setVisible(true);后面加上
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    或者给window加监听器!
    frame.addWindowListener(new WindowAdapter() { 
        public void windowClosing(WindowEvent e) { 
        System.exit(0); 
      } 
    }); 
      

  6.   

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)