引用对象的Java序列化是“丢失值"吗? [英] Java Serialization of referenced objects is "losing values"?

查看:230
本文介绍了引用对象的Java序列化是“丢失值"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我遇到了一个我无法理解的有趣问题.我有一个名为"HomeScreenManager"的类,该类维护名为聊天 LinkedList< String> . HomeScreenManager实例位于服务器上,并负责跟踪连接和聊天记录.然后,我创建了一个名为"ProtocolHomeScreen"的类,该类具有名为 chat LinkedList< String> 的类,并实现了Serializable.是时候用聊天更新所有客户端了,我想发送一个ProtocolHomeScreen.因此,我创建了ProtocolHomeScreen的全新实例,并说它的字符串链接列表获取"了HomeScreen的链接列表.因此,换句话说,我要序列化的链表是在另一个类中创建的,但是我是用另一个实例来指向它的.问题是数据没有被发送,另一侧仅显示一个空的LinkedList.我已经逐行完成了调试程序,并检查了所有变量,然后在调用writeObject之前一切似乎都很好,但是在客户端,当我收到ProtocolHomeScreen实例时,链接列表为空! (虽然它不是null)有什么想法吗?谢谢!

I am having an interesting problem that I cannot understand. I have a class called "HomeScreenManager" which maintains a LinkedList<String> called chat. The HomeScreenManager instance lives on the server and is in charge of keeping track of the connections and the chat log. I then created a class called "ProtocolHomeScreen" which is a class with a LinkedList<String> called chat and implements Serializable. When it comes time to update all the clients with the chat, I want to send out a ProtocolHomeScreen. So I create a brand new instance of the ProtocolHomeScreen and I say that it's linked list of strings "gets" the HomeScreen's linked list. So in other words, the linked list that I want to serialize was created in a different class, but I am pointing to it with another instance. The problem is that the data does not get sent, only an empty LinkedList appears on the other side. I've done the debugger line by line and inspected all the variables and right before I call writeObject everything appears to be fine, but then on the client side, when I receive the ProtocolHomeScreen instance, the linked list is empty! (its not null though) Any ideas? Thank you!

public class HomeScreenManager implements ObjectMessageHandler, Serializable
{
protected LinkedList<User> users;
protected LinkedList<String> chat;
protected LinkedList<String> sessions;

public HomeScreenManager()
{
    chat = new LinkedList<String>();
    users = new LinkedList<User>();
    sessions = new LinkedList<String>();
}
protected void handleChat(String message)
{
    chat.add(message);

    ProtocolHomeScreen p = new ProtocolHomeScreen(this);

    for(User u:users)
    {
        u.connection.WriteObject(p); //At this point in time, p has the correct data, but when I deserialize p, the chat list is empty...
    }
}

然后是协议类...

public class ProtocolHomeScreen implements Serializable
{
    public LinkedList<String> chat;
    public LinkedList<String> players;
    public LinkedList<String> sessions;


    public ProtocolHomeScreen(HomeScreenManager hms)
    {
        players = new LinkedList<String>();
        for(User u:hms.getUsers())
        {
            players.add(u.getUsername());
        }
        sessions = hms.getSessions();
        chat = hms.getChat();

        chat = new LinkedList<String>();

        for(String s: hms.getChat())
        {
            chat.add(s);
        }

    }

}

修改

这是客户端上的代码...请注意,播放器的LinkedList碰巧很好,但是我认为这与我在ProtocolHomeScreen的构造函数中创建了新实例有关(在服务器端)

This is the code on the client side... note that the player's LinkedList comes across just fine, but I think that has something to do with the fact that I created a new instance in the constructor of the ProtocolHomeScreen (on the server side)

else if(obj instanceof ProtocolHomeScreen)
        {       
            ProtocolHomeScreen phs = (ProtocolHomeScreen) obj;

            sessionModel.removeAllElements();
            playersModel.removeAllElements();
            chatOutput.setText("");
            for(String s:phs.players)
                playersModel.addElement(s);

            for(String s:phs.sessions)
                sessionModel.addElement(s); 

            for(String s:phs.chat)
            {
                chatOutput.setText(chatOutput.getText()+s);
            }

        }

推荐答案

您需要查看ObjectOutputStream.reset()及其存在的原因.除非您reset()流或使用writeUnshared().

You need to look at ObjectOutputStream.reset() and why it exists. Any given object is only sent once over an ObjectOutputStream unless you reset() the stream or use writeUnshared().

这篇关于引用对象的Java序列化是“丢失值"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