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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/347575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:02:25  来源:igfitidea点击:

How to check if a computer is responding from C#

c#networkingnetbios

提问by ripper234

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.

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

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

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

采纳答案by Mehrdad Afshari

Easy! Use System.Net.NetworkInformationnamespace's ping facility!

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

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

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

回答by gimel

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

要检查myPort已知服务器上的特定 TCP 端口 ( ),请使用以下代码段。您可以捕获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);

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

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

回答by SanBen

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

只要您想检查自己子网内的计算机,就可以使用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
    }

See Pinvoke.netfor more information.

有关更多信息,请参阅Pinvoke.net