Client端:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;public class ChatClient extends Frame {
TextField tf = new TextField();
TextArea ta = new TextArea();
Socket socket;
DataOutputStream dos = null;
DataInputStream dis = null; public static void main(String[] args) {
new ChatClient().launchFrame();
} public void launchFrame() {
add(tf, BorderLayout.SOUTH);
add(ta, BorderLayout.NORTH);
setLocation(200, 200);
setSize(400, 300);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
disconnected();
System.exit(0);
}
});
connected();
new Thread(new ReceThread()).start();
tf.addActionListener(new Monitor());
setVisible(true); } private class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) {
ta.setText( tf.getText() + "\n");
try {
dos.writeUTF(tf.getText());
dos.flush();
} catch (IOException e1) { e1.printStackTrace();
}
tf.setText(null);
}
} private void connected() {
try {
socket = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void disconnected(){
try {
socket.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
} private class ReceThread implements Runnable{

public void run() {
try {
while(true){
 String str = dis.readUTF();
 ta.setText(ta.getText()+str);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

}
Server端:
import java.io.FilterInputStream;
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.*;public class ChatServer {
boolean start = false;
ServerSocket ss = null;
DataOutputStream dos = null; ArrayList<Client> clients = new ArrayList<Client>(); public static void main(String[] args) {
new ChatServer();
} public ChatServer() {
try {
ss = new ServerSocket(8888);
start = true;
} catch (IOException e1) {
e1.printStackTrace();
}
try {
start = true;
while (start) {
Socket s = ss.accept();
System.out.println("A socket connected");
Client c = new Client(s);
clients.add(c);
new Thread(c).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} class Client implements Runnable {
private Socket s;
private DataInputStream dis;
private boolean connected = false; public Client(Socket s) {
this.s = s;
connected = true;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
} public void send(String s) {
try {
dos.writeUTF(s);
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
while (connected) {
try {
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("A socket is closed");
connected = false;
} catch (IOException e) {
e.printStackTrace();
try {
s.close();
dis.close();
} catch (IOException e1) {
e1.printStackTrace();
} }
}
}
}
}
现在的问题是只有一个发能收到还是只有自己收到,收到的是把别人的也收到了,高手帮我看一下哪里出错了,顺便解释一下,谢了

解决方案 »

  1.   

    应该是你的dos定义的位置有问题,把dos的定义放到Client类里面来CharServer.cimport java.io.FilterInputStream;
    import java.io.IOException;
    import java.net.*;
    import java.io.*;
    import java.util.*;public class ChatServer {
    boolean start = false;
    ServerSocket ss = null;

    ArrayList<Client> clients = new ArrayList<Client>();

    public static void main(String[] args) {
    new ChatServer();
    }

    public ChatServer() {
    try {
    ss = new ServerSocket(8888);
    start = true;
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    try {
    start = true;
    while (start) {
    Socket s = ss.accept();
    System.out.println("A socket connected");
    Client c = new Client(s);
    clients.add(c);
    new Thread(c).start();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    ss.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    class Client implements Runnable {
    private Socket s;
    private DataInputStream dis;
    private boolean connected = false;
    DataOutputStream dos = null; //把这句话放到这里来,而不是定义在ChatServer里

    public Client(Socket s) {
    this.s = s;
    connected = true;
    try {
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream(s.getOutputStream());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

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

    public void run() {
    while (connected) {
    try {
    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("A socket is closed");
    connected = false;
    } catch (IOException e) {
    e.printStackTrace();
    try {
    s.close();
    dis.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }

    }
    }
    }
    }
    }
      

  2.   

    改过后的意思是每个Thread都有单独的Dos了吗?