0

Hi with ESP8266WiFi library, I usually check for connection of a TCP client with client.connected().

But it seems to still return true if my TCP client in an unexpected way (like disconnect to WiFi/ Power loss/...). Which leaves my program stuck in a infinite loop.

Is there a more reliable to check for connection status?

2

You have the inherent problem that a TCP connection is terminated by an explicit "FIN" message from the terminating party, followed by a FIN-ACK and ACK.

Refer to here.

If your client (or server) just powers off without closing the connection properly, that's it. The connection hangs up and no data will be transfered if it is not explicitly handled.

If you must know whether the client is still there, you must use a heartbeat meachanism. That is, after some period time without a response has passed, query the connection party with a "heartbeat" message and see if it responds after a given maximum time. The other way around would be to make the client send a periodic "Keep Alive" message which holds up the connection as long as it is sent repeadetly.

Refer to here and here.

  • Thank to your answer I understood the general idea. I can't find any way to check for the KeepAlive message in the ESP8266 library. So I manually send data to client after x idle seconds, close the connection if the client doesn't reply. On the back of my mind I feel like there would be an built-in way to do this, is there? – MoonFan May 11 '18 at 3:19
  • @MoonFan I couldn't easy find a general piece of code that does it in all cases. You'll have to implement one yourself using a timer to keep track of timestamps (last message received / sent, ...). Besides millis you could a more sophisticated timer library. – Maximilian Gerhardt May 11 '18 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.