This question already has an answer here:
- Java socket API: How to tell if a connection has been closed? 6 answers
How do you detect if Socket#close()
has been called on a socket on the remote side?
This question already has an answer here:
How do you detect if Socket#close()
has been called on a socket on the remote side?
The isConnected
method won\'t help, it will return true
even if the remote side has closed the socket. Try this:
public class MyServer {
public static final int PORT = 12345;
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT);
Socket s = ss.accept();
Thread.sleep(5000);
ss.close();
s.close();
}
}
public class MyClient {
public static void main(String[] args) throws IOException, InterruptedException {
Socket s = SocketFactory.getDefault().createSocket(\"localhost\", MyServer.PORT);
System.out.println(\" connected: \" + s.isConnected());
Thread.sleep(10000);
System.out.println(\" connected: \" + s.isConnected());
}
}
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.
Since the answers deviate I decided to test this and post the result - including the test example.
The server here just writes data to a client and does not expect any input.
The server:
ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
out.println(\"output\");
if (out.checkError()) System.out.println(\"ERROR writing data to socket !!!\");
System.out.println(clientSocket.isConnected());
System.out.println(clientSocket.getInputStream().read());
// thread sleep ...
// break condition , close sockets and the like ...
}
You can also check for socket output stream error while writing to client socket.
out.println(output);
if(out.checkError())
{
throw new Exception(\"Error transmitting data.\");
}
The method Socket.Available will immediately throw a SocketException if the remote system has disconnected/closed the connection.