高手挑战题!!!

softbank 2011-12-13 08:51:02
代码已经精简可运行!问题已经标注请详细解答谢谢……
server端
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.DefaultListModel;

public class ChatServer {
ServerSocket ss = null;
DefaultListModel listModel = new DefaultListModel();
List<Client> clients = new ArrayList<Client>();

public static void main(String[] args) {
new ChatServer();
}
public ChatServer() {
try {
ss = new ServerSocket(8888);//server等待连接
while(true) {
Socket s = ss.accept();
Client c = new Client(s);
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}

class Client implements Runnable {//为每个client启动一个线程
private Socket s;
private ObjectInputStream dis = null;
private ObjectOutputStream dos = null;
public Client(Socket s) {
this.s = s;
try {
dos = new ObjectOutputStream(s.getOutputStream());
dis = new ObjectInputStream(s.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(Messages str) throws IOException {
dos.writeObject(str);
dos.flush();
}
public void run() {
try {
while(true) {
Messages ms = (Messages) dis.readObject();
listModel.addElement(ms.clientName);
ms.listModel = listModel;
System.out.println(ms.listModel);//问题出在这里!我这里打印ms.listmodel完全正确(即启动3个client打印[kk,kk,kk],但是问题出现在了下面的转发!请看client接收代码)
for(Client client:clients) {
client.send(ms);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

}
}
client端
import java.io.*;
import java.net.*;
import javax.swing.JList;

public class ChatClient{
ObjectOutputStream dos = null;
ObjectInputStream dis = null;
JList list;

ChatClient() {
String[] str = {"a","b","c"};
list = new JList();

connect();
new Thread(new RecvThread()).start();
}

public void connect() {
try {
Socket s = new Socket("127.0.0.1", 8888);
dos = new ObjectOutputStream(s.getOutputStream());
dis = new ObjectInputStream(s.getInputStream());
Messages ms = new Messages();
ms.clientName = "kk";
try {
dos.writeObject(ms);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private class RecvThread implements Runnable {
@Override
public void run() {
try {
while(true) {
Messages str = (Messages) dis.readObject();
list.setModel(str.listModel);
System.out.println(list.getModel());//问题在这里接收时新开的clientlistmodel为[kk,kk]可是旧client为[kk]和sever端明明是一个messages为什么这里读不出来
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
new ChatClient();
}
}
序列化message
import javax.swing.DefaultListModel;


public class Messages implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public String clientName,msg;
public DefaultListModel listModel = new DefaultListModel();
public int flag = 0,i;
}
...全文
88 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhz586 2011-12-14

。。
。。
晕了
回复
潇水汀寒 2011-12-14
socket传递序列化对象数据丢失?
貌似LZ是这个意思
回复
孟祥月 2011-12-13
好长的代码 看着晕
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2011-12-13 08:51
社区公告
暂无公告