ip地址增量问题

发布于 2024-09-14 11:53:15 字数 558 浏览 6 评论 0原文

我想增加我的 IP 地址;

这是代码

 ipAddressControl1.Text = "192.168.1.255";

 byte[] ip = ipAddressControl1.GetAddressBytes();
 ip[3] = (byte)(++ip[3]);

 IPAddress ipAddress1 = new IPAddress(ip);
 MessageBox.Show(ipAddress1.ToString());

,或者我也尝试过这个

ipAddressControl3.Text = "192.168.1.255";
 IPAddress ipAddress1 = new IPAddress(ıpAddressControl3.GetAddressBytes());
 ipAddress1.Address += 0x1 << 24;
 MessageBox.Show(ipAddress1.ToString());

,但它们都给了我 192.168.1.0 但我想获得 192.168.2.0 的值

I want to increase my ip address and;

Here is the code

 ipAddressControl1.Text = "192.168.1.255";

 byte[] ip = ipAddressControl1.GetAddressBytes();
 ip[3] = (byte)(++ip[3]);

 IPAddress ipAddress1 = new IPAddress(ip);
 MessageBox.Show(ipAddress1.ToString());

or I also tried this

ipAddressControl3.Text = "192.168.1.255";
 IPAddress ipAddress1 = new IPAddress(ıpAddressControl3.GetAddressBytes());
 ipAddress1.Address += 0x1 << 24;
 MessageBox.Show(ipAddress1.ToString());

but both of them gives me 192.168.1.0 but I want to get value as 192.168.2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

抠脚大汉 2024-09-21 11:53:15

您的问题是,当 ip[3] 环绕时(依此类推),您没有增加 ip[2] 。以下代码应该可以解决问题,最终从 255.255.255.255 包装到 0.0.0.0

byte[] ip = ipAddressControl1.GetAddressBytes();
ip[3] = (byte)(ip[3] + 1);
if (ip[3] == 0) {
    ip[2] = (byte)(ip[2] + 1);
    if (ip[2] == 0) {
        ip[1] = (byte)(ip[1] + 1);
        if (ip[1] == 0) {
            ip[0] = (byte)(ip[0] + 1);
        }
    }
}

以下代码也可能有效:

byte[] ip = ipAddressControl1.GetAddressBytes();
if (++ip[3] == 0)
    if (++ip[2] == 0)
        if (++ip[1] == 0)
            ++ip[0];

Your problem is that you're not increasing ip[2] when ip[3] wraps around (and so on up the hierarchy). The following code should do the trick, finally wrapping from 255.255.255.255 to 0.0.0.0:

byte[] ip = ipAddressControl1.GetAddressBytes();
ip[3] = (byte)(ip[3] + 1);
if (ip[3] == 0) {
    ip[2] = (byte)(ip[2] + 1);
    if (ip[2] == 0) {
        ip[1] = (byte)(ip[1] + 1);
        if (ip[1] == 0) {
            ip[0] = (byte)(ip[0] + 1);
        }
    }
}

The following may also work:

byte[] ip = ipAddressControl1.GetAddressBytes();
if (++ip[3] == 0)
    if (++ip[2] == 0)
        if (++ip[1] == 0)
            ++ip[0];
南城旧梦 2024-09-21 11:53:15

可能值得注意的是,现有答案均不处理 IPv6 地址,IPAddress 类本身确实可以满足需要。为此,您可能想要采用更通用的策略(我不确定 IPv6 的增量规则是什么样的,尽管它们可能完全相同,只是需要更多字节来完成,我怀疑这是案)。

- 编辑:

在此基础上,这似乎有效:

    public static IPAddress Increment (IPAddress address)
    {
        IPAddress result;

        byte[] bytes = address.GetAddressBytes();

        for(int k = bytes.Length - 1; k >= 0; k--){
            if( bytes[k] == byte.MaxValue ){
                bytes[k] = 0;
                continue;
            }

            bytes[k]++;

            result = new IPAddress(bytes);
            return result;
        }

        // Un-incrementable, return the original address.
        return address;
    }

It might be worth noting that none of the existing answers handle IPv6 addresses, which the IPAddress class itself does indeed cater for. For that you'd probably want to adopt a more general strategy (and I'm not sure what the increment rules for IPv6 are like, though they could be exactly the same, just with more bytes to do it over, which I suspect is the case).

