How detect TCP/IP socket shutdown when ethernet cable is disconnected

Hi,

I want to code TCP/IP client/server in linux application capable to fastly detect ethernet cable disconnection in any condition.

So I have activate SO_KEEPALIVE options and set TCP_KEEPCNT, TCP_KEEPIDLE and TCP_KEEPINTVL to 1.

When I disconnect ethernet cable I have the following behaviour :

  • If I wasn't sending data when I disconnect the cable, the TCP Keepavlie start and detect disconnection : expected behaviour.
  • If I was sending data when I disconnect ethernet cable, I was notify that connection is down only after about 16 minutes : not expected behaviour, time very too long.

I have look with tcpdump the packet on the device. If I was sending data when I disconnect the ethernet cable, I packet is resend 8 times (normal behaviour of TCP) but after the last TCP request without acknoledge, I wasn't notify that the connection is down

Could you help me to solve this behaviour please?

Regards

Try taking a look at the SIOCETHTOOL ioctl. This can be used to check if a physical connection is present on an Ethernet port.

Hi gratuitous_arp

SIOCETHTOOL ioctl command allow to know is physical connection is present.

I my case, I have physical connection with a switch but the connection between the distant equipment will be broken during exchange. So, the TCP acknowledge will not be receive, TCP request will be resend as soon as tcp_retry is reach. After these retry, even if I have asked to send more data with the send command, no more data will be send from my computer to the distant computer (no more traces with tcpdump). After about 15 minutes, the recv command return with error "no route to host". But, I wanted to detect the socket disconnection after some secondes.

If you have any other idea, don't hesitate to propose it.

Regards

TCP does best-effort delivery, which is why it has such obnoxiously long timeouts. I've seen it survive 10-minute network outages. It's really not designed to do what you want. Try pinging your host to see if it responds or not.

Hi Corona688,

How can I ping the host in C++ application?

Thanks in advance for your response.

ping uses icmp, port 5813 (as I remember)

It is far easier simply to call popen() with a ping command ( [code]/usr/bin/ping [remote address] [code]), read the return code, than to try to rewrite ping.