Question

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).

Was it helpful?

Solution 2

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

OTHER TIPS

A quick look at the javadoc entry for DataInputStream#readInt (link) taught me that, this method throws an EOFException (which stands for End Of File Exception) when the stream you're trying to read and int from ends within less than 4 bytes (a Java int being made of four bytes).

You may need to use some interface that blocks until the stream contains enough data :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top