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();
} }
}
}
}
}
现在的问题是只有一个发能收到还是只有自己收到,收到的是把别人的也收到了,高手帮我看一下哪里出错了,顺便解释一下,谢了
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();
} }
}
}
}
}
现在的问题是只有一个发能收到还是只有自己收到,收到的是把别人的也收到了,高手帮我看一下哪里出错了,顺便解释一下,谢了
解决方案 »
- Time() 线程会不会没有处理完就销毁?异常也没有报。求救!!!
- 请大家关注一下:3句话, java语言代码
- 请问类变量存放在内存的哪里?
- ~使用Log4J时的笔记,大家共同学习~
- 路过的看看,救救急!
- 谁给我讲一下void的用法呢?
- 请问哪里有JAVA反编译器下载??
- 请问在DOS中如何搭建JAVA程序的运行环境?
- 高分请教, 有谁使用过ipworks作过SNM开发的
- jbuilder4的enterprise setup需要weblogic51,但我的是weblogic6,应该怎样才能让jbuilder支持6啊?
- jtable 监听单元格
- 在线等待解决 用Jtree 打印d:\\所有文件和文件名 用树形结构排列 已经写好了 为什么输出结果是空白?
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();
}
}
}
}
}
}