Android 确切地说,“断管”异常对 Socket 意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10899391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What, exactly, does a "broken pipe" exception mean to the Socket?
提问by user316117
I have an android app that talks to a program on a PC. I'm using the Android (Java) Socket class. If I stop and restart the PC app the next time I send something from Android I get an IO Exception "Broken Pipe". My question is not about that. Here's the question:
我有一个与 PC 上的程序对话的 android 应用程序。我正在使用 Android (Java) Socket 类。如果我下次从 Android 发送内容时停止并重新启动 PC 应用程序,则会收到 IO 异常“Broken Pipe”。我的问题与此无关。这是问题:
After getting the broken pipe exception if I query the Socket's isClosed()method it returns false (i.e., it's not closed), and if I query the Socket's isConnected()method it returns true, i.e., that it IS connected. Could someone please explain these results to me? Thanks in advance!
如果我查询 Socket 的isClosed()方法,在得到管道损坏异常后,它返回 false(即它没有关闭),如果我查询 Socket 的isConnected()方法,它返回 true,即它已连接。有人可以向我解释这些结果吗?提前致谢!
采纳答案by Ben Barden
Broken pipe means pretty much exactly what you're talking about here. The program on your side still has its socket wide open, but the socket on the other side is no longer in communication, and didn't go through the standard "close pipe" procedure. This can happen if the other side lost power suddenly, if the physical line was severed, or whatever. As such, locally the socket is registering as both open and connected - it's just connected to a broken pipe. Did you wish some practical advice here, or just the theory?
破碎的管道几乎意味着您在这里谈论的内容。你这边的程序仍然敞开着它的套接字,但另一边的套接字不再通信,也没有通过标准的“关闭管道”程序。如果对方突然断电、物理线路被切断或其他任何情况,都可能发生这种情况。因此,在本地,套接字注册为打开和连接状态——它只是连接到一个损坏的管道。你想要一些实用的建议,还是只是理论?
回答by user207421
Socket.isClosed()
and Socket.isConnected()
only tell you what youhave done to the socket.They aren't there to tell you anything about the state of the connection.You haven't closed the socket: it's open. You connected the socket: it's connected.
Socket.isClosed()
并且Socket.isConnected()
只告诉你你对套接字做了什么。他们不会告诉您有关连接状态的任何信息。你还没有关闭套接字:它是打开的。您连接了套接字:它已连接。
When you get any IOException
operating a Socket
other than SocketTimeoutException
you must close the socket.
当您进行任何IOException
操作时Socket
,SocketTimeoutException
您必须关闭套接字。