【问题标题】:Invalid IP Address exceptionIP 地址无效异常
【发布时间】:2013-11-16 19:09:20
【问题描述】:

我正在开发一个小型 C# 项目,但在将字符串转换为 IPAddress 时遇到了一些麻烦。这是代码:

Ping pingeage = new Ping();
String ip = tabtempsoctets1[0] 
    + "." + tabtempsoctets1[1] 
    + "." + tabtempsoctets1[2] 
    + "." + tabtempsoctets1[3];
MessageBox.Show(ip);
IPAddress adresseTest = IPAddress.Parse(ip);
boxLogs.Text = adresseTest.ToString();
PingReply reponse = pingeage.Send(adresseTest,2000);

但 VisualStudio 引发异常,告诉我我的 IpAddress 不是 IPAddress。 为什么? tabtempoctets1 是一个字符串数组,我手动添加了"." 这里有什么问题?

【问题讨论】:

  • 你的字符串ip的值是多少
  • 第一个猜测是其中一个条目不在 0-255 之间。
  • 消息框显示为“127.1.1.1”

标签: c# visual-studio-2010 ip-address ping


【解决方案1】:

正确设置字符串string[] tabtempsoctets1 = { "127", "1", "1", "1" }; 上面的代码似乎对我有用。

您可以尝试转换为字节数组来检查范围。

这是我在 Win7 PC 上成功使用的测试应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace IPAddressTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] tabtempsoctets1 = { "127", "1", "1", "1" };
        Ping pingeage = new Ping();
        //Ping pingeage = new Ping();
        String ip = tabtempsoctets1[0]
            + "." + tabtempsoctets1[1]
            + "." + tabtempsoctets1[2]
            + "." + tabtempsoctets1[3];
        Console.WriteLine(ip);
        IPAddress adresseTest = IPAddress.Parse(ip);
        Console.WriteLine(adresseTest.ToString());

        byte [] addressAsBytes = new byte[tabtempsoctets1.Length];
        for (int i = 0; i < tabtempsoctets1.Length; i++)
        {
            if (!byte.TryParse(tabtempsoctets1[i], out addressAsBytes[i]))
                Console.WriteLine(tabtempsoctets1[i] + " is not formated correctely");
        }
        IPAddress adresseTest2 = new IPAddress(addressAsBytes);
        Console.WriteLine(adresseTest2.ToString());
        PingReply reponse = pingeage.Send(adresseTest2, 2000);
        Console.WriteLine(reponse.Status.ToString());

        Console.ReadKey();
    }
}

}enter code here

【讨论】:

    【解决方案2】:

    您可能有前导或尾随空格。否则它应该成功解析"127.1.1.1"尝试:

    IPAddress adresseTest = IPAddress.Parse(ip.Trim());
    

    您也可以尝试IPAddress.TryParse,它不会在解析失败时引发异常。喜欢:

    string str = "     127.1.1.1       ";
    IPAddress a;
    if (IPAddress.TryParse(str.Trim(), out a))
    {
        //parsing succesful
    }
    else
    {
        //invalid string
    }
    

    您也可以使用string.Join 来连接您的字符串,例如:

    string ip = string.Join(".", tabtempsoctets1);
    

    【讨论】:

    • 就是这样!谢谢 !完美运行
    • @user2564494,很高兴
    • @user2564494,如果你使用string ip = string.Join(".", tabtempsoctets1);作为ip字符串也更好,更少的代码行:)
    【解决方案3】:
            string[] tabtempsoctets1 = new string[] { "127", "1", "1", "1" }; 
    
            Ping pingeage = new Ping();
            String ip = tabtempsoctets1[0]
                + "." + tabtempsoctets1[1]
                + "." + tabtempsoctets1[2]
                + "." + tabtempsoctets1[3];
            MessageBox.Show(ip);
            IPAddress adresseTest = IPAddress.Parse(ip);
           // boxLogs.Text = adresseTest.ToString();
            PingReply reponse = pingeage.Send(adresseTest, 2000);
    
             works for me
    

    【讨论】:

      猜你喜欢
      • 2014-08-15
      • 2016-12-01
      • 2012-06-05
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多