tags:

views:

63

answers:

6

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

A: 

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.

Andreas Rehm
Why the redundant increment-and-assign?
Timwi
+3  A: 

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];
paxdiablo
It works, but it's not pretty.
Noon Silk
Why the redundant double-increments?
Timwi
@silky, feel free to post an answer with "prettier" code if you wish. That's perfectly readable to me, and to anyone else if you just took that first paragraph and put it in the code as a comment. Me, I prefer function over form.
paxdiablo
@paxdiablo: Just to be clear, I didn't downvote you (indeed, the opposite).
Noon Silk
@Timwi, that was held over from the OP's code, fixed now.
paxdiablo
@paxdiablo, I meant the fact that you used `++` but then also assigned the result. You’ve changed this to `+ 1` now, but I really don’t see why you can’t just write `ip[3]++` and do away with the cast too.
Timwi
@silky, that's okay, I don't succumb to retaliatory strikes.
paxdiablo
@Timwi, you could do that. It will be much the same code underneath. I'll add that as an option.
paxdiablo
@paxdiablo: No worries, just wanted to indirectly express my indignation that someone would downvote you for a correct answer.
Noon Silk
Thanks for all answers, I am trying to make a loop from a start ip to end ip, this increase is for it, but I cant imagine how can I make this?
Rapunzo
@Rapunzo, since you've already accepted an answer for this question, you're probably better off asking a new question for that (since it _is_, after all, a new question).
paxdiablo
+1  A: 

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.

Joel Etherton
A: 

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.

Timwi
A: 

You can convert the IP into its numerical equivalent.

Check this previously answered question for details:

http://stackoverflow.com/questions/3342378/best-type-for-ip-address-in-hibernate-entity/3342450#3342450

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

Pierre 303
A: 

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;
    }
Noon Silk