IP address increment problem

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 give me 192.168.1.0, but I want to get the value as 192.168.2.0

+3
source share
7 answers

Your problem is that you do not increase ip[2]when it is ip[3]wrapped (and so on up the hierarchy). The following code should do the trick, finally wrapping from 255.255.255.255to 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];
+8
source

, , 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;
    }
+2

, 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 .

+1

4- . , 255 0 [2].

1, 2 1. , .

+1

, 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);
}

- .

0

IP .

:

IP- 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;
    }

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

0

. , , , , . , , . , 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 , . , , , , .

, , , , , .

0

Source: https://habr.com/ru/post/1759658/


All Articles