[英]JAVA what causes end of stream on network sockets?
據我了解end of stream
的全部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.ObjectInputStream $ BlockDataInputStream.peekByte處的java.io.EOFException(未知源)
在java.io.ObjectInputStream.readObject0(未知來源)
在java.io.ObjectInputStream.readObject(未知來源)
ObjectInputStream
如何確定它是流的結尾
當ObjectInputStream
嘗試從基礎流中讀取時,就會發生這種情況,並且該流返回-1
...,這意味着已到達流的末尾。
是什么原因造成的?
在這種情況下,最可能的解釋是遠端(發送方)已關閉其套接字,或者它崩潰了……這將導致套接字關閉。
(從理論上講,連接可能已斷開,但這僅可能在晦澀的情況下發生,因此請忽略它。)
我該如何解決這個問題?
您需要了解為什么發送端關閉了套接字。 不幸的是,由於您提供的代碼不是MCVE,我們不能說出原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.