Implementation of Subnet mask concept.
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
class ip
{
public
static void main(String args[])throws IOException
{
InputStreamReader
isr=new InputStreamReader(System.in);
BufferedReader
stdin=new BufferedReader(isr);
System.out.println("Enter
an Ip address ");
String
ip=stdin.readLine();
System.out.println("Enter
no of Sub-nets");
int
sn=Integer.parseInt(stdin.readLine());
int
ipi[]=new int[4];
int
d=ip.lastIndexOf('.');
String
ip4=ip.substring(d+1,ip.length());
int temp[]=new int[3];
int k=0;
for(int
j=0;j<ip.length();j++)
{
if(ip.charAt(j)=='.')
{
temp[k]=j;
k++;
}
}
String ip1=ip.substring(0,3);
String
ip2=ip.substring(temp[0]+1,temp[1]);
String
ip3=ip.substring(temp[1]+1,temp[2]);
ipi[0]=Integer.parseInt(ip1);
ipi[1]=Integer.parseInt(ip2);
ipi[2]=Integer.parseInt(ip3);
ipi[3]=Integer.parseInt(ip4);
if(ipi[0]<128&&ipi[0]>-1)
{
System.out.println("It
is a Class A address");
System.out.println("Default
Mask is : 255.0.0.0");
System.out.print("Network
Address : ");
System.out.println(ip1+"."+0+"."+0+"."+0);
System.out.print("Broadcast
Address : ");
System.out.println(ip1+"."+255+"."+255+"."+255);
System.out.print("Subnet
Mask:");
if(sn<=2&&sn>=1)
{
System.out.println(255+"."+128+"."+0+"."+0);
bin(1,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=4&&sn>=3)
{
System.out.println(255+"."+192+"."+0+"."+0);
bin(2,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=8&&sn>=5)
{
System.out.println(255+"."+224+"."+0+"."+0);
bin(3,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=16&&sn>=9)
{
System.out.println(255+"."+240+"."+0+"."+0);
bin(4,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=32&&sn>=17)
{
System.out.println(255+"."+248+"."+0+"."+0);
bin(5,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=64&&sn>=33)
{
System.out.println(255+"."+252+"."+0+"."+0);
bin(6,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=128&&sn>=65)
{
System.out.println(255+"."+254+"."+0+"."+0);
bin(7,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=256&&sn>=129)
{
System.out.println(255+"."+255+"."+0+"."+0);
bin(8,sn,1,ipi[0],ipi[1],ipi[2]);
}
else
System.out.println("Capacity
Exceeded");
}
else
if(ipi[0]>=128&&ipi[0]<=191)
{
System.out.println("It
is a Class B address");
System.out.println("Default
Mask is : 255.255.0.0");
System.out.print("Network
Address : ");
System.out.println(ip1+"."+ip2+"."+0+"."+0);
System.out.print("Broadcast
Address : ");
System.out.println(ip1+"."+ip2+"."+255+"."+255);
System.out.print("Subnet
Mask:");
if(sn<=2&&sn>=1)
{
System.out.println(255+"."+255+"."+128+"."+0);
bin(1,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=4&&sn>=3)
{
System.out.println(255+"."+255+"."+192+"."+0);
bin(2,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=8&&sn>=5)
{
System.out.println(255+"."+255+"."+224+"."+0);
bin(3,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=16&&sn>=9)
{
System.out.println(255+"."+255+"."+240+"."+0);
bin(4,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=32&&sn>=17)
{
System.out.println(255+"."+255+"."+248+"."+0);
bin(5,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=64&&sn>=33)
{
System.out.println(255+"."+255+"."+252+"."+0);
bin(6,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=128&&sn>=65)
{
System.out.println(255+"."+255+"."+254+"."+0);
bin(7,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=256&&sn>=129)
{
System.out.println(255+"."+255+"."+255+"."+0);
bin(8,sn,2,ipi[0],ipi[1],ipi[2]);
}
else
System.out.println("Capacity
Exceeded");
}
else
if(ipi[0]>=192&&ipi[0]<=223)
{
System.out.println("It
is a Class C address");
System.out.println("Default
Mask is : 255.255.255.0");
System.out.print("Network
Address : ");
System.out.println(ip1+"."+ip2+"."+ip3+"."+0);
System.out.print("Broadcast
Address : ");
System.out.println(ip1+"."+ip2+"."+ip3+"."+255);
System.out.print("Subnet
Mask:");
if(sn<=2&&sn>=1)
{
System.out.println(255+"."+255+"."+255+"."+128);
bin(1,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=4&&sn>=3)
{
System.out.println(255+"."+255+"."+255+"."+192);
bin(2,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=8&&sn>=5)
{
System.out.println(255+"."+255+"."+255+"."+224);
bin(3,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=16&&sn>=9)
{
System.out.println(255+"."+255+"."+255+"."+240);
bin(4,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=32&&sn>=17)
{
System.out.println(255+"."+255+"."+255+"."+248);
bin(5,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=64&&sn>=33)
{
System.out.println(255+"."+255+"."+255+"."+252);
bin(6,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=128&&sn>=65)
{
System.out.println(255+"."+255+"."+255+"."+254);
bin(7,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
if(sn<=256&&sn>=129)
{
System.out.println(255+"."+255+"."+255+"."+255);
bin(8,sn,3,ipi[0],ipi[1],ipi[2]);
}
else
System.out.println("Capacity
Exceeded");
}
else
if(ipi[0]>=224&&ipi[0]<=239)
System.out.println("It
is a Class D address");
else
if(ipi[0]>=240&&ipi[0]<=255)
System.out.println("It
is a Class E address");
else
System.out.println("Not
Valid");
}
static
void bin(int s,int sn,int x,int ipi1,int ipi2,int ipi3)
{
int
ip[]=new int[s];
for(int
k=0;k<s;k++)
ip[k]=0;
//System.out.println("Initially
ip Arry");
//for(int
k=0;k<s;k++)
//System.out.print(ip[k]+"
");
//System.out.println();
int
c[]=new int[s+1];
for(int
l=1;l<=s;l++)
c[l]=0;
c[0]=0;
//System.out.println("Initially
c Arry");
//for(int
k=0;k<=s;k++)
//System.out.print(c[k]+"
");
//System.out.println();
int
temp[]=new int[s];
temp[0]=1;
for(int
k=1;k<s;k++)
temp[k]=0;
//System.out.println("Initially
temp Arry");
//for(int
k=0;k<s;k++)
//System.out.print(temp[k]+"
");
//System.out.println();
int
a=1;
while(a<=sn)
{
System.out.print("The
Starting Address of " +a+"th Sub-NetWork is :" );
for(int
k=0;k<s;k++)
{
if(a==1)
break;
else
if(ip[k]==1&&temp[k]==1&&c[k]==1)
{
c[k+1]=1;
ip[k]=1;
}
else
if(ip[k]==1&&temp[k]==1&&c[k]==0)
{
c[k+1]=1;
ip[k]=0;
}
else
if(ip[k]==1&&temp[k]==0&&c[k]==1)
{
c[k+1]=1;
ip[k]=0;
}
else
if(ip[k]==1&&temp[k]==0&&c[k]==0)
{
c[k+1]=0;
ip[k]=1;
}
else
if(ip[k]==0&&temp[k]==1&&c[k]==0)
{
c[k+1]=0;
ip[k]=1;
}
else
if(ip[k]==0&&temp[k]==0&&c[k]==1)
{
c[k+1]=0;
ip[k]=1;
}
else
if(ip[k]==0&&temp[k]==1&&c[k]==1)
{
c[k+1]=1;
ip[k]=0;
}
else
{
c[k+1]=0;
ip[k]=0;
}
}
a++;
int
ip1[]=new int[8];
for(int
i=0;i<=7;i++)
ip1[i]=0;
int
j=7;
for(int
i=s-1;i>=0;i--,j--)
ip1[j]=ip[i];
double
d1=dec(ip1);
int
d2=(int)d1;
if(x==1)
System.out.println(ipi1+"."+d2+"."+0+"."+0);
else
if(x==2)
System.out.println(ipi1+"."+ipi2+"."+d2+"."+0);
else
System.out.println(ipi1+"."+ipi2+"."+ipi3+"."+d2);
}
}
static
double dec(int ip1[])
{
double
d1=0.0;
for(int
j=0;j<ip1.length;j++)
{
double b=Math.pow(2,j);
d1=d1+ip1[j]*b;
}
return
d1;
}
}
/* output:
Enter an Ip address
176.11.12.0
Enter no of Sub-nets
6
It is a Class B address
Default Mask is : 255.255.0.0
Network Address : 176.11.0.0
Broadcast Address : 176.11.255.255
Subnet Mask:255.255.224.0
The Starting Address of 1th Sub-NetWork is
:176.11.0.0
The Starting Address of 2th Sub-NetWork is
:176.11.32.0
The Starting Address of 3th Sub-NetWork is
:176.11.64.0
The Starting Address of 4th Sub-NetWork is
:176.11.96.0
The Starting Address of 5th Sub-NetWork is
:176.11.128.0
The Starting Address of 6th Sub-NetWork is
:176.11.160.0
Press any key to continue...
*/
No comments:
Post a Comment