javainputstreamobjectinputstreameofexception

JAVA what causes end of stream on network sockets?


From what I understand the whole point of end of stream is to tell the input stream when to stop reading data, which makes sense in file streams, but when it's an ObjectInputStream why would I want it to stop reading objects if I might send another one in the future.

In my case the ObjectInputStream throws an EOFException whenever it decides that something is causing the end of stream.

Code segment:

Sending:

public synchronized void sendCommand(CommandBase command){
    try {
        commandOutputStream.writeUnshared(command);
        commandOutputStream.flush();
        System.out.println("Sent " + command.getAction().toString());
    } catch (IOException e) {
        System.out.println("Failed to send " + command.getAction().toString());
        try {
            commandOutputStream.close();
            running = false;
            this.dispose();
            System.out.println("Closing command output stream");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }
}

Receiving

while(going && tcpSender.running){
        //Get action from stream
        try {
            System.out.println("Available bytes: " + commandInputStream.available());
            Object command = ((CommandBase) commandInputStream.readObject());
            System.out.println("Action received");

            if (command instanceof CommandBase) {
                if(((CommandBase)command).getAction().equals(ActionType.MouseAction)){
                    System.out.println("Mouse action");
                    //JOptionPane.showMessageDialog(null, "Received Mouse Action");
                    ((MouseCommand)command).doAction(operator);
                }else if(((CommandBase)command).getAction().equals(ActionType.KeyboardAction)){
                    System.out.println("Keyboard action");
                    //JOptionPane.showMessageDialog(null, "Received Keyboard Action: " + ((KeyboardCommand)command).toString());
                    ((KeyboardCommand)command).doAction(operator);
                }else if(((CommandBase)command).getAction().equals(ActionType.CheckBooleanAction)){
                    System.out.println("Check boolean action");
                    udpSender.notifyThis((CheckBooleanCommand)command);
                }else{
                    //JOptionPane.showMessageDialog(null, "Received unknown command " + ((CommandBase)command).getAction().toString());
                    System.out.println("Action type is: " + ((CommandBase)command).getAction().toString());                         
                }
            }
        } catch (EOFException e) {
            System.out.println("EOF-Exception - end of stream reached");
            e.printStackTrace();
            continue;
        } catch(IOException e){
            System.out.println("IO-Exception - Unable to read action \n Closing socket");
            try {
                commandInputStream.close();
                System.out.println("Closed command input stream");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            going = false;
            tcpSender.running = false;
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found - failed to cast to Action");
            e.printStackTrace();
        } 

    }

TL;DR: How does the ObjectInputStream decide that it's the end of a stream, and what causes it? And how can I solve this problem?

Error message:

`

java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)

at java.io.ObjectInputStream.readObject0(Unknown Source)

at java.io.ObjectInputStream.readObject(Unknown Source)


Solution

  • How does the ObjectInputStream decide that it's the end of a stream

    It happens when ObjectInputStream attempts to read from the underlying stream, and that stream returns -1 ... which means that the end-of-stream has been reached.

    and what causes it?

    In this case, the most likely explanation is that the remote end (the sender) has closed its socket, or it has crashed ... which will cause the socket to close.

    (It is theoretically possible that the connection has been broken, but that is only likely to happen in obscure circumstances, so lets ignore that.)

    And how can I solve this problem?

    You need to understand why the sending end has closed the socket. Unfortunately, we cannot tell why that is because the code you have provided is not an MCVE.