C#ping程序给出了奇怪的输出

时间:2012-02-07 09:40:11

标签: c# ping

我有一个程序,将来会在一定的时间间隔内自动ping我公司的重要机器。它从数据库获取pingable地址,为了测试目的,现在只有3个地址:localhost 127.0.0.1,facebook 66.220.153.15和google 173.194.67.104。

问题在于,当我连接到互联网时,程序无法工作并提供神秘输出。

未连接到互联网时的输出

127.0.0.1 ping status: Success
173.194.67.104 ping status: HardwareError
66.220.153.15 ping status: HardwareError
Error IP 66.220.153.15
Address: 127.0.0.1 RTT: 0 TTTL: 128 Don't fragment: True Buffer size: 32
Error IP 173.194.67.104

和连接时

120.1.21.0 ping status: 2406264
Error IP 120.1.21.0
127.0.0.1 ping status: Success
120.1.21.0 ping status: 2406264
Error IP 120.1.21.0
Address: 127.0.0.1 RTT: 0 TTTL: 128 Don't fragment: True Buffer size: 32

我不知道奇怪的IP和状态来自哪里。

非常感谢任何帮助!

我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Diagnostics;

static int Main(string[] args)
{
        testidbDataSetTableAdapters.testitauluTableAdapter adapter = new testidbDataSetTableAdapters.testitauluTableAdapter();
        testidbDataSet.testitauluDataTable table = null;
        table = adapter.GetData();

        if (table != null)
        {
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Pinger pinger = new Pinger(table[i].id, table[i].ip);
                    new Thread(new ThreadStart(pinger.Pingaa)).Start();
                }
                Thread.Sleep(2500);
            }
        }
        return 0;
    }

    public class Pinger
    {
        int x;
        string Ip;
        public Pinger(int x, string Ip)
        {
            this.x = x;
            this.Ip = Ip;
        }

        public void Pingaa()
        {
            AutoResetEvent waiter = new AutoResetEvent(false);
            Ping pingSender = new Ping();
            PingOptions pingOptions = new PingOptions(64,true);
            byte[] bytes = new byte[32];
            pingOptions.DontFragment = true;
            pingOptions.Ttl = 15;
            int timeOut = 2000;
            pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_PingCompleted);
            pingSender.SendAsync(Ip, timeOut, bytes, pingOptions, waiter);
        }
        private static void pingSender_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");
                ((AutoResetEvent)e.UserState).Set();
            }
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed");
                Console.WriteLine(e.Error.ToString());
                ((AutoResetEvent)e.UserState).Set();
            }
            PingReply reply = e.Reply;
            DisplayReply(reply);
            ((AutoResetEvent)e.UserState).Set();
        }

        private static void DisplayReply(PingReply reply)
        {
            if (reply == null)
                return;

            Console.WriteLine("{0} ping status: {1}", reply.Address, reply.Status.ToString());
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address: {0} RTT: {1} TTTL: {2} Don't fragment: {3} Buffer size: {4}", reply.Address, reply.RoundtripTime, reply.Options.Ttl, reply.Options.DontFragment, reply.Buffer.Length);
            }
            else
            {
                Console.WriteLine("Error IP " + reply.Address);

            }
    }

编辑:奇怪的IP始终保持不变,但奇怪的状态总是变为随机(?)7-8位数字。

0 个答案:

没有答案