socketstcpesp8266sming

Keep TCP connection on permanently with ESP8266 TCP client


I am using the wifi chip ESP8266 with SMING framework.

I am able to establish a TCP connection as a client to a remote server. The code for initiating client connection to server is simple.

tcpClient.connect(SERVER_HOST, SERVER_PORT);

Unfortunately, the connection will close after idling for some time. I would like to keep this connection open forever permanently. How can this be done?


Solution

  • You will actually need to monitor the connection state and reconnect it if it failed. Your protocol on top of it will need to keep track of what got actually received by the other side and retransmit it.

    In any wireless network your link may go down for one reason or another and if you need to maintain a long term connection you will need to have it in a layer above TCP itself.

    TCP will continue to be connected as long as both sides allow for it (none of them disconnected) and there are no errors on the link, in this case sending keepalives may actually cause disconnects since the keepalive may fail at one time but the link could recover and if you didn't have the keepalive the link would have stayed up.