套接字聊天App- NotSerializableException

时间:2016-04-05 19:14:45

标签: java sockets

我正在与服务器和多个客户端构建聊天应用程序。当用户连接到服务器时,服务器调用notify_clients()并在线发送客户列表到每个客户端以显示在客户端"在线列表"。

  server.notify_clients(new MessageInfo(server_back_end.server_front_end.clients));

但是,我得到NotSerializableException(输出在底部)。

当我在线搜索时,解决方案经常为进程中使用的每个类和内部类实现Serializable。我在我的程序中的每个班级都继续implements Serializable,它仍然在下面显示的行中抛出异常:

public void notify_clients(MessageInfo message_info)
    {
            try
            {
                for (int i = 0; i < client_list.getSize(); i++)
                {
                ClientInfo client = getElementAt(i);
                ObjectOutputStream writer = client_list.indexOf(client).writer;
                writer.writeObject(message_info); //Exception thrown here
                writer.flush(); //doesnt reach here
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
   }

ClientInfo类:

public class ClientInfo implements Serializable
{
    public ObjectOutputStream writer;
    public ObjectInputStream reader;
    public String user_name;


    /*Constructor*/
    public ClientInfo(ObjectOutputStream writer, ObjectInputStream reader, String user_name)
    {
        this.reader = reader;
        this.writer = writer;
        this.user_name = user_name;
    }


    public ClientInfo(ObjectOutputStream writer, ObjectInputStream reader)
    {
        this.reader = reader;
        this.writer = writer;
        try
        {
            this.user_name = (String) this.reader.readObject();
        }
        catch (IOException | ClassNotFoundException ignored)
        {
        }
    }


    @Override
    public String toString()
    {
        return this.user_name;
    }
}//end class

MessageInfo类:

public class MessageInfo implements Serializable
    {
        public List<ClientInfo> clients;
        public List<ClientInfo> recipients;
        public String message_contents;


        public MessageInfo(List<ClientInfo> clients)
        {
            this.clients = clients;
        }
    }

我得到的错误:

java.io.NotSerializableException: java.io.ObjectInputStream
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at java.util.ArrayList.writeObject(ArrayList.java:762)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at ServerBackEnd.broadcast(ServerBackEnd.java:110)
    at ConnectionThread.add_incoming_clients(ServerBackEnd.java:211)
    at ConnectionThread.run(ServerBackEnd.java:184)

有人可以告诉我我需要做什么吗?

1 个答案:

答案 0 :(得分:1)

您应该将transient修饰符添加到读者/作者

    public transient ObjectOutputStream writer;
    public transient ObjectInputStream reader;