IP地址递增问题 [英] ip address increment problem
本文介绍了IP地址递增问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想提高我的IP地址和;
I want to increase my ip address and;
下面是代码
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
but both of them gives me 192.168.1.0 but I want to get value as 192.168.2.0
推荐答案
您的问题是,你不增加 IP [2]
时 IP [3]
环绕(依此类推,直至层次结构)。下面的代码应该做的伎俩,终于从 255.255.255.255
包装到 0.0.0.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];
这篇关于IP地址递增问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文