I get an EOFException when I'm trying to send an object through a server socket in Java.
This is the code on the sending part:
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);
}
}
I tried casting the object to the specific object and that didn't work. The problem actually shows up on the receiving side which looks like this:
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();
}
}
}
}
So when I initiate the download the console immediatly spits out:
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)
Edit: The object does implement Serializeable
public class Character implements Serializable{
private static final long serialVersionUID = -6390272842934614484L;
BIG EDIT!:
I dont know why or how but I think the download works BUT now it can't cast the received object to my object!
Character Char = (Character)OIS.readObject();
java.lang.ClassCastException: server.database.account.Character cannot be cast to database.Character
Any ideas why?