-- Edit:

On that basis, this seems to work:

    public static IPAddress Increment (IPAddress address)
    {
        IPAddress result;

        byte[] bytes = address.GetAddressBytes();

        for(int k = bytes.Length - 1; k >= 0; k--){
            if( bytes[k] == byte.MaxValue ){
                bytes[k] = 0;
                continue;
            }

            bytes[k]++;

            result = new IPAddress(bytes);
            return result;
        }

        // Un-incrementable, return the original address.
        return address;
    }
倾城°AllureLove 2024-09-21 11:53:15

在第一个示例中,您仅递增第四个字节序列。所以它将从 255 变为 0,对 byte[2] 没有影响。

在第二个序列中,您将其增加 1,但随后将其从 2 移回到 1。我不确定您为什么选择这样做。

In the first example, you're only incrementing the 4th byte sequence. So it's going to go from 255 to 0 with no effect to byte[2].

In the second sequence, you're incrementing it 1, but then you're shifting it back from 2 to 1. I'm not sure why you chose to do this.

自演自醉 2024-09-21 11:53:15

您需要检查您的地址是否为 254 - 255 且 0 为广播地址。

ipAddressControl1.Text = "192.168.1.255";

byte[] ip = ipAddressControl1.GetAddressBytes();
if (ip[3] != 255)
{
    ip[3] = (byte)(++ip[3]);
}
else
{
    ip[2] = (byte)(++ip[2]);
    ip[3] = (byte)0;
}
IPAddress ipAddress1 = new IPAddress(ip);
MessageBox.Show(ipAddress1.ToString());

但您只能检查 ip[0] 以内的溢出情况 - 如果您达到 255,则需要小心。

You need to check if your address is 254 - 255 and 0 are a broadcast addresses.

ipAddressControl1.Text = "192.168.1.255";

byte[] ip = ipAddressControl1.GetAddressBytes();
if (ip[3] != 255)
{
    ip[3] = (byte)(++ip[3]);
}
else
{
    ip[2] = (byte)(++ip[2]);
    ip[3] = (byte)0;
}
IPAddress ipAddress1 = new IPAddress(ip);
MessageBox.Show(ipAddress1.ToString());

But you can only check for overflows up to ip[0] - you need to take care if you hit 255 there.

沦落红尘 2024-09-21 11:53:15

看起来 IP 地址以“错误的方式”存储在您尝试使用的 .Address 属性中:

192.168.1.255
 c0 a8 01 ff     is stored as   0xff01a8c0

因此添加 1 << 24 只会增加左侧的 0xff,然后将其截断,将其变为 0

如果您希望它按照您描述的方式工作,您必须编写自己的加法函数。

public static IPAddress IncrementIP(IPAddress addr)
{
    byte[] ip = addr.GetAddressBytes();
    ip[3]++;
    if (ip[3] == 0) {
        ip[2]++;
        if (ip[2] == 0) {
            ip[1]++;
            if (ip[1] == 0)
                ip[0]++;
        }
    }
    return new IPAddress(ip);
}

或类似的东西。

Looks like IP addresses are stored the “wrong way around” in the .Address property you tried to use:

192.168.1.255
 c0 a8 01 ff     is stored as   0xff01a8c0

So adding 1 << 24 is only going to increment the 0xff on the left and then truncate it, turning it into 0.

You’ll have to write your own addition function if you want this to work the way you describe.

public static IPAddress IncrementIP(IPAddress addr)
{
    byte[] ip = addr.GetAddressBytes();
    ip[3]++;
    if (ip[3] == 0) {
        ip[2]++;
        if (ip[2] == 0) {
            ip[1]++;
            if (ip[1] == 0)
                ip[0]++;
        }
    }
    return new IPAddress(ip);
}

or something like that.

絕版丫頭 2024-09-21 11:53:15

您可以将 IP 转换为其等价的数字。

检查之前回答的问题以了解详细信息:

最佳 IP 类型-Hibernate 实体中的地址?

public static string GetStandardIP(long numericIP)
    {
        string w = Convert.ToString(Convert.ToInt64(numericIP / 16777216) % 256);
        string x = Convert.ToString(Convert.ToInt64(numericIP / 65536) % 256);
        string y = Convert.ToString(Convert.ToInt64(numericIP / 256) % 256);
        string z = Convert.ToString(Convert.ToInt64(numericIP) % 256);

        return w + "." + x + "." + y + "." + z;
    }

