ip address increment problem
I want to increase my ip address开发者_如何学JAVA 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
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];
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;
}
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.
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.
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.
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
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.
精彩评论