这是一个小的聊天程序ChatClient端:
package com.bjsxt.pm;import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class ChatClient extends Frame {
TextField tf = new TextField();
TextArea ta = new TextArea();
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bconnect = false;
TaReceive tr = new TaReceive();
Thread t = new Thread(tr); public static void main(String[] args) {
ChatClient c = new ChatClient();
c.lauchFrame(); } public void lauchFrame() {
//System.out.println("Hello!");
setBounds(50, 60, 100, 200);
add(tf, BorderLayout.SOUTH);
add(ta, BorderLayout.NORTH);
pack();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnection();
System.exit(0);
}
}); tf.addActionListener(new TFListener());
this.connection();
setVisible(true); t.start();
} public void connection() {
try {
s = new Socket("127.0.0.1",8000);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bconnect = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void disconnection() {
try {
bconnect = false;
//System.out.println(bconnect);
t.join();
}catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();

} catch (IOException e1) {
e1.printStackTrace();
} }
}

class TaReceive implements Runnable{ public void run() { try {
while(bconnect) {
String s;
s = dis.readUTF();
ta.setText(ta.getText()+s+"\n");
}
} catch(SocketException s) {
System.out.println("退出了,再见!");
} catch (IOException e) {
e.printStackTrace();
}

}

}
}
ChatServer端:
package com.bjsxt.pm;import java.io.*;
import java.io.*;
import java.net.*;
import java.util.*;public class ChatServer {
boolean start = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();

} public void start() { try {
ss = new ServerSocket(8000);
} catch(BindException e) {
System.out.print("端口使用中");
System.out.println("请停掉相关程序,并重新运行服务器!");
System.exit(0);

}
catch (IOException e) {
e.printStackTrace();
}
try {
start = true;
while (start) {
Socket s = ss.accept();
Client c = new Client(s);
Thread t = new Thread(c);
clients.add(c);
t.start();
System.out.print("A client is connected!");
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
class Client implements Runnable {
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
boolean bconnect = false;
Client (Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bconnect = true;
} catch (IOException e) {
e.printStackTrace();
} }

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

}

public void run() {
try {
while (bconnect) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++) {
Client c = clients.get(i);
c.send(str);

}

}
}catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (dis != null)
dis.close();
if(dos != null)
dos.close();
if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
} }

}
}
}为了减轻大侠的负担。问题是在client端的disconnection方法中加入了join之后窗口就关不了了,按那个叉之后没反应,我验证了的,若去掉join之后运行正常,若不去掉join是哪里有错,望哪位大侠提醒一下,十分感谢!

解决方案 »

  1.   

    join的作用就是等待TaReceive结束
    而你的TaReceive是一直在那跑的while(bconnect) {
    String s;
    s = dis.readUTF();
    ta.setText(ta.getText()+s+"\n");
    } 就算你设了bconnect=false,程序阻塞在dis.readUTF();必须这里能读到字节,TaReceive才能结束
    按这个道理,把你的disconnect改成
    try {
    dos.writeUTF("exit");
    bconnect = false;
    // System.out.println(bconnect);
    t.join();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    dos.close();
    dis.close();
    s.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }你再试试