而这个

public static long GetNumericIP(string standardIP)
    {
            if (standardIP != null && standardIP != string.Empty)
            {
                string[] ipParts = standardIP.Split('.');
                long numericIP = 16777216 * Convert.ToInt64(ipParts[0]) + 65536 * Convert.ToInt64(ipParts[1]) + 256 * Convert.ToInt32(ipParts[2]) + Convert.ToInt32(ipParts[3]);

                return numericIP;
            }
            return 0;
    }

您可能想通过检查参数并使用 string.concat 来改进它们

You can convert the IP into its numerical equivalent.

Check this previously answered question for details:

Best type for IP-address in Hibernate Entity?

public static string GetStandardIP(long numericIP)
    {
        string w = Convert.ToString(Convert.ToInt64(numericIP / 16777216) % 256);
        string x = Convert.ToString(Convert.ToInt64(numericIP / 65536) % 256);
        string y = Convert.ToString(Convert.ToInt64(numericIP / 256) % 256);
        string z = Convert.ToString(Convert.ToInt64(numericIP) % 256);

        return w + "." + x + "." + y + "." + z;
    }

And this one

public static long GetNumericIP(string standardIP)
    {
            if (standardIP != null && standardIP != string.Empty)
            {
                string[] ipParts = standardIP.Split('.');
                long numericIP = 16777216 * Convert.ToInt64(ipParts[0]) + 65536 * Convert.ToInt64(ipParts[1]) + 256 * Convert.ToInt32(ipParts[2]) + Convert.ToInt32(ipParts[3]);

                return numericIP;
            }
            return 0;
    }

You may want to improve them by doing checks on the parameters and use string.concat

风蛊 2024-09-21 11:53:15

我强烈不同意所提供的答案。它确实有效,但我可以看到它存在严重的问题,首先是可读性。在我看来,可读性和可维护性是最重要的,而接受的解决方案根本行不通。除此之外,更通用的方法也将解决 IPv6 的问题,而公认的解决方案将不起作用。

我的建议是使用以下方法:

    public static IPAddress AddOne(this IPAddress ipAddress)
    {
        byte[] data = ipAddress.GetAddressBytes();

        IncrementByOneFromRight(data, data.Length - 1);

        return new IPAddress(data);
    }

    private static void IncrementByOneFromRight(byte[] data, int index)
    {
        if (index < 0)
            return;

        if (data[index] < byte.MaxValue)
            data[index] += 1;
        else
        {
            data[index] = 0;

            IncrementByOneFromRight(data, index - 1);
        }
    }

将上述内容放在可见的静态类中,AddOne 方法将作为 IPAddress 的扩展方法。这使得使用起来更容易,并且您不会公开添加到类中的 IPAddress 的具体实现细节,同时保持可读性。这将带来额外的好处,即不会使您已经使用可能不相关的方法编写的类变得混乱。

如果您同意我的答案以及我不同意已批准答案的原因,请投票,以便提出此问题的人可以看到这一点。

I strongly disagree with the provided answer. It surely works, but I can see serious problems with it, starting with readability. In my opinion, readability and maintainability are paramount, and the accepted solution simply won't do. Adding to this, a more generic approach will also solve the problem for IPv6, while the accepted solution will not work.

My proposal is to use the following method:

    public static IPAddress AddOne(this IPAddress ipAddress)
    {
        byte[] data = ipAddress.GetAddressBytes();

        IncrementByOneFromRight(data, data.Length - 1);

        return new IPAddress(data);
    }

    private static void IncrementByOneFromRight(byte[] data, int index)
    {
        if (index < 0)
            return;

        if (data[index] < byte.MaxValue)
            data[index] += 1;
        else
        {
            data[index] = 0;

            IncrementByOneFromRight(data, index - 1);
        }
    }

Place the above in a visible static class, and the AddOne method will work as an extension method to IPAddress. This makes it easier to work with, and you will not expose the nitty-gritty implementation details of adding to the IPAddress in your class, while maintaining and readability. This will have the added benefit of not cluttering the class you are already writing with possibly unrelated methods.

Please vote up so that this is visible to people coming to this question if you agree with my answer and the reasons I disagree with the approved one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文