How to check if a computer responds to C #

What is the easiest way to check if a computer is alive and respond (say in ping / NetBios)? I need a deterministic method that I can limit.

One solution is to simply access the resource (File.GetDirectories (@ "\ compname")) in a separate stream and destroy the stream if it takes too long.

+5
source share
3 answers

Easily! Use the System.Net.NetworkInformationnamespace name checker!

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

+10
source

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 .

+3

As long as you want to check the computer on your own subnet, you can check it using ARP . Here is 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 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 check if it is alive by sending an arp request
        if (SendARP((int)host.Address, 0, macAddr, ref macAddrLen) == 0)
            return true; //Igor it alive!

        return false;//Not alive
    }

See Pinvoke.net for more details .

+1
source

All Articles