There is now a scenario where a starting IP address is provided and an ending IP address is provided, and all IP addresses in the IP segment are to be cycled out.
Of course, the first thing that comes to mind is 4 for loops. It's also the easiest way to do it. This is definitely not what it is now.
I think it is implemented through 2 loops or 1 function call, because I am not familiar with the addition and subtraction operations of binary and have no results.
Later, referring to the methods on the Internet, a solution I personally think is better.
using System; using ; namespace ConsoleApplicationForTest { class Program { static void Main(string[] args) { string startIP = "10.1.1.1"; string endIP = "10.2.3.4"; long lsIP = ToHex(startIP); long leIP = ToHex(endIP); SortedList list = new SortedList(); for (; lsIP < leIP; lsIP++) { string temp = ToIp(lsIP); (temp, temp); } } /// <summary> /// Convert IP address to numerical form /// </summary> /// <param name="ip"></param> /// <returns>Long plastic surgery</returns> public static long ToHex(string ip) { string[] arrIP = ('.'); for (int i = 0; i < ; i++) { arrIP[i] = (arrIP[i]) > 255 ? (255).PadLeft(2, '0') : arrIP[i] = ((arrIP[i]), 16).PadLeft(2, '0'); } ip = ("", arrIP); long hexIP = (ip, ); return hexIP; } /// <summary> /// Convert numerical form to IP address /// </summary> /// <param name="l"></param> /// <returns>IP Address</returns> public static string ToIp(long l) { var bt = new long[4]; for (int i = 0; i < 4; i++) { bt[3 - i] = (long)(l >> 8 * i & 255); } string ipAddress = (".", bt); return ipAddress; } } 60 }
The following is a Class given by a colleague
using System; using ; namespace ConsoleApplicationForTest { public class IpDigitCovert { /// <summary> /// Convert IP address to numerical form /// </summary> /// <returns>Long integer</returns> public static long ToDigit(IPAddress ip) { int x = 3; long l = 0; foreach (byte b in ()) { l += (long)b << 8 * x; } return l; } /// <summary> /// Convert the value to an IP address /// </summary> /// <returns>IP Address</returns> public static IPAddress ToIP(long l) { var bt = new byte[4]; for (int i = 0; i < 4; i++) { bt[3 - i] = (byte)(l >> 8 * i & 255); } return new IPAddress(bt); } /// <summary> /// Convert IP address to numerical form /// </summary> /// <param name="ip"></param> /// <returns>Long integer</returns> public static long ToHex(string ip) { string[] arrIP = ('.'); for (int i = 0; i < ; i++) { arrIP[i] = (arrIP[i]) > 255 ? (255).PadLeft(2, '0') : arrIP[i] = ((arrIP[i]), 16).PadLeft(2, '0'); } ip = ("", arrIP); long hexIP = (ip, ); return hexIP; } /// <summary> /// Convert numeric form to IP address /// </summary> /// <param name="l"></param> /// <returns>ip address</returns> public static string ToIp(long l) { var bt = new long[4]; for (int i = 0; i < 4; i++) { bt[3 - i] = (long)(l >> 8 * i & 255); } string ipAddress = (".", bt); return ipAddress; } } }
Method supplement
import ; import ; import ; import ; public class IpRangeCalculator { public static String[] calculateIpRange(String startIp, String endIp) { try { InetAddress startInetAddress = (startIp); InetAddress endInetAddress = (endIp); byte[] startBytes = (); byte[] endBytes = (); List<String> ipList = new ArrayList<>(); for (int i = 0; i < ; i++) { for (int j = (("\\.")[i]); j < (("\\.")[i]); j++) { byte[] tempBytes = (); tempBytes[i] = (byte) j; ((tempBytes).getHostAddress()); } } return (new String[0]); } catch (Exception e) { (); return null; } } public static void main(String[] args) { String startIp = "192.168.1.1"; String endIp = "192.168.1.5"; String[] ipRange = calculateIpRange(startIp, endIp); for (String ip : ipRange) { (ip); } } }
This method first converts the start and end IP addresses into InetAddress objects, and then gets their byte array representation. For each byte of the IP address, it traverses the possible values, creates a new byte array for each byte, and converts it back to the IP address, and adds it to the result list. Finally, convert the list to an array of strings and return.
This is the article about how Java obtains all IP addresses in an IP segment. For more related Java obtains all IP addresses in an IP segment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!