Menu

Computer Networks - (LAB PROGRAMS)



Notice: Undefined index: title in /home/u681245571/domains/studyglance.in/public_html/labprograms/cndisplay.php on line 89

Aim:

 

Solution :

Week 1

Bit Stuffing :


Source Code:

File Name: bit_stuffing.c

#include<stdio.h>

void main() {
    int ip_frame[100], op_frame[200];
    int i, j = 0, n;
    int count = 0;

    printf("Enter frame length: ");
    scanf("%d", &n);

    printf("Enter input frame (0's and 1's only):\n");
    for(i = 0; i < n; i++) {
        scanf("%d", &ip_frame[i]);
    }

    // Bit stuffing logic
    for(i = 0; i < n; i++) {
        op_frame[j++] = ip_frame[i];

        if(ip_frame[i] == 1) {
            count++;
            if(count == 5) {
                op_frame[j++] = 0;  // Stuff a 0 after five 1s
                count = 0;          // Reset count after stuffing
            }
        } else {
            count = 0;  // Reset count if a 0 is found
        }
    }

    printf("\nAfter bit stuffing, the frame is:\n");
    for(i = 0; i < j; i++) {
        printf("%d", op_frame[i]);
    }
    printf("\n");
}


Output:


$ gcc bit_stuffing.c
$ ./a.out
enter frame length: 9
Enter input frame (0's & 1's only) :
0
1
0
1
1
1
1
1
1

After stuffing, the frame is:
0101111101



Character Stuffing :


Source Code:

File Name: char_stuffing.c

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

void main()
{
    int i = 0, j = 0, n, pos;
    char a[20], b[100], ch;

    printf("Enter the string:\n");
    scanf("%s", a);

    n = strlen(a);

    printf("Enter position to insert the character:\n");
    scanf("%d", &pos);

    // Validate position
    while(pos < 1 || pos > n + 1)
    {
        printf("Invalid position, enter again: ");
        scanf("%d", &pos);
    }

    getchar(); // Clear newline left by previous scanf
    printf("Enter the character to stuff:\n");
    ch = getchar();

    // Add starting frame delimiter
    b[0] = 'd'; b[1] = 'l'; b[2] = 'e'; b[3] = 's'; b[4] = 't'; b[5] = 'x';
    j = 6;

    for (i = 0; i < n; i++)
    {
        // Insert stuffing character at given position
        if (i == pos - 1)
        {
            b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
            b[j++] = ch;
            b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
        }

        // Escape 'dle' sequence
        if (a[i] == 'd' && a[i + 1] == 'l' && a[i + 2] == 'e')
        {
            b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
        }

        b[j++] = a[i];
    }

    // If position is at the end
    if (pos == n + 1)
    {
        b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
        b[j++] = ch;
        b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e';
    }

    // Add ending frame delimiter
    b[j++] = 'd'; b[j++] = 'l'; b[j++] = 'e'; b[j++] = 'e'; b[j++] = 't'; b[j++] = 'x';
    b[j] = '\0';

    printf("\nFrame after stuffing:\n%s\n", b);
}


Output:


$ gcc char_stuffing.c
$ ./a.out
Enter the string:
madhu
Enter position to insert the character:
2
Enter the character to stuff:
k

Frame after stuffing:
dlestxmdlekdleadhudleetx



Character Count :


Source Code:

File Name: char_count.c

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

char data[20][20];
int n;

int main() {
    int i, j, ch;
    char tmp[20][30];  // Stores frames with character count

    printf("Enter the number of frames: ");
    scanf("%d", &n);
    getchar();  // clear newline after scanf

    // Input the frames
    for (i = 1; i <= n; i++) {
        printf("Frame %d: ", i);
        fgets(data[i], sizeof(data[i]), stdin);
        data[i][strcspn(data[i], "\n")] = '\0';  // remove newline
    }

    // Creating frames with count prefix
    for (i = 1; i <= n; i++) {
        int len = strlen(data[i]);
        tmp[i][0] = len + '0';  // store count as char (e.g., '5')
        tmp[i][1] = '\0';
        strcat(tmp[i], data[i]);  // append actual data after count
    }

    // AT THE SENDER
    printf("\n\t\tAT THE SENDER:\n");
    printf("Data as frames:\n");
    for (i = 1; i <= n; i++) {
        printf("Frame %d: ", i);
        puts(tmp[i]);
    }

    printf("Data transmitted: ");
    for (i = 1; i <= n; i++) {
        printf("%s", tmp[i]);
    }

    // AT THE RECEIVER
    printf("\n\n\t\tAT THE RECEIVER:\n");
    printf("The data received: ");

    for (i = 1; i <= n; i++) {
        ch = tmp[i][0] - '0';  // get count
        for (j = 0; j < ch; j++) {
            data[i][j] = tmp[i][j + 1];  // skip count character
        }
        data[i][j] = '\0';  // null terminate
    }

    printf("\nData after removing count characters: ");
    for (i = 1; i <= n; i++) {
        printf("%s", data[i]);
    }

    printf("\n\nData in frame form:\n");
    for (i = 1; i <= n; i++) {
        printf("Frame %d: ", i);
        puts(data[i]);
    }

    return 0;
}


Output:


$ gcc char_count.c
$ ./a.out
Enter the number of frames: 3
Frame 1: Computer
Frame 2: Networks
Frame 3: Lab

                AT THE SENDER:
Data as frames:
Frame 1: 8Computer
Frame 2: 8Networks
Frame 3: 3Lab
Data transmitted: 8Computer8Networks3Lab

                AT THE RECEIVER:
The data received:
Data after removing count characters: ComputerNetworksLab

Data in frame form:
Frame 1: Computer
Frame 2: Networks
Frame 3: Lab


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