How to detect if Socket is closed by remote peer?
I am writing a proxy in Java. I want to know, how one may detect if the server or client has closed its socket? I am using pre-NIO sockets.
I have used read() method for reading. When I am reading (with read() ) as a client (from the server) if I get -1, does it mean that the server has closed
In the same way, If I am reading (with read()) from the client and get -1, does it mean that the client has closed the socket?
What is the best way to detect if a remote peer has closed the socket or not?
If I use methods like isInputShutdown() or isOutputShutdown(), will that help?
My main goal is read into a byte array until the remote peer has closed the socket? Any sample code will be much appreciated.
Re: How to detect if Socket is closed by remote peer?
the -1 just tells you that there's NOTHING more in the "pipeline" and not about the state of the Socket. The Same with ]isInputShutdown() or isOutputShutdown(). Both refer to the state of the In- or Out-pipeline.
Originally Posted by anis_huq
Only the method isClosed() returns the closed state of the socket.