【问题标题】:How to pinging domain in every few seconds/minutes using C#.net?如何使用 C#.net 每隔几秒/分钟 ping 一次域?
【发布时间】:2011-06-28 04:41:13
【问题描述】:

我打算开发一个网站,每隔一段时间就 ping 域以检查域状态。参考站点:http://pingdom.com/ 这个站点也喜欢这样做。如何使用 C# 获取 Ping 统计信息?

【问题讨论】:

    标签: c# asp.net dns httpresponse ping


    【解决方案1】:

    使用Ping class

            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();
    
            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;
    
            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
    

    您可以在 Global.asax 中启动计时器,并使用 Ping 类在每个时间间隔内 ping 您的域

    【讨论】:

    • 它不是 Global.aspx 而是 Global.asax
    • 糟糕,已修复 @Shekhar_Pro,谢谢
    • 谢谢@Shekhar_Pro,你也+1 ;)
    • 你是在做工作还是做个人生意?
    • @ArsenMkrt 你的工作在哪里?很高兴认识你。
    【解决方案2】:

    您可以使用PING 类。您可以使用发送方法进行 ping 操作并接收回显。

            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();
    
            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;
    
            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
    

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多