Java NIO:IOException:Broken pipe是什么意思? [英] Java NIO: What does IOException: Broken pipe mean?

查看:151
本文介绍了Java NIO:IOException:Broken pipe是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的一些Java NIO连接,当我有一个 SocketChannel.write(ByteBuffer)调用时,它会抛出一个 IOException :断管。



导致管道损坏的原因是什么,更重要的是,是否可以从该状态恢复?如果它无法恢复,似乎这是一个好的迹象,表明发生了不可逆转的问题,我应该简单地关闭这个套接字连接。这是一个合理的假设吗?是否曾经有一段时间这个 IOException 会在套接字连接首次正确连接时发生(而不是在某个时候失败的工作连接)? / p>

另一方面,在尝试<$ c $之前总是调用 SocketChannel.isConnected()是明智的c> SocketChannel.write(),如果是这样,我还可以假设连接已损坏,如果 SocketChannel.isConnected() SocketChannel.isConnectionPending()都是 false



谢谢!

解决方案


导致破管的原因,更重要的是,是否可以从该状态恢复?


这是由导致连接关闭的原因造成的。 (关闭连接不是您的应用程序:这会导致不同的异常。)



无法恢复连接。你需要开一个新的。


如果它无法恢复,似乎这是一个好的迹象,表明发生了不可逆转的问题我应该简单地关闭这个套接字连接。这是一个合理的假设吗?


是的。一旦收到该异常,套接字将无法再次运行。关闭它是唯一明智的做法。


有没有时间这个 IOException (而不是某个时候失败的工作连接)?


没有。 (或者至少,并非没有颠覆OS网络堆栈,JVM和/或您的应用程序的正确行为。)







在尝试 SocketChannel.write()之前,总是调用 SocketChannel.isConnected()是明智的吗? code> ...


一般情况下,调用 r.isXYZ是一个坏主意()在使用(外部)资源 r 的某些调用之前。 两个调用之间的资源状态很可能会发生变化。最好的做法是执行操作,捕获失败操作产生的 IOException (或其他),并采取任何必要的补救措施。



在这种特殊情况下,调用 isConnected()是没有意义的。如果套接字在过去的某个时刻连接,则该方法被定义为返回 true 。它不会告诉您连接是否仍然有效。确定连接是否仍然存在的唯一方法是尝试使用它;例如做一个读或写。


For some of my Java NIO connections, when I have a SocketChannel.write(ByteBuffer) call, it throws an IOException: "Broken pipe".

What causes a "broken pipe", and, more importantly, is it possible to recover from that state? If it cannot be recovered, it seems this would be a good sign that an irreversible problem has occurred and that I should simply close this socket connection. Is that a reasonable assumption? Is there ever a time when this IOException would occur while the socket connection is still being properly connected in the first place (rather than a working connection that failed at some point)?

On a side note, is it wise to always call SocketChannel.isConnected() before attempting a SocketChannel.write(), and if so, can I also assume that the connection is "broken" and should be closed if both SocketChannel.isConnected() and SocketChannel.isConnectionPending() are both false?

Thanks!

解决方案

What causes a "broken pipe", and more importantly, is it possible to recover from that state?

It is caused by something causing the connection to close. (It is not your application that closed the connection: that would have resulted in a different exception.)

It is not possible to recover the connection. You need to open a new one.

If it cannot be recovered, it seems this would be a good sign that an irreversible problem has occurred and that I should simply close this socket connection. Is that a reasonable assumption?

Yes. Once you've received that exception, the socket won't ever work again. Closing it is is the only sensible thing to do.

Is there ever a time when this IOException would occur while the socket connection is still being properly connected in the first place (rather than a working connection that failed at some point)?

No. (Or at least, not without subverting proper behavior of the OS'es network stack, the JVM and/or your application.)


Is it wise to always call SocketChannel.isConnected() before attempting a SocketChannel.write() ...

In general, it is a bad idea to call r.isXYZ() before some call that uses the (external) resource r. There is a small chance that the state of the resource will change between the two calls. It is a better idea to do the action, catch the IOException (or whatever) resulting from the failed action and take whatever remedial action is required.

In this particular case, calling isConnected() is pointless. The method is defined to return true if the socket was connected at some point in the past. It does not tell you if the connection is still live. The only way to determine if the connection is still alive is to attempt to use it; e.g. do a read or write.

这篇关于Java NIO:IOException:Broken pipe是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