gpt4 book ai didi

java - 读取对象时出现 EOFException

转载 作者:行者123 更新时间:2023-11-29 09:30:35 26 4
gpt4 key购买 nike

<分区>

当我尝试通过 Java 中的服务器套接字发送对象时,出现 EOFException。

这是发送部分的代码:

public static void sendObject(Object Object, InetAddress IP, int Port){
try{
Socket receivingSocket = new Socket(IP, Port);
OutputStream OS = receivingSocket.getOutputStream();
ObjectOutputStream OOS = new ObjectOutputStream(OS);

OOS.writeObject(Object);
OOS.close();
OS.close();
receivingSocket.close();
}
catch(Exception e){
System.err.println("Failed to send object to: " + IP + " on port: " + Port);
System.err.println(e);
}
}

我尝试将对象转换为特定对象,但没有成功。问题实际上出现在接收端,如下所示:

public Downloader(int PortStart, int PortStop, Loading_State loading_state){
this.loading_state = loading_state;

for(int i = PortStart;i<PortStop;i++){
try {
SS = new ServerSocket(i);
break;
}
catch (Exception e) {
e.printStackTrace();
}
}
}

public void run(){
try {
S = SS.accept();
IS = S.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!destroy){
while(active){
try {
OIS = new ObjectInputStream(IS);
Character Char = (Character)OIS.readObject();
loading_state.setChar(Char);
pause();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

因此,当我启动下载时,控制台立即吐出:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at input_output.network.Downloader.run(Downloader.java:68)

编辑:该对象确实实现了Serializeable

public class Character implements Serializable{
private static final long serialVersionUID = -6390272842934614484L;

大修改!

我不知道为什么或如何但我认为下载有效但现在它无法将接收到的对象转换为我的对象!

Character Char = (Character)OIS.readObject();
java.lang.ClassCastException: server.database.account.Character cannot be cast to database.Character

有什么想法吗?

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