[英]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". 当用户连接到服务器时,服务器将调用
notify_clients()
并在线将客户端列表发送给每个客户端,以显示在客户端的“联机列表”中。
server.notify_clients(new MessageInfo(server_back_end.server_front_end.clients));
However, I am getting NotSerializableException
(output at the bottom). 但是,我得到
NotSerializableException
(在底部的输出)。
When I searched online, the solution was frequently implementing Serializable
for every class and inner class used in the process. 当我在线搜索时,该解决方案经常为过程中使用的每个类和内部类实现
Serializable
。 I went ahead and implements Serializable
on every class in my program, and it still throws the exception at the line shown below: 我继续在程序中的每个类上
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();
}
}
The ClientInfo
class: 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
The MessageInfo
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;
}
}
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 您应该将
transient
修饰符添加到读取器/写入器
public transient ObjectOutputStream writer;
public transient ObjectInputStream reader;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.