如何从 C# 检查计算机是否响应

发布于 2024-07-10 11:24:33 字数 160 浏览 9 评论 0原文

检查计算机是否处于活动状态并响应(例如在 ping/NetBios 中)的最简单方法是什么? 我想要一种可以限制时间的确定性方法。

一种解决方案是在单独的线程中简单地访问共享 (File.GetDirectories(@"\compname")),如果花费太长时间,则终止该线程。

What is the easiest way to check if a computer is alive and responding (say in ping/NetBios)?
I'd like a deterministic method that I can time-limit.

One solution is simple access the share (File.GetDirectories(@"\compname")) in a separate thread, and kill the thread if it takes too long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷路的信 2024-07-17 11:24:33

简单的! 使用 System.Net.NetworkInformation 命名空间的 ping 工具!


http://msdn.microsoft.com /en-us/library/system.net.networkinformation.ping.aspx

Easy! Use System.Net.NetworkInformation namespace's ping facility!


http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

救星 2024-07-17 11:24:33

要检查已知服务器上的特定 TCP 端口 (myPort),请使用以下代码片段。 您可以捕获 System.Net.Sockets.SocketException 异常来指示不可用的端口。

using System.Net;
using System.Net.Sockets;
...

IPHostEntry myHostEntry = Dns.GetHostByName("myserver");
IPEndPoint host = new IPEndPoint(myHostEntry.AddressList[0], myPort);

Socket s = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);
s.Connect(host);

此外,专门的检查可以尝试套接字上超时的 IO。

To check a specific TCP port (myPort) on a known server, use the following snippet. You can catch the System.Net.Sockets.SocketException exception to indicate non available port.

using System.Net;
using System.Net.Sockets;
...

IPHostEntry myHostEntry = Dns.GetHostByName("myserver");
IPEndPoint host = new IPEndPoint(myHostEntry.AddressList[0], myPort);

Socket s = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);
s.Connect(host);

Further, specialized, checks can try IO with timeouts on the socket.

云朵有点甜 2024-07-17 11:24:33

只要您想检查自己子网内的计算机,您就可以使用 ARP 进行检查。 下面是一个示例:

    //for sending an arp request (see pinvoke.net)
    [DllImport("iphlpapi.dll", ExactSpelling = true)]
    public static extern int SendARP(
                                        int DestIP, 
                                        int SrcIP, 
                                        byte[] pMacAddr, 
                                        ref uint PhyAddrLen);


    public bool IsComputerAlive(IPAddress host)
    {
        //can't check the own machine (assume it's alive)
        if (host.Equals(IPAddress.Loopback))
            return true;

        //Prepare the magic

        //this is only needed to pass a valid parameter
        byte[] macAddr = new byte[6];
        uint macAddrLen = (uint)macAddr.Length;

        //Let's check if it is alive by sending an arp request
        if (SendARP((int)host.Address, 0, macAddr, ref macAddrLen) == 0)
            return true; //Igor it's alive!

        return false;//Not alive
    }

有关详细信息,请参阅 Pinvoke.net

As long as you want to check a computer within the own subnet you could check it using ARP. Here's an example:

    //for sending an arp request (see pinvoke.net)
    [DllImport("iphlpapi.dll", ExactSpelling = true)]
    public static extern int SendARP(
                                        int DestIP, 
                                        int SrcIP, 
                                        byte[] pMacAddr, 
                                        ref uint PhyAddrLen);


    public bool IsComputerAlive(IPAddress host)
    {
        //can't check the own machine (assume it's alive)
        if (host.Equals(IPAddress.Loopback))
            return true;

        //Prepare the magic

        //this is only needed to pass a valid parameter
        byte[] macAddr = new byte[6];
        uint macAddrLen = (uint)macAddr.Length;

        //Let's check if it is alive by sending an arp request
        if (SendARP((int)host.Address, 0, macAddr, ref macAddrLen) == 0)
            return true; //Igor it's alive!

        return false;//Not alive
    }

See Pinvoke.net for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文