gpt4 book ai didi

java - 套接字聊天应用程序 - NotSerializedException

转载 作者:行者123 更新时间:2023-12-01 10:06:22 27 4
gpt4 key购买 nike

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

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

但是,我收到 NotSerializedException (输出在底部)。

当我在线搜索时,解决方案经常是为流程中使用的每个类和内部类实现Serialized。我继续在程序中的每个类上实现了可序列化,但它仍然在下面所示的行中抛出异常:

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)

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

最佳答案

您应该将 transient 修饰符添加到读取器/写入器

    public transient ObjectOutputStream writer;
public transient ObjectInputStream reader;

关于java - 套接字聊天应用程序 - NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36435146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com