How to handle the network errors in SMACK?

Hi, everybody

I have a problem now.

I want to write a client with smack.

And this is my problem:

As a user connected and login to the server.

After that, something is wrong to the network, the client can’t connect to the server.

How can I catch the event?

I tried on this way.

I use a thread to check the last active time(this time will be updated every packet been received)

I sent a ping packet to server every 30 seconds to keep the connection not be timeout.

And if the last active time is more than 60 seconds(most possiably, it’s due to the unreachable server), I will call XMPPConnection.disconnect() to mark the connection closed.

And another thead will try to reconnect to the server.

but I found that if the server is unreachable, the disconnect function will cost too much time, maybe 5-10 min, to close the reader and writer.

How can I make a quick disconnect when the server is unreachable?