简体   繁体   English

通过套接字多次发送序列化对象

[英]Sending serialized Objects multiple times over a Socket

The client sends specific Objects multiple times over a socket to the server. 客户端通过套接字多次向服务器发送特定的对象。 Code looks like this: 代码如下:

public void send(Operation operation, Object o){
    try{
    out.writeObject(operation);
    if (o != null) out.writeObject(o);
    out.flush();
    } catch (IOException e) {
        System.err.println("Couldn't write to OutputStream");
    }   
}

In the Object is a HashMap from Integer to Integer, thats altering very often. 在对象中是一个从Integer到Integer的HashMap,多数民众赞成在经常更改。 The Server accepts the Message with: 服务器通过以下方式接受消息:

User newUser = (User) oin.readObject();

The first time everything works well, the newUser Objects contains of the new received Object. 第一次一切正常时,newUser对象包含新接收到的对象。 But after the second, third, ... execution, the newUser Object always reads the old Object (the HashMap contains the old Values from the first communication). 但是在执行第二,第三,...之后,newUser对象总是读取旧对象(HashMap包含来自第一次通信的旧值)。 What am i doing wrong? 我究竟做错了什么?

Thanks in advance! 提前致谢!

You need to either call reset() regularly to stop the cache of objects building up, preventing you from seeing changes to those objects, or you need to use writeUnshared() 您需要定期调用reset()以停止建立对象的缓存,从而防止看到这些对象的更改,或者您需要使用writeUnshared()

I would consider calling reset() before flush() each time. 我会考虑每次在flush()之前调用reset()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM