写的一个简单的聊天室类~!
客户端退不出去
不知道怎么解决
//客户端类
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
Socket sc=null;
DataOutputStream out=null;
DataInputStream in=null;
PrintStream ps;
boolean connect=false;
Container f=this.getContentPane();
Thread cts=new Thread(new CtThread());

public void client(){
f.setLayout(new BorderLayout());
txta=new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf=new JTextField(20);
bt=new JButton("发送");
pl=new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
txtf.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});

try
{
Socket sc=new Socket("127.0.0.1",8000);
out=new DataOutputStream(sc.getOutputStream());
in= new DataInputStream(sc.getInputStream());
ps=new PrintStream(out);
connect=true;
}catch(UnknownHostException ex){
txta.append(ex.toString()+'\n');
} catch (IOException e){

}

cts.start();

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt|e.getSource()==txtf)
{
String Dialog=txtf.getText();
try
{
ps.println(Dialog);
txtf.setText("");
}
catch(Exception ex)
{
txta.append(ex.toString()+'\n');
}
}
}
public void disconnect() {
try {
out.close();
in.close();
sc.close();
}catch(IOException e){
e.printStackTrace();
}
}

private class CtThread implements Runnable { public void run() {
try {
while(connect) {
String Dialog = in.readUTF();
txta.setText(txta.getText() + Dialog + '\n');
}
} catch (SocketException e) {
System.out.println("退出客户端..~!");
} catch (IOException e) {
e.printStackTrace();


}
}

public static void main(String args[])
{
Client myclient=new Client();
myclient.client();
}
}//服务器类
import java.io.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame{
JTextArea txt;
ServerSocket seSocket=null;
ArrayList<CtThread> cts=new ArrayList<CtThread>();

public void serve(){
txt=new JTextArea();
Container f=this.getContentPane();
setTitle("学习通讯类");
f.setLayout(new BorderLayout());
f.add(txt,BorderLayout.CENTER);
setSize(500,300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});

try{
seSocket=new ServerSocket(8000);
txt.append("服务器启动时间是:"+new Date()+'\n');
  } catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {}

try{
while(true){
Socket st=seSocket.accept();
CtThread ct=new CtThread();
ct.deliveSt(st);
new Thread(ct).start();
cts.add(ct);
  }
}catch(IOException e){}
}



class CtThread implements Runnable{
Socket st=null;
DataInputStream in=null;
DataOutputStream out=null;
public void deliveSt(Socket st)
{
this.st=st;
try{
in=new DataInputStream(st.getInputStream());
out=new DataOutputStream(st.getOutputStream());
}catch(IOException e){}
}

public void run()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while(true){
String Dialog=br.readLine();
txt.append("从客户端收到信息"+Dialog+'\n');
for(int i=0; i<cts.size(); i++) {
CtThread c = cts.get(i);
c.send(Dialog);
txt.append("已经发出信息"+Dialog+'\n');


}
  }
}catch(IOException e){}
}
public void send(String Dialog){
try {
out.writeUTF(Dialog);
} catch (IOException e) {
e.printStackTrace();
}

}

}
public static void main(String args[])
{
Server myserver=new Server();
myserver.serve();
}
}