17BCE0764 Sumanto Pal CSE1004 Networks and Communication: Lab Exercise 4a IP Address Allocation
17BCE0764 Sumanto Pal CSE1004 Networks and Communication: Lab Exercise 4a IP Address Allocation
import java.util.*;
class Allocator{
int[] array,endid;
public long available;
long end,cidr;
int id1,id2,id3,id4;
Allocator(){}
Allocator(int[] array, int cidr)
{
this.array=array;
this.id1=array[0];
this.id2=array[1];
this.id3=array[2];
this.id4=array[3];
this.cidr=cidr;
this.available=(long)Math.pow(2,(32-cidr));
}
void counter(int c,int a)
{
System.out.println("Group End
Address"+id1+"."+id2+"."+id3+"."+id4);
int alloc = c*a;
if(alloc<=available)
{
for(;alloc>1;alloc--)
{
--available;
++id4;
if(id4==256)
{
id4=0;
id3++;
}
}
1
}
System.out.println("Group End
Address"+id1+"."+id2+"."+id3+"."+id4++);
}
}
public class IPAllocation
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); int i=0;
System.out.println("Enter Starting address");
String ad = sc.nextLine();
String[] add= ad.split("\\.");
int j=0;
int[] array = new int[4];
for(String a:add)
{
array[j]=Integer.parseInt(a);
j++;
}
System.out.println("Enter CIDR value");
int cidr = sc.nextInt();
Allocator al = new Allocator(array,cidr);
System.out.println("Addresses available:
"+al.available);
while(i!= 5)
{
System.out.println("Enter number of clients: ");
int c = sc.nextInt();
System.out.println("Enter number of Addresses
needed");
int a = sc.nextInt();
al.counter(c,a);
System.out.println("Continue?\n1.Yes\n2.No");
String ans = sc.next();sc.nextLine();
if(ans.equals("No"))
{
i=5;
System.out.println("Addresses available:
"+al.available); break;
}
}
2
}
Output