You are on page 1of 2

CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM

Congestion: It is a state occurs in the network layer when the message traffic is so heavy that
it slows down network response time.

Congestion control is a mechanism that controls the entry of data packets into the network.

Image I bucket with a hole and water is flowing from a tap at a variable rate, the water is
flowing drop by drop from the hole. In this scenario, no matter at what rate the water enters the
bucket, the outflow is constant. Once after the bucket is full, that water is spilled out and gets
wasted.

In the same way, no matter how the host is sending the packets, the bucket will outflow only
one packet at a time in a constant rate. When the congestion is there, what this leaky bucket
will do? It will holds the packets and sends no matter how many packets the sender is sending
but sends out the packet from the router one after the other in a regulated flow. So, a busty
traffic is converted into uniform traffic with the help of leaky bucket.
package leacky;
import java.util.Scanner;

public class Leacky {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int bcktsize, iter, rate, line, total = 0;
int[] pkt = new int[25];
System.out.print("Enter the bucket size and output rate(both in MB):");
bcktsize = sc.nextInt();
rate = sc.nextInt();
System.out.println("Enter the number of input lines");
line = sc.nextInt();

System.out.println("Enter input packet rate of " + line + " lines");


for(int i = 0; i < line; i++) {
pkt[i] = sc.nextInt();
}
System.out.println("Enter the number of iterations");
iter = sc.nextInt();
for(int i = 0; i < iter; i++) {
System.out.println("\nIteration " + (i + 1));
for(int j = 0; j < line; j++) {
total += pkt[j];
if(total <= bcktsize) {
System.out.println("\nInput from line " + (j + 1) + " with rate " + pkt[j] + " is added to the
bucket\nCurrent bucket size(Mb) is " + total);
} else {
total -= pkt[j];
System.out.println("\nInput from line " + (j + 1) + " with rate " + pkt[j] + " is thrown out of
bucket\nCurrent bucket size(Mb)is " + total);
}
}
if(total <= rate) {
System.out.print("packet sent to outputline at rate " + total);
total = 0;
System.out.print("current bucket size is " + total);
} else {
total -= rate;
System.out.println("\n---------------------------------");
System.out.println("\npacket sent to output line at rate " + rate + " \n Current bucket size(Mb)is
" + total);
}
}
}

You might also like