Answer the question
In order to leave comments, you need to log in
How to check socket connection in android?
Good afternoon. The task is to make an Android application that, being in the same Wi-Fi network with the server, communicates with it via TCP via the Socket. It was possible to implement client-server communication, but there is one point. If the Server abruptly stops working, but does not close the connection (for example, the lights are turned off), how does the android device know about it? The standard methods Socket isConnected, isClosed are not suitable, because perform slightly different functions. I googled all over but didn't find a solution. It was written in many places that Socket does not implement checking for the presence or absence of a connection. In general, the question for connoisseurs is how to find out that the server has crashed?
Answer the question
In order to leave comments, you need to log in
To do this, you need to implement keep-alive, that is, constantly send special requests with a short interval, and let the server respond to them, and it’s better not to rely on the standard implementation in the library, it can easily depend on the OS.
By the way, without keep-alive, that is, without activity, the connection will close by itself after a few hours, there is not even any need for any failures.
Also, the connection may disappear already when the packet is sent (loss of contact in the connector, etc.), and then resume again, then everything will go without any Exception, but the receiving side will receive a damaged packet (with zero bytes starting from some number) , so the receiving side must check the packet and re-request if necessary (send a special request that re-requests this packet), on the reverse side, check again, and so on ad infinitum until the session is successful.
Finally, you need to check if 2 packets can "merge" into 1 with frequent sending, and if they can, then take action - designate artificial boundaries.
A rather complicated algorithm is obtained. It’s also good that the network is local, and there are no bots in it, and the risk of failures is lower.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question