yuhao_1202
发贴: 1
积分: 0
|
于 2010-01-15 23:07
这么一个问题:精简版代码如下缩减为仅有服务器和客户端) [iframe] //Server.java import java.io.IOException; import java.io.ObjectInputStream;[iframe][/iframe] import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Vector; public class Server { Vector<String> v=new Vector<String>(); Vector<Task> tasklist=new Vector<Task>(); ServerSocket server; static int count=0; Server()throws Exception{ server=new ServerSocket(9999); } public static void main(String[] args)throws Exception { Server s=new Server(); s.listen(); } public void listen()throws Exception{ while(true){ Socket tmp=server.accept(); count++; Task tmptask=new Task(tmp); addUser(tmptask,"user"+count); Thread s=new Thread(tmptask); s.start(); } } public synchronized void addUser(Task t,String u){ this.tasklist.add( t ); this.v.add( u ); } public synchronized void removeUser(Task t,String u){ this.tasklist.remove( t ) ; this.v.remove( u ); } public void sendUserToAll()throws IOException { Task tmptask; System.out.println("#######begin send to all######"); for(int i=0;i<tasklist.size();i++){ tmptask=tasklist.get( i ); tmptask.oos.writeObject(v); System.out.println("send v to the port:"+tmptask.socket.getPort()+" ,the v's data is :"); for(int j=0;j<v.size();j++){ String s=v.get( i ) ; System.out.println("******"+s); } } System.out.println("#######end send to all######"); } class Task implements Runnable{ Task(Socket s)throws Exception{ this.socket=s; ois=new ObjectInputStream(s.getInputStream()); oos=new ObjectOutputStream(s.getOutputStream()); } public Socket socket; public ObjectInputStream ois; public ObjectOutputStream oos; public void run(){ try{ sendUserToAll(); }catch(IOException e){ e.printStackTrace(); } } } }
//****************** //Client.java import java.net.Socket; import java.util.Vector; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Client { Socket s; ObjectInputStream ois; ObjectOutputStream oos; Client()throws Exception{ s=new Socket("127.0.0.1",9999); oos=new ObjectOutputStream(s.getOutputStream()); ois=new ObjectInputStream(s.getInputStream()); } public void work()throws Exception { while(true){ Vector v=(Vector)ois.readObject(); System.out.println("received a vector ,this is the data:"); for(int i=0;i<v.size();i++){ System.out.println("******"+(String)(v.get( i ))); } } } public static void main(String[] args)throws Exception{ Client c=new Client(); c.work(); } }
[/iframe] 运行结果如下: 运行Client1: java Client received a vector ,this is the data: ******user1 received a vector ,this is the data: ******user1
再打开一个新的CMD,运行Client2: java Client received a vector ,this is the data: ******user1 ******user2 为什么收到的不是同样的数据 服务器端输出: #######begin send to all###### send v to the port:2563 ,the v's data is : ******user1 #######end send to all###### #######begin send to all###### send v to the port:2563 ,the v's data is : ******user1 ******user1 send v to the port:2564 ,the v's data is : ******user2 ******user2 #######end send to all######
yuhao_1202 edited on 2010-01-15 23:21
|