Menu

Computer Networks - (LAB PROGRAMS)


Aim:

 Write a program for frame sorting techniques used in buffers.

Solution :

Week 9

Frame sorting technique used in buffers.


Source Code:

File Name: leakybucket.c

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

#define FRAME_TEXT_SIZE 3      // Each frame holds 3 characters
#define MAX_NO_OF_FRAMES 127   // Maximum number of frames supported

char inputMessage[FRAME_TEXT_SIZE * MAX_NO_OF_FRAMES]; // Buffer to store the original message

// Frame structure to hold individual fragments of the message
struct Frame {
    char text[FRAME_TEXT_SIZE];
    int sequenceNumber;
} frames[MAX_NO_OF_FRAMES], shuffledFrames[MAX_NO_OF_FRAMES];

// Function to split the message into frames and assign sequence numbers
int assignSequenceNumbers() {
    int i = 0, j = 0, frameIndex = 0;
    
    while (i < strlen(inputMessage)) {
        frames[frameIndex].sequenceNumber = frameIndex;
        for (j = 0; j < FRAME_TEXT_SIZE && inputMessage[i] != '\0'; j++)
            frames[frameIndex].text[j] = inputMessage[i++];
        frameIndex++;
    }

    printf("\nAfter assigning sequence numbers:\n");
    for (i = 0; i < frameIndex; i++)
        printf("%d:%s ", frames[i].sequenceNumber, frames[i].text);

    return frameIndex;  // Return total number of frames created
}

// Utility to generate an array of unique random indices for shuffling
void generateRandomArray(int *randomArray, int limit) {
    int r, i = 0, j;
    while (i < limit) {
        r = rand() % limit;
        for (j = 0; j < i; j++)
            if (randomArray[j] == r)
                break;
        if (i == j)  // r is unique
            randomArray[i++] = r;
    }
}

// Shuffle the frames based on random sequence numbers
void shuffleFrames(int totalFrames) {
    int randomIndices[totalFrames];
    generateRandomArray(randomIndices, totalFrames);

    for (int i = 0; i < totalFrames; i++)
        shuffledFrames[i] = frames[randomIndices[i]];

    printf("\n\nAfter shuffling:\n");
    for (int i = 0; i < totalFrames; i++)
        printf("%d:%s ", shuffledFrames[i].sequenceNumber, shuffledFrames[i].text);
}

// Sort the shuffled frames based on their original sequence numbers
void sortFrames(int totalFrames) {
    int i, j, swapped;
    struct Frame temp;

    for (i = 0; i < totalFrames - 1; i++) {
        swapped = 0;
        for (j = 0; j < totalFrames - i - 1; j++) {
            if (shuffledFrames[j].sequenceNumber > shuffledFrames[j + 1].sequenceNumber) {
                temp = shuffledFrames[j];
                shuffledFrames[j] = shuffledFrames[j + 1];
                shuffledFrames[j + 1] = temp;
                swapped = 1;
            }
        }
        if (!swapped) break; // Optimization: Break if already sorted
    }
}

int main() {
    int totalFrames;

    printf("Enter the message: ");
    fgets(inputMessage, sizeof(inputMessage), stdin);
    
    // Remove newline character if present
    size_t len = strlen(inputMessage);
    if (len > 0 && inputMessage[len - 1] == '\n')
        inputMessage[len - 1] = '\0';

    // Split into frames, shuffle them, then sort to recover original message
    totalFrames = assignSequenceNumbers();
    shuffleFrames(totalFrames);
    sortFrames(totalFrames);

    // Display the final reconstructed message
    printf("\n\nAfter sorting (Reconstructed message):\n");
    for (int i = 0; i < totalFrames; i++)
        printf("%s", shuffledFrames[i].text);
    printf("\n\n");

    return 0;
}


Output:


$ gcc frame_sorting.c
$ ./a.out
Enter the message: Study Glance

After assigning sequence numbers:
0:Stu 1:dy  2:Gla 3:nce

After shuffling:
3:nce 2:Gla 1:dy  0:Stu

After sorting (Reconstructed message):
Study Glance




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