简体   繁体   English

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

[英]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. 据我了解end of stream的全部end of stream是告诉输入流何时停止读取数据,这在文件流中是有意义的,但是当它是一个ObjectInputStream为什么我希望它停止读取对象(如果我可以发送另一个)在将来。

In my case the ObjectInputStream throws an EOFException whenever it decides that something is causing the end of stream. 在我的情况下,只要ObjectInputStream决定导致流结束的原因,就会抛出EOFException

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? TL; DR: ObjectInputStream如何确定它是流的结尾,并且是什么原因造成的? And how can I solve this problem? 我该如何解决这个问题?

Error message: 错误信息:

` `

java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) java.io.ObjectInputStream $ BlockDataInputStream.peekByte处的java.io.EOFException(未知源)

at java.io.ObjectInputStream.readObject0(Unknown Source) 在java.io.ObjectInputStream.readObject0(未知来源)

at java.io.ObjectInputStream.readObject(Unknown Source) 在java.io.ObjectInputStream.readObject(未知来源)

How does the ObjectInputStream decide that it's the end of a stream ObjectInputStream如何确定它是流的结尾

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

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. 不幸的是,由于您提供的代码不是MCVE,我们不能说出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM