The fastest way to check whether remote computer is offline

1.9k views Asked by At

I need a fastest way to check whether remote computer is off. There are some ways I've tried, but none of them gives me desired time speed (it should be less than 0.5 sec and it's critical, because occurs on context menu popup)

            //using System.Net
            IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
            //if error occurs - remote computer is off

            //using System.Net.NetworkInformation
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(hostIp);
            if (pingReply.Status != IPStatus.Success)
            {
                //Remote computer is off
            }

            //using System.Management
            ManagementScope scope = new ManagementScope(@"\\" + hostName + @"\root\cimv2", null);
            scope.Connect();
            //if error occurs - remote computer is off

As far as I can see the fastest way is by using Dns.GetHostEntry(hostName) - but still it takes it about 3 sec to throw an exception. Any idea?

3

There are 3 answers

2
Orel Eraki On

Because you're depending on so many factors (Network loads, Packet losses etc`), and additionally, when a computer is not responding immediately when he gets the request doesn't necessarily says it's online, you can't assume the computer is offline. I don't think you can do it any faster than a ping request.

2
Wapac On

Use Ping with timeout. This version of Send method will allow you to specify how long to wait for a response. If you define 500ms and the server you are checking is not on the other side of the planet or on the very high latency Internet connection, you can quite reliably say that either the target machine is down or that you have problems with your Internet connection.

Dns.GetHostEntry does not check whether the host is up or down. This works with DNS and only tries to resolve host name. DNS can be slow and can timeout even if the host name exists and just the used DNS server is not responsive.

2
LLaus On

Given the fact that it is just for one computer, why don't you have the logic on your answer running on a separate thread and caching the state of the computer? Your context popup would be reading from this cache and reacting "instantaneously", but the exact state could be a few seconds delayed... Now, depending on what you are doing with the computer afterwards (you did not mention in the answer), this might be an approach to consider...