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.c s produces IPAddressInfo.d ll
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(s tring 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.WriteLi ne("Incorrect IP Address Format ---> "+ ipaddress);
Console.WriteLi ne("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.WriteLi ne("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.WriteLi ne("Incorrect Format ---> "+ ipaddress +"enter in integers
I.E. 1,2,3 etc");
}
}
public SubnetInfo():ba se() //default constructor
{
networkBits = 0;
borrowedBits = 0;
}
// special constructor
public SubnetInfo(stri ng 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.ToChar Array();
string octet4 = IP.ToString();
for (int x = 0; x <= 4; x++) // for loop
{
split = octet4.Split(de limiter); // split the ip address at the delimiter
which is a dot
}
Console.WriteLi ne("Total Subnets : "+ totalsubnets);
int hold = Int32.Parse(spl it[3]);
for (i = 0; i < totalsubnets;i+ +)
{
first = (i/totalsubnets)*t otalnetworks+1+ hold; // find the first usuable
network number
last = ((i+1)/totalsubnets)*t otalnetworks-2+hold;
Console.Write(" Subnet "+(i+1)+" ranges from ");
for(int j=0; j < split.Length -1;j++)
{
Console.Write(s plit[j]);
if (j==3)
break;
Console.Write(" .");
}
Console.Write(f irst+"/"+newNet+" to ");
for(int j=0;j<split.Len gth -1;j++)
{
Console.Write(s plit[j]);
if (j==3)
break;
Console.Write(" .");
}
Console.Write(l ast+"/"+newNet);
Console.WriteLi ne("");
Console.WriteLi ne("first: "+ first);
Console.WriteLi ne("last : "+ last);
}
}
public override void Display() // over ride the ToString method
{
Console.WriteLi ne("IP Address is : "+ IP);
Console.WriteLi ne("Network bits : "+ networkBits);
Console.WriteLi ne("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:IPAdd ressInfo.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.ToChar Array();
//validation checking...
while(check == false) // while loop
{
Console.WriteLi ne("enter in a Class C IP Address");
try // try catch general exception
{
entIPaddress = Console.ReadLin e(); //read in from console line
for (int x = 0; x <= 4; x++) // for loop
{
split = entIPaddress.Sp lit(delimiter); // split the ip address at the
delimiter which is a dot
}
int temphold = Int32.Parse(spl it[0]); // convert and insert the 1st octet
into an integer.
if((temphold < 192)||(temphold >= 224)) // validation check
{
Console.WriteLi ne("***** 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(sp lit[i]) < 0 || Int32.Parse(spl it[i]) > 255)
{
Console.WriteLi ne("Invalid IP address - all octets should be in range
0-255");
}
}
}
catch(Exception ) // catch a general exception
{
Console.WriteLi ne("Incorrect entry");
}
}
try
{
Console.WriteLi ne("enter in network bits");
netbit = int.Parse(Conso le.ReadLine()); // read in console line and convert
to and integer
while((netbit < 24)||(netbit > 30)) // validate while loop
{
Console.WriteLi ne("***** Not a Valid Bit -- between 24 & 30");
Console.WriteLi ne("enter in network bits");
netbit =int.Parse(Cons ole.ReadLine()) ; // read in console line and convert
to and integer
}
}
catch(Exception ) // catch a general exception
{
Console.WriteLi ne("Incorrect integer Entry");
}
try
{
Console.WriteLi ne("enter in borrowed bits");
borobit = int.Parse(Conso le.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.WriteLi ne("***** Not a Valid Bit -- between 2 & "+ck+"");
Console.WriteLi ne("enter in borrowed bits");
borobit = int.Parse(Conso le.ReadLine());// read in console line and convert
to and integer
}
}
catch(Exception ) //catch general execption
{
Console.WriteLi ne("Incorrect integer Entry");
}
Console.WriteLi ne("");
SubnetInfo sub1 = new SubnetInfo(netb it,borobit,entI Paddress); //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