Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求助:ObjectInputStream/ObjectOutputStream,同步
yuhao_1202





发贴: 1
积分: 0
于 2010-01-15 23:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这么一个问题:精简版代码如下Sad缩减为仅有服务器和客户端)
[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


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923