剛開始學習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();
}
}
}
}
你用到了繼承和接口,父類的輸出可能被子類給覆蓋了,你調試的時候仔細看看是不是這方法的問題;