Plug in Java socket connection when disconnected

I have Java code that makes long-term connection of sockets to a home automation device to control it for activity / events. The problem is that if the device reboots or receives power, the Java code should automatically connect as soon as possible. Any ideas on how to detect a dropped connection on the Java side, preferably without periodically polling the device?

+6
java sockets
source share
3 answers

If the device reboots without properly closing the socket, I don’t know anything that will allow you to detect it. Your server will not be able to detect that the socket is gone, in this case, if the device does not return or until the TCP / IP stack on your server surrenders and announces that the connection is dead. At some point you will get an IOException. . You need to catch this exception and then reconnect when it happens.

You are really dominated by TCP / IP behavior here. When one end of the connection simply disappears (there is no FIN packet), the other end can wait for an arbitrary amount of time (depending on many factors) before deciding that the connection is dead.

If you want to catch this problem earlier - and you can guarantee that the device will respond within the set time, then set a timeout on the socket. If you do this, you will get a SocketTimeoutException if the device is not responding, and then you can close the socket and try opening the socket again. I have used this with success.

+3
source share

If your software is just waiting for data from the home automation device, the only way to detect that the device is gone is to enable the SO_KEEPALIVE parameter in the slot, which is open for communication with the device.

To do this, simply call the setKeepAlive (true) method on the socket, and this will cause the main socket implementation to include the SO_KEEP_ALIVE parameter.

This will cause the base implementation to periodically exchange some data with the remote endpoint, and if the device dies while waiting for data, an exception will be thrown.

The only problem is that maintaining the latency depends on the operating system and sometimes cannot be changed.

If you need shorter timeouts, there is no other way than to implement a periodic probe on the device.

+2
source share

I solved this same problem.

If you want to catch this problem earlier - and you can guarantee that the device will respond within the set time, then set a timeout on the socket. If you do this, you will get a SocketTimeoutException if the device does not respond, and you can close the socket and try opening the socket again. I have used this with success.

Quoted from above:

I force it to close the socket, then try to open it again, and it runs this code well. The only problem is that as soon as I close the socket I cannot open it again, I get an exception that the socket is closed. This happens every time. How to close the socket and then open it again to receive this exception?

+1
source share

All Articles