How to Detect the Physical Connected State of a Network Cable/Connector

How to detect the physical connected state of a network cable/connector?

You want to look at the nodes in


/sys/class/net/

I experimented with mine:

Wire Plugged in:

eth0/carrier:1
eth0/operstate:unknown

Wire Removed:

eth0/carrier:0
eth0/operstate:down

Wire Plugged in Again:

eth0/carrier:1
eth0/operstate:up

Side Trick: harvesting all properties at once the easy way:

grep "" eth0/* 

This forms a nice list of key:value pairs.

how a network interface card identifies if the ethernet cable is plugged?

Ethernet over twisted pair uses Fast Link Pulses to (auto) negotiate a link: each port advertises its link capabilities, e.g. 1000BASE-T full duplex, 100BASE-TX full duplex, 100BASE-TX half duplex, 10BASE-T full duplex, 10BASE-T half duplex, and when a link partner is detected, the best mutual protocol is selected and the link is established (depending on its subtleties).

Accordingly, its not the media that is detected (e.g. by a mechanical switch in the jack) but the link partner.

FLPs are defined in IEEE 802.3 Clause 28, the actual link process is detailed in each physical-layer variant, e.g. 1000BASE-T in Clause 40.5. You can download the IEEE 802.3 specifications free after registration.

Be informed when a network cable is plugged in / Wifi connected

You can use the GetIstNetworkAvailable-method to check if there is a network connection at all. Whenever it changes the NetworkAvailabilityChanged event get's fired. For more information see this question.

If you want to track the availability of different NetworkInterfaces here is an sample on how to do this.

Detect when ethernet cable is plugged

This links shows how to do it with Powershell, but one of the cases uses WMI.

http://www.powershellmagazine.com/2013/04/04/pstip-detecting-wi-fi-adapters/

And this links shows a interesting property that can help sometimes:

https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx

GatewayCostMetric

Data type: uint16 array

Access type: Read-only Array

of integer cost metric values (ranging from 1 to 9999) to be used in
calculating the fastest, most reliable, or least resource-intensive
routes. This argument has a one-to-one correspondence with the
DefaultIPGateway property.

How to get current connection state ? (network cable plugged or not ?)

Take a look at the code on MSDN here, http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.aspx. It show how to query the network adapters to determine their state.

public class NetworkingExample
{
public static void Main()
{
NetworkChange.NetworkAddressChanged += new
NetworkAddressChangedEventHandler(AddressChangedCallback);
Console.WriteLine("Listening for address changes. Press any key to exit.");
Console.ReadLine();
}
static void AddressChangedCallback(object sender, EventArgs e)
{

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInterface n in adapters)
{
Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus);
}
}
}

Detect network cable unplugged errors with epoll

You have two options here:

  • Implement application-layer heartbeats. In other words, you write code that detects idle connections and periodically sends an application-layer message to essentially probe the connection and make sure that it's still open
  • You use TCP keepalive. This basically shifts the work of implementing and dealing with heartbeat messages down to the TCP layer. This seems like a good choice in your case.

To enable and configure TCP keepalive, you need to change the client_fd socket options with setsockopts(2). There are 3 parameters you need to check / change:

  • TCP_KEEPCNT - this is the number of outstanding, unanswered probes that are allowed at any given time. If more than TCP_KEEPCNT probes are sent with no reply within a given time interval, the connection is assumed to be dead.
  • TCP_KEEPIDLE - the amount of time the connection needs to be idle before probing packets start being sent.
  • TCP_KEEPINTVL - the time between individual probes.

So, you do something like this on client_fd:

int tcp_keepcnt = 3;
int tcp_keepidle = 30;
int tcp_keepintvl = 60;

setsockopt(client_fd, IPPROTO_TCP, TCP_KEEPCNT, &tcp_keepcnt, sizeof(tcp_keepcnt));
setsockopt(client_fd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle));
setsockopt(client_fd, IPPROTO_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl));

Broken connections are reported as readable by epoll(7), with the EPOLLHUP flag. Note that an order shutdown will be reported as readable without EPOLLHUP, but instead read(2) will return 0.

Keep in mind that detecting a dead connection is not immediate. It will take a while. For example, with the above parameters, it will take roughly 3 minutes.



Related Topics



Leave a reply



Submit