首页 > 解决方案 > 在静态 IP 上连续 Ping,如果返回 false,则发送电子邮件

问题描述

我陷入了混乱,所以敲了社区的门。我需要制作一个程序,通过它我需要static IP不断地点击特定地址(业务需要)。如果任何时候特定 IP 地址没有回复,我需要向服务器关闭的上级部门发送紧急电子邮件。

我需要问一些问题,哪一个适合这个东西?

我尝试使用 for 循环,但遇到了一些问题,当我运行下面的代码时,问题是returning false like after every 25 requests什么return false 1 time

public static bool PingHost(string nameOrAddress)
    {
        bool pingable = false;
        Ping pinger = null;

        try
        {
            pinger = new Ping();
            PingReply reply = pinger.Send(nameOrAddress);
            pingable = reply.Status == IPStatus.Success;
        }
        catch (PingException)
        {
           // send email something went wrong please check 
        }
        finally
        {
            if (pinger != null)
            {
                pinger.Dispose();
            }
        }

        return pingable;
    }
    static void Main(string[] args)
    {
        int count; ;
        string address = "ip-adress";
        for(count = 1; count >0; count++ )
        {
            bool response = PingHost(address);
            if (response)
            {
                Console.WriteLine("Ping successfull  " + address);
            }
            else
            {
              // send email that server is down. 
            }
        }


        Console.ReadLine();
    }

标签: c#ip-addressping

解决方案


推荐阅读