How to find out if the network cable is disconnected?

I have a C # application that should only be used when the network is disconnected, but I am afraid that users will simply disconnect the network cable to use it.

Is there a way to determine if the network cable is disconnected?

thanks

+6
c # networking
source share
9 answers

You can use IsNetworkAlive (). Although technically it does not check the status of the link, it is probably better since it can also detect wireless and dial-up connections. Here is an example:

using System.Runtime.InteropServices; class Program { [DllImport("sensapi.dll")] static extern bool IsNetworkAlive(out int flags); static void Main(string[] args) { int flags; bool connected = IsNetworkAlive(out flags); } } 

The flags parameter returns whether the connection to the Internet or only to the local network. I'm not 100% sure how he knows, but I would argue that he is just looking to see if there is a set of default gateways.

+5
source share

In my humble opinion, there is no definite way to distinguish between the network down and the disconnected cable. And even if there is a way, there is a way around it.

Suppose you have a solution and look at some situations:

  • There is no network traffic, the cable is not disconnected from the computer: it can be disconnected at the other end.
  • There is no network traffic, the cable is disconnected: but this has always been the case, the laptop is connected via Wi-Fi, which is currently not working.
  • There are several network interfaces, only one connected to the WAN is disconnected: should your application work?
  • The network does not actually work, in the sense that you mean: someone managed to reboot the router continuously to use your application.
+4
source share

You can use this

 System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() 
+3
source share

Some network drivers may detect this. However, you need to use unmanaged code to access them from C # (which can be very difficult / impossible), and the solution may be unreliable for all network adapters.

+2
source share

The network card will report this as a condition. Tools like ethtool can display this ( Link up ), but it is only available for Linux / Unix.

If you can list the installed network cards using the Windows API, I'm sure you will find a flag for "link up" somewhere there.

+1
source share

You can register a delegate in the NetworkChange class. When a network change occurs, it does not actually notify you of what happened, so you can list all network interfaces (using NetworkInterface ), filter out those that interest you (by checking their properties) and check their working status.

+1
source share

If I really wanted to use your application and whether it will work, it depends on something like that, I can always find a way to trick your application. Are you sure there is no better solution?

0
source share

How about sending a standard gateway?

There is code here that gets the default gateway from the registry.

0
source share

To detect β€œis the network cable connected to the machine?”, The code snippet below works.

 class Program { private static void Main(string[] args) { bool isNetworkCableConnected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); } } 
0
source share

All Articles