ip地址增量问题
我想增加我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的问题是,当
ip[3]
环绕时(依此类推),您没有增加ip[2]
。以下代码应该可以解决问题,最终从255.255.255.255
包装到0.0.0.0
:以下代码也可能有效:
Your problem is that you're not increasing
ip[2]
whenip[3]
wraps around (and so on up the hierarchy). The following code should do the trick, finally wrapping from255.255.255.255
to0.0.0.0
:The following may also work:
可能值得注意的是,现有答案均不处理 IPv6 地址,IPAddress 类本身确实可以满足需要。为此,您可能想要采用更通用的策略(我不确定 IPv6 的增量规则是什么样的,尽管它们可能完全相同,只是需要更多字节来完成,我怀疑这是案)。
- 编辑:
在此基础上,这似乎有效:
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:
在第一个示例中,您仅递增第四个字节序列。所以它将从 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.
您需要检查您的地址是否为 254 - 255 且 0 为广播地址。
ipAddressControl1.Text = "192.168.1.255";
但您只能检查 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";
But you can only check for overflows up to ip[0] - you need to take care if you hit 255 there.
看起来 IP 地址以“错误的方式”存储在您尝试使用的
.Address
属性中:因此添加
1 << 24
只会增加左侧的0xff
,然后将其截断,将其变为0
。如果您希望它按照您描述的方式工作,您必须编写自己的加法函数。
或类似的东西。
Looks like IP addresses are stored the “wrong way around” in the
.Address
property you tried to use:So adding
1 << 24
is only going to increment the0xff
on the left and then truncate it, turning it into0
.You’ll have to write your own addition function if you want this to work the way you describe.
or something like that.
您可以将 IP 转换为其等价的数字。
检查之前回答的问题以了解详细信息:
最佳 IP 类型-Hibernate 实体中的地址?
而这个
您可能想通过检查参数并使用 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?
And this one
You may want to improve them by doing checks on the parameters and use string.concat
我强烈不同意所提供的答案。它确实有效,但我可以看到它存在严重的问题,首先是可读性。在我看来,可读性和可维护性是最重要的,而接受的解决方案根本行不通。除此之外,更通用的方法也将解决 IPv6 的问题,而公认的解决方案将不起作用。
我的建议是使用以下方法:
将上述内容放在可见的静态类中,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:
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.