简体   繁体   中英

Sockets Chat App- NotSerializableException

I am building a chat application with server and multiple clients. When a user connects to the server, the server calls notify_clients() and send a list of clients online to each client to be displayed in the clients "online list".

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

However, I am getting NotSerializableException (output at the bottom).

When I searched online, the solution was frequently implementing Serializable for every class and inner class used in the process. I went ahead and implements Serializable on every class in my program, and it still throws the exception at the line shown below:

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();
            }
   }

The ClientInfo class:

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

The MessageInfo class:

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;
        }
    }

The error I am getting:

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)

Can someone tell me what I need to do?

You should add the transient modifier to reader/writer

    public transient ObjectOutputStream writer;
    public transient ObjectInputStream reader;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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