What, exactly, does a "broken pipe" exception mean to the Socket?

user316117 picture user316117 · Jun 5, 2012 · Viewed 28k times · Source

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:

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!

Answer

Ben Barden picture Ben Barden · Jun 5, 2012

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?