I am a newbie .I write a program to get IP Range in C# 4.0 it works well for the small ranges but when I go for ranges like class A IP address, my program takes a lot of time .Need some help .Here is my code (its not a good approach ). Any suggestion to write in better way.?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ipSplitt
{
class MainClass
{
public static void Main(string[] args)
{
string[] z = new string[4];
int t = 0;
string ip = "3.0.0.0";
string[] Ip = ip.Split(new char[] { '.' });
foreach (string m in Ip)
{
z[t] = m;
t++;
}
int a1, b1, c1, d1 = 0;
a1 = Convert.ToInt32(z[0]);
b1 = Convert.ToInt32(z[1]);
c1 = Convert.ToInt32(z[2]);
d1 = Convert.ToInt32(z[3]);
string[] v = new string[4];
string ip2 = "3.255.255.255";
int l = 0;
string[] Ip2 = ip2.Split(new char[] { '.' });
foreach (string m in Ip2)
{
v[l] = m;
l++;
}
int a2, b2, c2, d2 = 0;
a2 = Convert.ToInt32(v[0]);
b2 = Convert.ToInt32(v[1]);
c2 = Convert.ToInt32(v[2]);
d2 = Convert.ToInt32(v[3]);
while (d2 >= d1 || c2 > c1 || b2 > b1 || a2 > a1)
{
if (d1 > 255)
{
d1 = 1;
c1++;
}
if (c1 > 255)
{
c1 = 1;
b1++;
}
if (b1 > 255)
{
b1 = 1;
a1++;
}
using (StreamWriter writer = new StreamWriter("import.txt",true))
writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);
d1++;
}
}
}
}
but when I go for ranges like class A IP address, my program takes a lot of time
Because you're opening and closing a file handle 255^3 times. Put the using(Streamwriter ...)
block around the outer while loop.
Take a look at the IPAddress class in the .NET framework, and the static Parse function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With