Checking Ethernet Connection

Discussion to talk about software related topics only.
Post Reply
Jcz
Posts: 7
Joined: Wed Mar 03, 2021 7:04 am

Checking Ethernet Connection

Post by Jcz »

I have a websocket running on my mod54415 system that gets data sent through it a few times per second.
I'm wondering if there's an elegant way besides what I'm currently doing to check if the ethernet connection is lost (like if the cable gets yanked out) and the system isn't expecting the loss in connectivity. This might end up being needed elsewhere to detect lose of connection with an application talking to the system via TCP, there isn't currently a heartbeat check on that.

I'm currently polling the GetDHCPAddress() function before updating the websocket. So if that fails then it doesn't keep trying to send data through the websocket.

In testing once the cable is reconnected the socket closes and opens as expected. Before the function polling, the system would eventually trap and reset. I tried checking the error_fds rather than polling GetDHCPAddress() but nothing triggered when the ethernet was removed. I assume with my fairly surface level understanding of websockets that it wouldn't know that it was closed since the fd wasn't closed properly.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Checking Ethernet Connection

Post by pbreed »

You can check link status with: (Same code for 2.X and 3.X)


#include <netinterface.h>
bool GetInterfaceLink(int ifn);
or if you just want the first interface..
bool bHaveLink=GetInterfaceLink(GetFirstInterface());
Jcz
Posts: 7
Joined: Wed Mar 03, 2021 7:04 am

Re: Checking Ethernet Connection

Post by Jcz »

Thanks a lot!
My netinterface.h didn't have GetInterfaceLink, I'm running 2.7.1
But I did find GetInterFaceBlock which also appears to report back whether or not the connection is alive.

I've found the TestDHCP example code, I think I should be able to get what I need from this.
Post Reply