示例#1
0
        } // Ping and get CPU loop reads hosts.txt and localhosts.txt

        public static String PingHost(string host)
        {
            //Returned Message
            String Message = "Host is online \n";

            //IPAddress instance for holding the returned host
            IPAddress address = GetIpFromHost(ref host);

            //set the ping options, TTL 128
            PingOptions pingOptions = new PingOptions(128, true);

            //create a new ping instance
            Ping ping = new Ping();

            //32 byte buffer (create empty)
            byte[] buffer = new byte[32];

            // amount of times the host is pinged
            int pings = 4;

            //first make sure we actually have an internet connection
            if (HasConnection())
            {
                errorCount   = 0;
                avgReplyTime = 0;
                //here we will ping the host 4 times (standard)
                for (int i = 0; i < pings; i++)
                {
                    try
                    {
                        //send the ping 4 times to the host and record the returned data.
                        //The Send() method expects 4 items:
                        //1) The IPAddress we are pinging
                        //2) The timeout value
                        //3) A buffer (our byte array)
                        //4) PingOptions

                        PingReply pingReply = ping.Send(address, 1000, buffer, pingOptions);

                        //make sure we dont have a null reply
                        if ((pingReply != null))
                        {
                            switch (pingReply.Status)
                            {
                            // Ping reply
                            case IPStatus.Success:
                                //Output.LogAppend(string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3} \n", pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl), Color.White);
                                avgReplyTime += pingReply.RoundtripTime;
                                break;

                            // Ping timed out
                            case IPStatus.TimedOut:
                                Message = string.Format("Error: Connection to the host: {0} [{1}] has timed out...", pingReply.Address, host);
                                //Output.WriteLine(Message, Color.OrangeRed);
                                errorCount++;
                                break;

                            // Ping failed
                            default:
                                Message = string.Format("Error: Ping failed: {0}", pingReply.Status.ToString());
                                //Output.WriteLine(Message, Color.OrangeRed);
                                errorCount++;
                                break;
                            }
                        }
                        else
                        {
                            Message = "Error: Connection failed...";
                            errorCount++;
                        }
                    }
                    catch (ArgumentNullException) // IP address was not set from hostname
                    {
                        Message = "Error: Remote host was not found";
                        errorCount++;
                    }
                    catch (NullReferenceException) // No host exists
                    {
                        Message = "Error: Remote host was not found";
                        errorCount++;
                    }
                    catch (Exception ex) // Any other unforseen scenarios
                    {
                        Message = "Unexpected error: " + ex.Message;
                        errorCount++;
                    }
                }
            }
            else
            {
                Message = "Error: No Internet connection found..."; // HasConnection() returned false
            }

            avgReplyTime = avgReplyTime / pings; // Calculate average ping time in miliseconds
            Output.WriteLine("Average response time: " + avgReplyTime + "ms", Color.LightBlue);

            if (Message.Contains("Error"))                  // Check if Message has error to determine text colour
            {
                Output.WriteLine(Message, Color.OrangeRed); // Red text error
            }
            else
            {
                Output.WriteLine(Message, Color.LightGreen); // Green text success
            }

            return(Message);
        }