java - JAVA是什么导致网络套接字上的流结束?
问题描述
据我了解,整点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。
推荐阅读
- amazon-web-services - 使用 Multer 和 nodejs 上传图像而不存储它
- php - 如何访问本地文件?
- python - 当用户在他的个人资料中更改他的电子邮件时,有没有办法确认用户电子邮件 - django
- r - How can I convert a list of data frames into a data frame of lists using purrr or tidyr?
- typescript - Fastify: ts-node shoot error 属性 'foo' 在类型 'FastifyInstance<...>' 上不存在
- javascript - 为微型富文本编辑器创建点击弹出窗口
- typescript - ReactNative Animated.ScrollView ref TypeScript 定义
- javascript - 我不确定为什么我的 Fetch-Post 请求未定义
- flutter - 由于谷歌地图,颤振应用程序无法编译
- unique - vega:过滤每组的第n个