Java Socket Programming

前端 未结 6 1780
孤街浪徒
孤街浪徒 2020-12-15 02:02

I am building a simple client/server application using java sockets and experimenting with the ObjectOutputStream etc.

I have been following the tutorial at this url

6条回答
  •  天命终不由人
    2020-12-15 02:37

    Just a reminder.

    When you use ObjectOutputStream keep in mind that it keeps a reference cache. If you write an object, change the object contents, and then send the same object again, you will get duplicate data. For example:

    List list = new ArrayList();
    list.add("value1");
    out.writeObject(list);
    list.clear();
    list.add("value2");
    out.writeObject(list);
    

    Will produce in the client side two lists with the string "value1".

    To avoid this, the reset method must be invoked to reset the stream cache when writing the same object reference multiple times:

    List list = new ArrayList();
    list.add("value1");
    out.writeObject(list);
    out.reset();
    list.clear();
    list.add("value2");
    out.writeObject(list);
    

提交回复
热议问题