I'm trying to create a server that can detect when a client disconnects. I searched online and found this method isReachable()
that from what I understand should be perfect for this. The problem is I tried to test it, but it doesn't work to detect disconnection, it always returns true even after I close the program on the different PC I'm using as a client.
public static void main(String[] args) {
int socketPort = 80;
ServerSocket socket;
try {
socket = new ServerSocket(socketPort);
} catch (IOException e) {
System.out.println("Impossible to open server socket");
System.exit(1);
return;
}
while (true) {
try {
Socket client = socket.accept();
while (true) {
if (client.getInetAddress ().isReachable (60 * 1000)==true)
System.out.println ("still connected");
else
System.out.println ("disconnected");
}
} catch (IOException e) {
System.out.println("Connection Lost");
}
}
}