gpt4 book ai didi

java - 通过套接字发送可序列化对象时出现 NotSerializableException?

转载 作者:行者123 更新时间:2023-11-29 09:45:33 25 4
gpt4 key购买 nike

我正在尝试通过套接字连接发送定制对象。该类实现了可序列化,但当我尝试将对象写入套接字时,构造函数仍然抛出 NotSerializableException。我将在下面发布相关代码:

public class serilizableArrayHolder implements Serializable {
private ArrayList<Client> array= null;

public serilizableArrayHolder(ArrayList<Client> array) {
this.array=array;
}

public ArrayList<Client> getArray() {
return array;
}
}

这是我定制的类(class)。现在我正在尝试从服务器向客户端发送一个数组列表,但我会在稍后阶段添加其他信息。发送方法在我的服务器类中发布如下:

public void sendData(Socket clientSocket){
ObjectOutputStream out;

try {
serilizableArrayHolder temp = new serilizableArrayHolder(clientCollection);
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(temp); <---This line generates the error.
out.flush();
} catch (IOException e) {
e.printStackTrace();
}

}

这是我从服务器发送的方法。 clientCollection 是我尝试发送的 arrayList。

整个客户端类:

public class Client implements Runnable, Serializable{
public Thread thread = new Thread(this);
private Socket s;
private DataInputStream in;
private DataOutputStream out;
private ObjectOutputStream objOut;
private ServerMain server=null;
private String host=null;
private Client c;
private String userName;

public Client(Socket s, String host, ServerMain server) throws IOException{
c=this;
this.host=host;
this.s=s;
this.server=server;
this.userName=userName;

in= new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
objOut=new ObjectOutputStream(s.getOutputStream());
thread.start();
}

public String getClientInfo(){
return host;
}
public String getUserName(){
return userName;
}
public void send(String s){
try {
out.writeUTF(s);
}
catch (IOException e){
}
}

public void run() {

while(true){
try {
String temp = in.readUTF();
if(temp.equals("sendOnline")){
sendOnline();
}

String tempHost=s.getInetAddress().getHostAddress();

server.appendString(tempHost+" Skickade: "+temp+"\n");

}
catch (IOException e) {
String str = s.getInetAddress().getHostName();
server.clientDisconnect(str);
break;
}
}

try {
s.close();
}
catch (IOException e) {
}

}
public void sendOnline(){
serilizableArrayHolder temp = new serilizableArrayHolder(server.getClients());
try {
objOut.writeObject(temp);
objOut.flush();
} catch (IOException e) {

e.printStackTrace();
}
System.out.println("Metoden anropas");
}

新的堆栈跟踪:

java.io.NotSerializableException: java.io.DataInputStream
Metoden anropas
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at java.util.ArrayList.writeObject(ArrayList.java:710)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Gesäll.Client.sendOnline(Client.java:83)
at Gesäll.Client.run(Client.java:58)
at java.lang.Thread.run(Thread.java:722)

最佳答案

异常告诉你问题所在。您的 Client 类不可序列化。要序列化一个对象,它引用的所有对象(等等,可传递的)也需要是可序列化的。如果您不希望它被序列化,您可以将引用标记为 transient

关于java - 通过套接字发送可序列化对象时出现 NotSerializableException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7572191/

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