Search code examples
javaobjectinputstreamobjectoutputstreameofexceptioncustom-object

Why is my ObjectInputStream throwing an EOFException?


I am writing a chat room code and each message is sent back and forth between client and server constantly. I have made all the messages into serialized objects and am using them to pass the information back and forth. Unfortunately this has led to an EOFException being thrown at the very first time the object is being read. The error code is as follows:

Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream.readInt(Unknown Source)
    at Serverside.ChatObject.readObject(ChatObject.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Clientside.Client.run(Client.java:161)
    at Clientside.Client.main(Client.java:233)

I am not sure if this is caused by the ObjectOutputStream never being closed, but if so, how do I work around this? Would I have to reopen the stream every single time an object is going to be written and can the input stream be left in the loop to wait for data?

This is my custom object read/write code:

    private void writeObject(java.io.ObjectOutputStream stream) throws IOException{
    stream.writeObject(type);
    stream.writeObject(chatroom);
    stream.writeObject(target);
    stream.writeObject(sender);
    stream.writeObject(message);
    stream.writeObject(users);
    stream.close();
}

private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException{
    type = stream.readInt();
    chatroom = stream.readInt();
    target = (String) stream.readObject();
    sender = (String) stream.readObject();
    message = (String) stream.readObject();
    users = (ArrayList<String>) stream.readObject();
    stream.close();
}

And this is the first bit of code once the thread has been started on the server side. The issue could not have happened beyond this because the other methods will not have had the ability to be called yet.

    public void run(){
        try{
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());

            while(true){
                out.writeObject(new ChatObject(0));
                out.flush();
                info = (ChatObject) in.readObject();
                name = info.getSender();
                if(name == null){
                    return;
                }
                synchronized (names){
                    if(!names.contains(name)){
                        names.add(name);
                        break;
                    }
                }
            }

If anyone feels it is neccessary, I can include more code as needed (from the object or from the client/writing side).


Solution

  • If you read type with type = stream.readInt(); it should be written with stream.writeInt(type);