How do you detect if Socket#close() has been called on a socket on the remote side?
How do you detect if Socket#close() has been called on a socket on the
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
isConnectedmethod won’t help, it will returntrueeven if the remote side has closed the socket. Try this:Start the server, start the client. You’ll see that it prints ‘connected: true’ twice, even though the socket is closed the second time.
The only way to really find out is by reading (you’ll get -1 as return value) or writing (an
IOException(broken pipe) will be thrown) on the associated Input/OutputStreams.