Let's say 开发者_开发百科I have two IP addresses (in .NET, the System.Net.IPAddress
class). How can I iterate over all the IP addresses between two given addresses?
For example, let one address be 192.168.1.1 and the other 192.168.2.3. I want to somehow iterate over all the addresses in between and print them to the console.
Thank you.
In the end I ended up using the approach provided in this answer.
It's a little more complicated but it works well without using deprecated properties. It converts IP addresses to uint
s, increments them, then converts them back.
IPv4 addresses are basically Int64
's with a different notation. So you can do the following:
for (var i = fromAddress.Address; i <= toAddress.Address; i++)
{
Console.WriteLine(new IPAddress(i));
}
The Address property is deprecated but that may not be a concern to you.
精彩评论