hi im needing help with a program im writing to do subnetting
i was on before about it and i got some help.
the code below wont work for me and i cant think of why not.
i was wondering if anyone out there can help, even come up with a better way
of doing this..
i have 2 files..
here is my first file..
// a library assembly i.e. dll
// csc /target:library IPAddressInfo.cs produces IPAddressInfo.dll
using System;
using System.Net;
namespace IPinfo
{
// start class..... inserting IP address information
public abstract class IPAddressInfo
{
private IPAddress ipad;// IPAddress class variable
// read / write property
public IPAddress IP
{
get // read
{
return ipad;
}
set //write
{
ipad = value;
}
}
// constuctor class
public IPAddressInfo(string ipaddress) //read in a string
{
// try catch excetion block
try
{
ipad = IPAddress.Parse(ipaddress); // change the string information into and
ip address
}
catch (Exception) // general catched exception and print out message
{
Console.WriteLine("Incorrect IP Address Format ---> "+ ipaddress);
Console.WriteLine("Correct Format ----> 192.0.0.0 to 223.255.255.255");
}
}
public IPAddressInfo() //default constructor
{
ipad = IPAddress.Parse("192.0.0.0"); // change the string information into
and ip address
}
public abstract void calSubnet(); // abstract class
public virtual void Display() // over ride the ToString method
{
Console.WriteLine("IP Address ---> "+ ipad); // return the IP Address
}
}//end class
// start class..... inserting IP address information
public class SubnetInfo : IPAddressInfo // inherts IPAddressInfo
{
private int networkBits, borrowedBits; // variables of type integer
// constuctor class
public SubnetInfo(int netBits, int boroBits, string ipaddress) :
base(ipaddress)//read in 2 integers and a string
{
// try catch excetion block
try
{
networkBits = netBits;
borrowedBits = boroBits;
}
catch (Exception) // general catched exception and print out message
{
Console.WriteLine("Incorrect Format ---> "+ ipaddress +"enter in integers
I.E. 1,2,3 etc");
}
}
public SubnetInfo():base() //default constructor
{
networkBits = 0;
borrowedBits = 0;
}
// special constructor
public SubnetInfo(string ipaddress):base(ipaddress)
{
}
public override void calSubnet()
{
int bitsleft = 32 - networkBits; // calculate the bits left over.
int totalnetworks = (int)Math.Pow(2,bitsleft);// calculate the total number
of networks (2 to the power of bitsleft)
int totalsubnets = (int)Math.Pow(2,borrowedBits);// calculate the total
number of subnets
int newNet = networkBits + borrowedBits;
int first = 0;
int last = 0;
int i = 0;
string delimStr = ".";
string [] split = null;
char [] delimiter = delimStr.ToCharArray();
string octet4 = IP.ToString();
for (int x = 0; x <= 4; x++) // for loop
{
split = octet4.Split(delimiter); // split the ip address at the delimiter
which is a dot
}
Console.WriteLine("Total Subnets : "+ totalsubnets);
int hold = Int32.Parse(split[3]);
for (i = 0; i < totalsubnets;i++)
{
first = (i/totalsubnets)*totalnetworks+1+hold; // find the first usuable
network number
last = ((i+1)/totalsubnets)*totalnetworks-2+hold;
Console.Write("Subnet "+(i+1)+" ranges from ");
for(int j=0; j < split.Length -1;j++)
{
Console.Write(split[j]);
if (j==3)
break;
Console.Write(".");
}
Console.Write(first+"/"+newNet+" to ");
for(int j=0;j<split.Length -1;j++)
{
Console.Write(split[j]);
if (j==3)
break;
Console.Write(".");
}
Console.Write(last+"/"+newNet);
Console.WriteLine("");
Console.WriteLine("first: "+ first);
Console.WriteLine("last : "+ last);
}
}
public override void Display() // over ride the ToString method
{
Console.WriteLine("IP Address is : "+ IP);
Console.WriteLine("Network bits : "+ networkBits);
Console.WriteLine("Borrowed bits : "+ borrowedBits); // return the IP
Address
}
}//end class
} // end name space
and the other file is
using System;
using System.IO;
using IPinfo;
// application assembly i.e. .exe
// csc /reference:IPAddressInfo.dll TestIP.cs
// default (global) namespace
public class TestIP
{
public static void Main()
{
//initialise variables
string entIPaddress = "";
int netbit = 0, borobit = 0;
bool check = false;
string delimStr = ".";
string [] split = null;
char [] delimiter = delimStr.ToCharArray();
//validation checking...
while(check == false) // while loop
{
Console.WriteLine("enter in a Class C IP Address");
try // try catch general exception
{
entIPaddress = Console.ReadLine(); //read in from console line
for (int x = 0; x <= 4; x++) // for loop
{
split = entIPaddress.Split(delimiter); // split the ip address at the
delimiter which is a dot
}
int temphold = Int32.Parse(split[0]); // convert and insert the 1st octet
into an integer.
if((temphold < 192)||(temphold >= 224)) // validation check
{
Console.WriteLine("***** Not a Class C IP Address");
check = false;
}
else
{
check = true;
}
for (int i = 0; i != 4; i++) // validation check on each octet in the vald
range 0 to 255
{
if (Int32.Parse(split[i]) < 0 || Int32.Parse(split[i]) > 255)
{
Console.WriteLine("Invalid IP address - all octets should be in range
0-255");
}
}
}
catch(Exception) // catch a general exception
{
Console.WriteLine("Incorrect entry");
}
}
try
{
Console.WriteLine("enter in network bits");
netbit = int.Parse(Console.ReadLine()); // read in console line and convert
to and integer
while((netbit < 24)||(netbit > 30)) // validate while loop
{
Console.WriteLine("***** Not a Valid Bit -- between 24 & 30");
Console.WriteLine("enter in network bits");
netbit =int.Parse(Console.ReadLine()); // read in console line and convert
to and integer
}
}
catch(Exception) // catch a general exception
{
Console.WriteLine("Incorrect integer Entry");
}
try
{
Console.WriteLine("enter in borrowed bits");
borobit = int.Parse(Console.ReadLine()); // read in console line and convert
to and integer
int ck = 32 - netbit; //maxs number of bits left to borrow.
while((borobit < 2)||(borobit > ck)) // validate while loop
{
Console.WriteLine("***** Not a Valid Bit -- between 2 & "+ck+"");
Console.WriteLine("enter in borrowed bits");
borobit = int.Parse(Console.ReadLine());// read in console line and convert
to and integer
}
}
catch(Exception) //catch general execption
{
Console.WriteLine("Incorrect integer Entry");
}
Console.WriteLine("");
SubnetInfo sub1 = new SubnetInfo(netbit,borobit,entIPaddress); //initialise
subnet info class and insert entered data
sub1.calSubnet();
sub1.Display(); // calls the display function
}
}
i cant seem to get this to work for a class C ip address with subnetting..
if any one can help it would be much apprecatied....
thanks