This is rarely required and often does not work. TCP is a data transfer protocol, if there is no data loss, nothing should be done. Think twice why you need it.
Otherwise, you can try periodically polling the interface and checking the UP flag. If the interface loses the UP flag, the OS is already responding to disconnecting the cable and refusing the interface. man 7 netdevice , see SIOCGIFFLAGS for more.
Network drivers also generate an event even when the cable is connected, but I'm not sure if you can access this user or not. You might want to check out udev as the documentation explicitly mentions network interfaces .
source share