Reputation: 1656
I'm got the following Java code:
Socket s = new Socket();
s.connect(mySockAddr, myTimeout);
Assuming I don't use the socket, I need to detect a server side connection close (FIN
or RST
packet) as soon as it happens.
For instance, though a thread which checks socket status, or intercepting the FIN
/RST
packets...
How can I detect it?
I've tried with printWriter.checkError()
, socket.isConnected()
, socket.isClosed()
methods but nothing works.
Upvotes: 1
Views: 774
Reputation: 86585
The only way i know of to detect whether the other side has closed the connection is by attempting to read from the input stream. A read from a shut-down socket will return -1. That's your notification that there won't be any more to read.
As far as the other functions go, s.isConnected()
tells you whether you've successfully connect()
ed the socket, and s.isClosed()
would tell you whether you closed it. It tells you nothing about what the other side has done.
Upvotes: 3