IP 和 Ping Class .Net - 在开发中而不是在生产中?

IP and Ping Class .Net - Works in Development not in Production?

所以我实现了 Ping Class including the most comprehensive one I found thanks to Vinjay B R 的各种方法。但是,我似乎无法获得任何可用于生产的解决方案。我可以让它在 VS 2015 中运行,但这并没有说明什么,因为我认为 PING 甚至不会离开我的网络(我绝对不是网络专家,所以这是一个假设)。我还必须实施下面的 Split(":") 方法,只采用数组元素 0,因为如果我不这样做,IPAddress class 会在核心 IPAddress 的尾端捕获一个带有“:4321”的 IP 地址.我已经尝试了完整地址和截断地址,但两种方法都不起作用。我已经使用带有截断地址的 cmd.exe Ping 命令来检查两者,我只能用截断的地址完成 ping,所以我让 Split() 发挥作用。我不知所措,希望有人看到一头大象。如果有帮助,我的生产服务器是 Azure?

目前正在抛出 PingException 捕获。

 public class PingTest
{

    public void Pinger()
    {

        SpeedTest Test = new SpeedTest();

        IPAddress ip = new IPAddress();

        var clientIP = ip.GetIPAddress();

        string[] IPAddresses = clientIP.Split(':');

        string address = IPAddresses[0];

        //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];


        //here we will ping the host 4 times (standard)
        for (int i = 0; i < 4; 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)
                    {
                        case IPStatus.Success:
                            Test.Address = pingReply.Address.ToString();
                            Test.ResponseTime = pingReply.RoundtripTime.ToString() + " ms";
                            Test.Status = pingReply.Status.ToString();
                            Test.UserId = User.Identity.GetUserId();
                            Test.TestDate = DateTime.Now;
                            Test.TestType = "Ping";
                            break;
                        case IPStatus.TimedOut:
                            Test.Address = pingReply.Address.ToString();
                            Test.ResponseTime = "";
                            Test.Status = "Connection has timed out...";
                            Test.UserId = User.Identity.GetUserId();
                            Test.TestDate = DateTime.Now;
                            Test.TestType = "Ping";
                            break;
                        default:
                            Test.Address = pingReply.Address.ToString();
                            Test.ResponseTime = "";
                            Test.Status = pingReply.Status.ToString();
                            Test.UserId = User.Identity.GetUserId();
                            Test.TestDate = DateTime.Now;
                            Test.TestType = "Ping";
                            break;
                    }
                }
                else
                {
                    Test.Address = pingReply.Address.ToString();
                    Test.ResponseTime = "";
                    Test.Status = "Connection failed for an unknown reason...";
                    Test.UserId = User.Identity.GetUserId();
                    Test.TestDate = DateTime.Now;
                    Test.TestType = "Ping";
                }
            }
            catch (PingException ex)
            {
                Test.Address = address;
                Test.ResponseTime = "";
                Test.Status = string.Format("Connection Error: {0}", ex.Message);
                Test.UserId = User.Identity.GetUserId();
                Test.TestDate = DateTime.Now;
                Test.TestType = "Ping";

            }
            catch (SocketException ex)
            {
                Test.Address = address;
                Test.ResponseTime = "";
                Test.Status = string.Format("Connection Error: {0}", ex.Message);
                Test.UserId = User.Identity.GetUserId();
                Test.TestDate = DateTime.Now;
                Test.TestType = "Ping";
            }
        }
        using (ApplicationDbContext db = new ApplicationDbContext())
        {

            db.SpeedTest.Add(Test);
            db.SaveChanges();

        }
    }
}

获取 IP 地址:

public class IPAddress
{
    public string GetIPAddress()
    {
        //The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a 
        //client connecting to a web server through an HTTP proxy or load balancer
        String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (string.IsNullOrEmpty(ip))
        {
            ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        return ip;
    }
}

the IPAddress class captures an IP address with ":4321" on the tail end

:4321 部分是端口号。它不应该在您调用 ping.Send() 时提供。

如果您查看 MSDN 上的 documentation of Ping,您会看到该短语的自由使用

Attempts to send an Internet Control Message Protocol (ICMP) echo message

尝试次数 是关键词。许多服务器配置为不响应 ping 请求,许多防火墙阻止此类请求。

您的代码在本地有效。如果您的服务器 IP 地址正确,则代码应该是正确的。失败的最可能原因是网络基础结构或服务器阻止请求完成。

要测试假设,请在客户端计算机上打开命令提示符并输入

ping 1.2.3.4

将1.2.3.4替换为实际IP地址。如果您没有收到回复,则表明 ping 已被阻止,您的代码也将无法完成 ping。

My production server is Azure if that helps?

是的,确实如此。

The ICMP protocol is not permitted through the Azure load balancer.

http://blogs.msdn.com/b/mast/archive/2014/06/22/use-port-pings-instead-of-icmp-to-test-azure-vm-connectivity.aspx

然而,并非一无所有。您可以使用 PsPing 等工具来测试服务器上某个端口(例如 HTTP 端口 80)的可用性。请注意,这篇文章讨论了将 您的 Azure 服务器 连接到 互联网。但是,这在任一方向都有效。