JAVA导致网络套接字流结束的原因是什么?

时间:2018-06-18 13:20:48

标签: java inputstream objectinputstream eofexception

据我所知,end of stream的重点是告诉输入流何时停止读取数据,这在文件流中是有意义的,但是当它是ObjectInputStream时为什么我要它呢?如果我将来再发送一个对象,请停止阅读。

在我的情况下,ObjectInputStream会在确定某些内容导致流结束时抛出EOFException

代码段:

发送:

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();
    }
}

接收

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:ObjectInputStream如何确定它是流的结尾,是什么原因造成的? 我该如何解决这个问题?

错误讯息:

`

  

java.io.EOFException at   java.io.ObjectInputStream中的$ BlockDataInputStream.peekByte(未知   源)

     

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

     

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

1 个答案:

答案 0 :(得分:1)

  

ObjectInputStream如何确定它是流的结尾

ObjectInputStream尝试从基础流中读取时,会发生这种情况,并且该流返回-1 ...这意味着已到达流末尾。

  

是什么原因造成的?

在这种情况下,最可能的解释是远程端(发送方)已关闭其套接字,或者它已崩溃...这将导致套接字关闭。

(理论上可能连接已被破坏,但这种情况很可能发生在不明显的情况下,所以我们忽略它。)

  

我该如何解决这个问题?

您需要了解发送端关闭套接字的原因。不幸的是,我们无法分辨为什么这是因为您提供的代码不是MCVE。