简体   繁体   English

如何判断网络电缆是否已拔下?

[英]How can I tell if a network cable has been unplugged?

I have a C# application that should only be used when the network is down, but am afraid users will just unplug the network cable in order to use it. 我有一个C#应用程序,只应在网络关闭时使用,但我担心用户只需拔掉网线即可使用它。

Is there a way to detect if the network cable has been unplugged? 有没有办法检测网络电缆是否已拔下?

Thanks 谢谢

You could use IsNetworkAlive(). 您可以使用IsNetworkAlive()。 Although technically it doesn't check link state, it's probably better since it can detect wireless and dialup connectivity as well. 虽然从技术上讲它不会检查链路状态,但它可能更好,因为它也可以检测无线和拨号连接。 Here's 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 param returns whether the connection is to the internet or just a LAN. flags param返回连接是互联网还是LAN。 I'm not 100% sure how it knows, but I'd bet it just looks to see if there is a default gateway set. 我不是100%确定它是如何知道的,但我敢打赌它只是看看是否有默认的网关设置。

In my humble opinion, there is no certain way to distinguish between a network down and an unplugged cable. 在我看来,没有一种方法可以区分网络连接和不插电缆。 And even if there is a way, there is also a way to work around it. 即使有办法,也有办法解决它。

Let's assume that you have a solution and let's look at some situations: 让我们假设您有一个解决方案,让我们看看一些情况:

  • There is no network traffic, the cable is not unplugged from the computer: it may be unplugged at the other end. 没有网络流量,电缆未从计算机上拔下:它可能在另一端拔掉。
  • There is no network traffic, the cable is unplugged: but this has always been the case, the laptop is connected via Wi-Fi, which is down at the moment. 没有网络流量,电缆已拔下:但情况一直如此,笔记本电脑通过Wi-Fi连接,目前已关闭。
  • There are several network interfaces, only the one connected to WAN is down: should your app work? 有几个网络接口,只有连接到WAN的网络接口关闭:您的应用程序应该工作吗?
  • The network is actually down, in the sense you mean: someone has managed to reboot the router continuously for using your app. 从某种意义上说,网络实际上是关闭的:有人设法连续重启路由器以使用您的应用程序。

你可以用它

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

Some network drivers are able to detect this. 一些网络驱动程序能够检测到这一点。 However you'd need to use unmanaged code to access them from C# (which may be very difficult/impossible) and the solution may not be reliable for all network adapters. 但是,您需要使用非托管代码从C#访问它们(这可能非常困难/不可能),并且该解决方案可能对所有网络适配器都不可靠。

You could register a delegate to the NetworkChange Class. 您可以将委托注册到NetworkChange类。 When a network change occurs, it doesn't actually notify you what happened, so you could list all the network interfaces (Using NetworkInterface ), filter the ones that concern you (By checking there properties) and check their operational status. 当发生网络更改时,它实际上不会通知您发生了什么,因此您可以列出所有网络接口(使用NetworkInterface ),过滤与您有关的接口(通过检查其属性)并检查其运行状态。

The network card will report this as a state. 网卡会将此状态报告为状态。 Tools like ethtool can display this ( Link up ), but that is only available for Linux/Unix. ethtool这样的工具可以显示这个( Link up ),但这只适用于Linux / Unix。

If you can enumerate the installed network cards with a Windows API, I'm sure you'll find the flag for "link up" somewhere in there. 如果您可以使用Windows API枚举已安装的网卡,我相信您会在那里的某处找到“链接”的标记。

To detect 'Is network cable plugged in a machine?', below piece of code works. 要检测“网络电缆是否插入机器?”,下面的代码可以正常工作。

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

If I really wanted to use your application and whether it will work depends on something like this, I would always be able to find a way to trick your application. 如果我真的想要使用你的应用程序,它是否会起作用取决于这样的东西,我总能找到一种方法来欺骗你的应用程序。 Are you sure there's no better solution? 你确定没有更好的解决方案吗?

How about pinging the default gateway? ping默认网关怎么样?

There is some code here that gets the default gateway from the registry. 有一些代码, 这里是会从注册表中的默认网关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM