刚开始学习java ,自己写了一个聊天小程序,没有报任何错误。测试时打开3个聊天小窗口 A,B,C,在A中输入文字,只在B中显示出来了,而且显示出了三句相同语句。查了好几个小时都没有查出来,希望高手能帮帮忙,看看是怎么回事,并且告诉我是通过什么方法找出来的。
以下是客户端和服务器端代码
客户端:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyChatClient extends Frame {
/**
* @param args
*/
//设置窗口位置及大小
private static final int CHAT_LAYOUT_X = 300;
private static final int CHAT_LAYOUT_Y = 100;
private static final int CHAR_SIZE_X =500;
private static final int CHAR_SIZE_Y =500;
//设置文本框和文本域
private TextArea ta;
private TextField tf;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private String message = "";
private boolean flag = false;
private Socket s ;
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyChatClient().MyChatFrame();
}
//设置聊天室框架及基本相应事件
public void MyChatFrame(){
this.setTitle("聊天室");
this.setLocation(CHAT_LAYOUT_X,CHAT_LAYOUT_Y);
this.setSize(CHAR_SIZE_X,CHAR_SIZE_Y);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
disConnection();
System.exit(0);
}
});
ta = new TextArea();
tf = new TextField();
this.add(ta,BorderLayout.NORTH);
this.add(tf,BorderLayout.SOUTH);
this.pack();
TfEvent te = new TfEvent();
tf.addActionListener(te);
this.setVisible(true);
connecter();
new Thread(new ReadThread()).start();
}
class TfEvent implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = tf.getText();
ta.setText(ta.getText()+str+'\n');
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
str = "";
}
}
//连接服务器
public void connecter() {
try {
s = new Socket("127.0.0.1",6666);
flag = true;
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
//new Thread(new ReadThread()).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class ReadThread implements Runnable{
public void run(){
try {
while(flag){
message = dis.readUTF();
ta.setText(ta.getText()+message+'\n');
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void disConnection(){
try {
if(dis!=null){dis.close();dis=null;}
if(dos!=null){dos.close(); dos=null;}
if(s!=null){s.close(); s=null;}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器端:
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class MyChatServer {
private boolean bConnection = false;
private boolean flag = false;
private ServerSocket ss = null;
private Socket s = null;
private DataInputStream dis ;
private DataOutputStream dos ;
private List clients = new ArrayList();
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyChatServer().myChat();
}
public void myChat(){
try {
ss =new ServerSocket(6666);
bConnection = true;
while(bConnection){
s = ss.accept();
flag = true;
ChatClient cc = new ChatClient(s);
clients.add(cc);
new Thread(cc).start();
}
}catch(IOException e){
e.printStackTrace();
}
}
//处理客户信息内部类
class ChatClient implements Runnable{
Socket so = null;
String message;
public ChatClient(Socket s){
try {
this.so = s ;
dis = new DataInputStream(so.getInputStream());
dos = new DataOutputStream(so.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
try {
while(flag){
message = dis.readUTF();
for(int i=0; i<clients.size(); i++){
ChatClient c = clients.get(i);
c.send(message);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(dis!=null){dis.close(); dis=null;}
if(dos!=null){dos.close(); dos=null;}
if(s!=null){s.close(); s=null;}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void send(String str){
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}