6 years, 10 months ago.

EthernetInterface Network reconnection

I am using MBED-OS 5.3. My hardware is the FRDM-K64F I have set up a TCPServer and I am trying to get the error recovery conditions accounted for. If I come up and have a valid Ethernet connection and I get an IP address through DHCP and then I start listening for a connection and I then disconnect the Ethernet cable I get no indication I have lost the network, the accept() method continues to block. I can plug the network back in and still get no indication. The same happens if I have accepted a connection but am waiting to receive a message. The recv method just blocks and does not throw an error. I would have though that in both cases I would have gotten a NSAPI_ERROR_NO_CONNECTION. I have tried setting the timeout on the socket but all I get back is the NSAPI_ERROR_WOULD_BLOCK and no indication there is any other error. How can I tell that I have lost my connection or if there is any other error I need to deal with?

1 Answer

6 years, 10 months ago.

Have you looked into KEEPALIVE settings? IIRC, Keepalive is off by default in the stock settings of LWIP on MBED. There are multiple schools-of-thought on how to deal with quiet connections versus a broken cable. You might be able to look into how the connection handler deals with a loss of link and then use that to message your application to quit trying. Also ponder the case where the problem is not local between your card and the network but the far-end is somehow disconnected. How would you route a message back to you from a network point 20 hops away for example? Keepalive is the only practical method using TCP that I am aware of.

Yes - keep alive will work for the TCP connection. However that is not the issue, the real issue is that once the local cable connection is lost so is the IP address in the interface and if you are relying on DHCP you must disconnect and reconnect the network for LWIP to go through that process. So what ends up happening is that you timeout due to inactivity and you think everything is just fine, you go back to listening for a new client but that client never comes because you have no IP address. Currently I am using the blocking timeout timer to solve this problem and if it times out I shut down the network and reconnect it. That seems to work but in the case where I have a sleepy client or there is a loss in comms somewhere else (as you mention) I am doing a lot of extra work.

posted by Dan Sexton 27 May 2017

Yep. That is another problem twisted into the whole issue. I have not looked into mbed's handler for link interrupt but it should be available to alert you that a link has dropped at which point you can start over and re-establish connection and ip address....hopefully some others will chime in on this topic as well...

posted by Bill Bellis 28 May 2017