0% found this document useful (0 votes)
28 views

17BCE0764 Sumanto Pal CSE1004 Networks and Communication: Lab Exercise 4a IP Address Allocation

This document contains code for an IP address allocation program. It defines an Allocator class with methods to initialize an IP address range based on a starting address and CIDR value entered by the user. The counter method allocates a number of addresses entered by the user and prints the ending address of the group. The main method gets user input, creates an Allocator object, and calls the counter method in a loop until the user enters "No" to discontinue.

Uploaded by

Sumanto Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

17BCE0764 Sumanto Pal CSE1004 Networks and Communication: Lab Exercise 4a IP Address Allocation

This document contains code for an IP address allocation program. It defines an Allocator class with methods to initialize an IP address range based on a starting address and CIDR value entered by the user. The counter method allocates a number of addresses entered by the user and prints the ending address of the group. The main method gets user input, creates an Allocator object, and calls the counter method in a loop until the user enters "No" to discontinue.

Uploaded by

Sumanto Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

17BCE0764 SUMANTO PAL

CSE1004 Networks and Communication


Lab Exercise 4a IP Address Allocation Code

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

You might also like