【问题标题】:ArrayList Doesnt update contents over socket [duplicate]ArrayList不通过套接字更新内容[重复]
【发布时间】:2014-05-27 12:28:49
【问题描述】:

我有一个典型的多线程服务器和一个通过套接字与服务器连接并执行一些操作的客户端。

客户端部分:

try {
        mySocket=new Socket("localhost", 1234);


    ObjectOutputStream oos= new ObjectOutputStream(mySocket.getOutputStream());  
    ObjectInputStream  ois= new ObjectInputStream(mySocket.getInputStream());


           try{
               while(true){

               //Here user is asked for input the protocol string  

                    if("one message".equals(protocol))                             
                       {   
                        oos.writeObject(protocol); 
                        oos.flush();

                        mylist=(ArrayList<MyClass>)ois.readObject();
                        System.out.println(mylist);
                       }


                    if("two message".equals(protocol))  
                        { 
                          //tell server to change few things    on the list                                               
                        }

}//end while        
        } //end inner try
                    catch (IOException | ClassNotFoundException ex)
                    {
                        // ex.printStackTrace();
                    }

            ois.close();
            oos.close();
            mySocket.close();

}//end outer try 
        catch (IOException e)
    {
        //message
    } 

和服务器端:

public void run() {
try{

    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());               
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());  

    String protocol;


  try{

  while(true)
    {            
        protocol= (String)ois.readObject();


    if ("one message".equals(protocol))
            {                                
             oos.writeObject(list); 
             oos.flush();

             System.out.println(list);
         }


     if ("two message".equals(protocol))

    {
      //change objects in the list          
    }



} //end while true

    } //end try
                catch (IOException | ClassNotFoundException ex)
                {                        
                   // ex.printStackTrace();
                }

            ois.close();
            oos.close();
            socket.close();
}  
  catch (Exception ex)
            {                   
                //ex.printStackTrace();
            }
}

有问题的部分在这里:

if ("one message".equals(protocol))
                {

                 oos.writeObject(list); //sends the wrong (?)
                 oos.flush();

                 System.out.println(list); //prints the correct
             }

服务器似乎每次用户更改某些内容时都会打印正确的列表。但是随后,无论客户端进行多少更改,客户端都会接收并打印第一个列表。如果另一个线程来询问列表,它将获取最新列表,如果该线程或另一个线程进行更多更改,服务器仍将每次从其一侧打印正确的列表,但客户端总是会返回错误的列表,即该客户端线程进入时存在的列表。

【问题讨论】:

    标签: java multithreading sockets debugging arraylist


    【解决方案1】:

    ObjectOutputStream,如果你要求它发送一个它已经发送的对象,它只是发送一个对先前发送的对象的引用。这就是允许发送复杂的对象图的原因,在对象之间具有双向引用或循环。

    如果您希望流忘记它已经发送的内容,那么您需要调用reset() 方法。

    【讨论】:

    • 我爱你,谢谢它显然有效。别的,一旦你使用了 reset() 就不需要使用 flash() 了吗?它只对我有用 reset() 而不是 flash() 很好,但最佳技术是什么?
    • 如果需要flush(),则调用flush()。如果需要reset(),则调用reset()。他们做不同的事情,文档并没有说 reset() 也会刷新流。
    【解决方案2】:

    您是序列化流中对象缓存的受害者。第一次编写对象时,会为该引用地址创建它的缓存版本。然后每次你写它(即使你改变了内容)它不会再次写它,因为它说“我已经在这个地址看到了对象”。

    解决此问题的最佳方法是将套接字流保留在 while 循环之外,但将 ObjectOutput 和 ObjectInput 流放在 while 循环中。这应该可以解决您的问题。通常其中一组是为一个稳定的对象图设计的。

    【讨论】:

    • 感谢输入,下次记住了。
    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 2018-12-29
    • 2014-07-24
    • 1970-01-01
    • 2015-01-26
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多