【问题标题】:Convert host name to IP address then ping将主机名转换为 IP 地址,然后 ping
【发布时间】:2012-04-17 21:57:00
【问题描述】:

旁注:我真的想不出一个好的标题。

好的,我正在更新一个由不相信代码重用的开发人员编写的程序。所以我的任务是更新并使其对未来的变化“友好”。

编辑 我应该说:最终用户想要一个命令提示符来查看每个 ping。我知道使用内置 ping 会是一个更好的选择,但这正是他们想要的……你们知道 /End Edit

我正在使用一个在整个程序中实现了几次的类。 (以前有两个类基本上做同样的事情)

我决定制作一个接口并在类上实现它。

(我还没有失去你吗)

该方法实际上只是将主机名(方法参数之一)转换为 IP 地址,并生成一个命令提示符来 ping 它。我还有一个单独的类,它实现了接口并调用了方法,但是没有传递一个字符串(主机名),它传递了一个 ipaddress 对象。

我的问题在这里:处理可以更改类型的参数时,最佳做法是什么?有人会看看我做了什么吗?它符合要求,我只是想确保它是最佳实践。

// Interface that is implement twice in the program 
interface Icmd_Ping
    {
    void initilize_Proc(object target, string last_Octet);

    }


 class process_spawn : Icmd_Ping
{     
    writeLog_Delegate writeLog = new writeLog_Delegate(error_Log.write_log);

    private string ipaddress;
    public object Ipaddress
        {
        get { return ipaddress; }
        set
            {                
            IPAddress ip = value as IPAddress;
            if (ip != null)
                ipaddress = ip.ToString();
            else
                {

                try
                    {
                    formatIP format = new formatIP();
                    ipaddress = format.convert_Ip(((string)value));
                    }
                catch (Exception ex)
                    {
                    writeLog(ex);
                    }
                }   
            }
        }

    public void initilize_Proc(object target, string last_Octet = null)
        {
        if (target == null)
            throw new ArgumentNullException();

        formatIP format_IP = new formatIP();
        this.Ipaddress = target;

        Process cmd = new Process();
        ProcessStartInfo psi = new ProcessStartInfo {FileName = "cmd", UseShellExecute = false, RedirectStandardOutput = false };
        cmd.StartInfo = psi;

        if (last_Octet != string.Empty)
            psi.Arguments = string.Format("/c ping {0} -t", format_IP.format_Ip(((string)Ipaddress), last_Octet));
        if (last_Octet == string.Empty)
            psi.Arguments = string.Format("/c ping {0} -t", ((string)Ipaddress));


        cmd.Start();
        }



}

我应该在 ipaddress 属性中转换 IPaddress 吗?或者只是创建一个新方法并调用它?

抱歉,我知道它在这一点上不是很优雅,我只是想在我真正深入研究之前确保我处于写入方向。

感谢大家的帮助。

【问题讨论】:

  • 您是否关心this.Ipaddress = target 并知道如何处理不同的类型?你愿意为了Ping 放弃这整个shebang 吗?
  • 不,最终用户需要命令提示符。我试过相信我......是的,我关心的是让不同类型的人都安全。

标签: c# properties ip-address


【解决方案1】:

首先,我建议使用System.Net.IPAddress 而不是自己滚动。 http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx

其次,您可以使用System.Net.NetworkInformation.Ping 进行 ICMP-Echo http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

例如

        IPAddress ip = IPAddress.Parse("192.168.0.1");
        Ping ping = new Ping();

        for (int i = 0; i < 4; ++i)
        {
            var reply = ping.Send(ip);
            Console.WriteLine("Reply from {0} Status: {1} time:{2}ms", 
                              reply.Address, 
                              reply.Status, 
                              reply.RoundtripTime);
        }

【讨论】:

    【解决方案2】:

    您可以使用 .NET 函数 Dns.GetHostAddresses() 将 DNS 条目解析为单个主机地址(我认为这也是您的要求之一)。

    foreach (IPAddress address in Dns.GetHostAddresses(hostName))
    {
       PingReply reply = Ping.Send(address);
       ...
    }
    

    从那里,将回复格式化回类似于 ping 工具的格式。对于 TTL 和字节发送参数也可以通过 .NET 中的 Ping.Send() API 进行控制。

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2014-04-12
      • 2011-09-15
      • 2019-05-26
      • 2014-11-12
      • 2011-03-12
      • 1970-01-01
      • 2014-12-19
      • 2014-12-02
      相关资源
      最近更新 更多