Menu

Computer Networks - (LAB PROGRAMS)


Aim:

 Write a program for congestion control using Leaky bucket algorithm.

Solution :

Week 8

Congestion control using Leaky Bucket algorithm.


Source Code:

File Name: leakybucket.c

#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define NUM_PACKETS 10

// Custom random function to ensure non-zero result
int get_random(int max_value) {
    int result = (random() % 10) % max_value;
    return result == 0 ? 1 : result;
}

int main() {
    int packet_size[NUM_PACKETS];
    int i, clock_tick, bucket_size, output_rate;
    int remaining_data = 0, data_to_send, transmission_time, transmitted;

    // Generate random packet sizes
    for (i = 0; i < NUM_PACKETS; ++i)
        packet_size[i] = get_random(6) * 10;

    // Display the generated packet sizes
    for (i = 0; i < NUM_PACKETS; ++i)
        printf("\nPacket[%d]: %d bytes", i + 1, packet_size[i]);

    // Get output rate and bucket size from the user
    printf("\n\nEnter the Output Rate (bytes/unit time): ");
    scanf("%d", &output_rate);

    printf("Enter the Bucket Size (in bytes): ");
    scanf("%d", &bucket_size);

    // Process each incoming packet
    for (i = 0; i < NUM_PACKETS; ++i) {
        // Check if incoming packet + remaining data exceeds bucket size
        if ((packet_size[i] + remaining_data) > bucket_size) {
            if (packet_size[i] > bucket_size) {
                printf("\n\nIncoming Packet[%d] size (%d bytes) exceeds bucket capacity (%d bytes) - PACKET REJECTED!", i + 1, packet_size[i], bucket_size);
            } else {
                printf("\n\nBucket capacity exceeded for Packet[%d] - PACKET REJECTED!", i + 1);
            }
        } else {
            // Accept and add packet to bucket
            remaining_data += packet_size[i];
            printf("\n\nIncoming Packet[%d] size: %d bytes", i + 1, packet_size[i]);
            printf("\nTotal bytes to transmit (in bucket): %d", remaining_data);

            // Simulated transmission time (random)
            transmission_time = get_random(4) * 10;
            printf("\nEstimated transmission time: %d units", transmission_time);

            // Transmit data in units of time
            for (clock_tick = 10; clock_tick <= transmission_time; clock_tick += 10) {
                sleep(1);  // Simulate delay (1 second per 10 units)

                if (remaining_data > 0) {
                    if (remaining_data <= output_rate) {
                        transmitted = remaining_data;
                        remaining_data = 0;
                    } else {
                        transmitted = output_rate;
                        remaining_data -= output_rate;
                    }

                    printf("\nTransmitted %d bytes ---- Remaining in bucket: %d bytes", transmitted, remaining_data);
                } else {
                    printf("\nNo data to transmit at time unit %d!", clock_tick);
                }
            }
        }
    }

    return 0;
}


Output:


$ gcc leakybucket.c
$ ./a.out

Packet[1]: 30 bytes
Packet[2]: 10 bytes
Packet[3]: 10 bytes
Packet[4]: 50 bytes
Packet[5]: 30 bytes
Packet[6]: 50 bytes
Packet[7]: 10 bytes
Packet[8]: 20 bytes
Packet[9]: 30 bytes
Packet[10]: 10 bytes

Enter the Output Rate (bytes/unit time): 10
Enter the Bucket Size (in bytes): 15


Incoming Packet[1] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[2] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 20 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
No data to transmit at time unit 20!

Incoming Packet[3] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 30 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes
No data to transmit at time unit 20!
No data to transmit at time unit 30!

Incoming Packet[4] size (50 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[5] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[6] size (50 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[7] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 10 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes

Incoming Packet[8] size (20 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[9] size (30 bytes) exceeds bucket capacity (15 bytes) - PACKET REJECTED!

Incoming Packet[10] size: 10 bytes
Total bytes to transmit (in bucket): 10
Estimated transmission time: 10 units
Transmitted 10 bytes ---- Remaining in bucket: 0 bytes



Related Content :

Computer Networks Lab Programs

1) Implement the data link layer framing methods such as character, character-stuffing and bit stuffing.   View Solution

2) Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP   View Solution

3) Develop a simple data link layer that performs the flow control using the sliding window protocol, and loss recovery using the Go-Back-N mechanism.   View Solution

4) Implement Dijsktra’s algorithm to compute the shortest path through a network   View Solution

5) Take an example subnet of hosts and obtain a broadcast tree for the subnet.   View Solution

6) Implement distance vector routing algorithm for obtaining routing tables at each node.   View Solution

7) Implement data encryption and data decryption   View Solution

8) Write a program for congestion control using Leaky bucket algorithm.   View Solution

9) Write a program for frame sorting techniques used in buffers.   View Solution

10) Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv.Analysis and Statistics & Filters.
  View Solution

11) How to run Nmap scan   View Solution

12) Operating System Detection using Nmap   View Solution

13) Do the following using NS2 Simulator
i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate & Throughput.
vi. Simulate to Plot Congestion for Different Source/Destination
vii. Simulate to Determine the Performance with respect to Transmission of Packets
  View Solution