【问题标题】:JAVA what causes end of stream on network sockets?JAVA是什么导致网络套接字上的流结束?
【发布时间】:2018-06-18 13:20:47
【问题描述】:

据我了解,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 在 java.io.ObjectInputStream$BlockDataInputStream.peekByte(未知 来源)

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

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

【问题讨论】:

  • 请提供负责的代码。具体来说:您在哪里发送/接收数据?
  • @L.Spillner 我在两台计算机之间发送数据(通常在同一个网络上),我无法提供所有代码,因为它是更大系统的一部分,我不认为它会起作用。
  • @L.Spillner 我添加了负责发送/接收的方法

标签: java inputstream objectinputstream eofexception


【解决方案1】:

ObjectInputStream 如何确定它是流的结尾

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

是什么原因造成的?

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

(理论上有可能是连接断开了,但这种情况只可能发生在不明显的情况下,所以让我们忽略它。)

我该如何解决这个问题?

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

【讨论】:

  • 我运行了一个测试,当它抛出EOFException时,发送方仍在尝试向接收方发送文件,还有其他可能的解决方案吗?
  • 你有没有考虑过另一个线程正在关闭commandOutputStream的可能性?
  • 不是这样的。难道你不能在不同的线程上同时在一个套接字的输入和输出流上发送和接收?虽然我不明白为什么会出现这样的问题。
  • 没有。不是这样的。我建议您编写一个 MCVE 并将其添加到问题中。这可能是我们能够为您提供帮助的唯一方法
  • 我无法在新编写的示例中重现该错误,所以我猜我错过了一些东西,如果我可以发送代码链接:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
相关资源
最近更新 更多