0% found this document useful (0 votes)
36 views17 pages

CN - PART B

The document discusses implementation of various data link layer protocols. It includes programs for bit stuffing, character stuffing, distance vector routing, Dijkstra's algorithm, CRC codes, stop and wait protocol and sliding window protocol.

Uploaded by

kushkruthik555
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)
36 views17 pages

CN - PART B

The document discusses implementation of various data link layer protocols. It includes programs for bit stuffing, character stuffing, distance vector routing, Dijkstra's algorithm, CRC codes, stop and wait protocol and sliding window protocol.

Uploaded by

kushkruthik555
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/ 17

PART__B

1. Write the code for

i) bit stuffing

#include<stdio.h>

int main() {

int a[15];

int i, j, k, n, c = 0, pos = 0;

// Input the number of bits

printf("\n Enter the number of bits: ");

scanf("%d", &n);

// Input the bits

printf("\n Enter the bits: ");

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

// Perform bit stuffing

for (i = 0; i < n; i++) {

if (a[i] == 1) {

c++;

if (c == 5) {

pos = i + 1;

c = 0;

for (j = n; j >= pos; j--) {

k = j + 1;
a[k] = a[j];

a[pos] = 0;

n = n + 1;

} else {

c = 0;

// Print the stuffed data

printf("\n DATA AFTER STUFFING \n");

printf(" 01111110 ");

for (i = 0; i < n; i++) {

printf("%d", a[i]);

printf(" 01111110 ");

getchar(); // Pause the console window

return 0;

/*o/p:

Enter the number of bits: 9

Enter the bits: 1 0 1 1 1 1 1 0 1

DATA AFTER STUFFING

01111110 1011111001 01111110

*/

________________________________________
ii) Character stuffing:

#include<stdio.h>

#include<string.h>

void main()

int i=0,j=0,n,pos;

char a[20],b[50],ch;

printf("Enter the Characters: ");

scanf("%s",a);

printf("\nOrginal Data:%s",a);

n=strlen(a);

b[0]='d';

b[1]='l';

b[2]='e';

b[3]='s';

b[4]='t';

b[5]='x';

j=6;

while(i<n)

if( a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')

Computer Networks Laboratory [18ECL76]

Department of Electronics & Communication Engineering 6th Semester

b[j]='d';

b[j+1]='l';

b[j+2]='e';
j=j+3;

b[j]=a[i];

i++;

j++;

b[j]='d';

b[j+1]='l';

b[j+2]='e';

b[j+3]='e';

b[j+4]='t';

b[j+5]='x';

b[j+6]='\0';

printf("\nTransmitted Data: %s\n",b);

printf("Received Data:%s",a);

/*o/p:

Enter the Characters: abcdefg

Orginal Data:abcdefg

Transmitted Data: dlestxabcdefgdleetx

Received Data:abcdefg

*/

_____________________________________________________________________________________
______
2. Write a program for distance vector algorithm to find suitable path for
transmission.
#include <stdio.h>

struct node {

unsigned dist[20];

unsigned from[20];

} rt[10];

int main() {

int dmat[20][20], n, i, j, k, count = 0;

printf("\nEnter the number of nodes: ");

scanf("%d", &n);

printf("\nEnter the cost matrix:\n");

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

scanf("%d", &dmat[i][j]);

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

rt[i].dist[j] = dmat[i][j], rt[i].from[j] = j;

do {

count = 0;

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

for (k = 0; k < n; k++)

if (rt[i].dist[j] > dmat[i][k] + rt[k].dist[j])

rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j], rt[i].from[j] = k, count++;

} while (count != 0);

for (i = 0; i < n; i++) {

printf("\n\nState value for router %d is \n", i + 1);

for (j = 0; j < n; j++)


printf("\t\nNode %d via %d Distance %d", j + 1, rt[i].from[j] + 1, rt[i].dist[j]);

printf("\n\n");

return 0;

/*o/p:

Enter the number of nodes: 3

Enter the cost matrix:

045

103

890

State value for router 1 is

Node 1 via 1 Distance 0

Node 2 via 2 Distance 4

Node 3 via 3 Distance 5

State value for router 2 is

Node 1 via 1 Distance 1

Node 2 via 2 Distance 0

Node 3 via 3 Distance 3

State value for router 3 is

Node 1 via 1 Distance 8

Node 2 via 2 Distance 9

Node 3 via 3 Distance 0


*/___________________________________________________________________________________
__________________

3. Implement Dijkstra’s algorithm to compute the shortest routing path.

#include <stdio.h>

#define MAX_NODES 15

int n, s, nb, nbs[MAX_NODES], snbs[MAX_NODES], delay[MAX_NODES][MAX_NODES], i, j,


temp[MAX_NODES], ze = 0;

void min();

int main() {

printf("Enter the number of nodes: ");

scanf("%d", &n);

printf("\nEnter the source node: ");

scanf("%d", &s);

printf("\nEnter the number of neighbors to %d: ", s);

scanf("%d", &nb);

printf("\nEnter the neighbors: ");

for (i = 1; i <= nb; i++)

scanf("%d", &nbs[i]);

printf("\nEnter the time delay from source to neighbors: ");

for (i = 1; i <= nb; i++)

scanf("%d", &snbs[i]);

printf("\nEnter the time delays for neighbors:\n");

for (i = 1; i <= nb; i++) {

printf("Enter the time delays from %d: ", nbs[i]);

for (j = 1; j <= n; j++)


scanf("%d", &delay[i][j]);

printf("\nThe time delays for neighbors:\n");

for (i = 1; i <= nb; i++) {

printf("Time delays from %d: ", nbs[i]);

for (j = 1; j <= n; j++)

printf("%3d ", delay[i][j]);

printf("\n");

min();

return 0;

void min() {

int sum, k, y = 1, store = 1;

printf("\n\t\t\tNew Route");

printf("\n\t\t\tTime Delay");

printf("\n");

for (i = 1; i <= n; i++) {

sum = 0;

k = 1;

for (j = 1; j <= nb; j++) {

temp[k++] = delay[j][i];

sum = temp[1] + snbs[1];

for (y = 2; y <= nb; y++) {

if (sum > temp[y] + snbs[y]) {

sum = temp[y] + snbs[y];

store = y;
}

if (s == i) {

printf("\n\t%d +\t%d =\t%d --", ze, ze, ze);

} else {

printf("\n\t%d +\t%d =\t%d\t%d", temp[store], snbs[store], sum, nbs[store]);

/*o/p:

Enter the number of nodes: 3

Enter the source node: 0

Enter the number of neighbors to 0: 2

Enter the neighbors: 1 2

Enter the time delay from source to neighbors: 10 20

Enter the time delays for neighbors:

Enter the time delays from 1: 4 8 12

Enter the time delays from 2: 3 6 9

The time delays for neighbors:

Time delays from 1: 4 8 12

Time delays from 2: 3 6 9

New Route

Time Delay

4+ 10 = 14 1

8+ 10 = 18 1

12 + 10 = 22 1
*/

_____________________________________________________________________________________
________________

4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the
program for the cases
a. Without error
b. With error
#include<stdio.h>

#include<string.h>

#define N strlen(g)

char t[28],cs[28],g[]="10001000000100001";

int a,i,j;

void xor()

for(j = 1;j < N; j++)

cs[j] = (( cs[j] == g[j])?'0':'1');

void crc()

for(i=0;i<N;i++)

cs[i]=t[i];

do

if(cs[0]=='1')

xor();

for(j=0;j<N-1;j++)

cs[j]=cs[j+1];

cs[j]=t[i++];
}

while(i<=a+N-1);

int main()

printf("\nEnter data : ");

scanf("%s",t);

printf("\nGeneratng polynomial : %s",g);

a=strlen(t);

for(i=a;i<a+N-1;i++)

t[i]='0';

printf("\nModified data is : %s",t);

crc();

printf("\nChecksum is : %s",cs);

for(i=a;i<a+N-1;i++) t[i]=cs[i-a];

printf("\nFinal codeword is : %s",t);

printf("\nEnter received message ");

scanf("%s",t);

crc();

for(i=0;(i<N-1) && (cs[i]!='1');i++);

if(i<N-1)

printf("\nError detected\n\n");

else

printf("\nNo error detected\n\n");

return 0;

}
/*op:

Enter data : 1011101

Generatng polynomial : 10001000000100001

Modified data is : 10111010000000000000000

Checksum is : 1000101101011000

Final codeword is : 10111011000101101011000

Enter received message 10111001100010110101000

Error detected

Enter data : 1011101

Generatng polynomial : 10001000000100001

Modified data is : 10111010000000000000000

Checksum is : 1000101101011000

Final codeword is : 10111011000101101011000

Enter received message 10111011000101101011000

No error detected*/

_____________________________________________________________________________________
_____________________

5. Implementation of Stop and Wait Protocol and Sliding Window Protocol

i) Program: Stop and Wait Protocol

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> // For sleep function

int main() {

int i, f, frames[50];
printf("\nEnter number of frames to transmit: ");

scanf("%d", &f);

printf("\nEnter the %d frames: ", f);

for (i = 1; i <= f; i++)

scanf("%d", &frames[i]);

for (i = 1; i <= f; i++) {

if ((rand() % 2) == 1) {

printf("%d\n", frames[i]);

printf("Acknowledgement of above frames sent is received by sender\n\n");

else {

sleep(3);

printf("Negative acknowledgement: Resend %d frame\n", i);

i = i - 1; // Resend the same frame

sleep(1);

return 0;

/*Output:

Enter number of frames to transmit: 6

Enter the 6 frames: 1 4 5 7 8 9

Acknowledgement of above frames sent is received by sender

Negative acknowledgement: Resend 2 frame

4
Acknowledgement of above frames sent is received by sender

Acknowledgement of above frames sent is received by sender

Acknowledgement of above frames sent is received by sender

Acknowledgement of above frames sent is received by sender

Negative acknowledgement: Resend 6 frame

Negative acknowledgement: Resend 6 frame

Acknowledgement of above frames sent is received by sender

*/

______________________________________

ii) Program: Sliding Window Protocol

#include <stdio.h>

int main() {

int w, i, f, frames[50];

printf("Enter Window Size: ");

scanf("%d", &w);

printf("\nEnter number of frames to transmit: ");

scanf("%d", &f);

printf("\nEnter the %d frames: ", f);

for (i = 1; i <= f; i++)


scanf("%d", &frames[i]);

for (i = 1; i <= f; i++) {

if (i % w == 0) {

printf("%d\n", frames[i]);

printf("Acknowledgment of above frames sent is received by sender\n\n");

else {

printf("%d\n", frames[i]);

if (f % w != 0)

printf("Acknowledgment of above frames sent is received by sender\n\n");

return 0;

/*Output:

Enter Window Size: 3

Enter number of frames to transmit: 5

Enter the 5 frames: 12 20 87 65 4

12

20

87

Acknowledgment of above frames sent is received by sender

65

Acknowledgment of above frames sent is received by sender

_____________________________________________________________________________________
6. Write a program for congestion control using leaky bucket algorithm.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> // For sleep function

int bucket_size;

void bucket_input(int pkt_sz, int op_rt) {

if (pkt_sz > bucket_size) {

printf("\n\n Bucket Overflow\n");

else {

sleep(1);

while (pkt_sz > op_rt) {

printf("\n %d bytes outputted", op_rt);

pkt_sz -= op_rt;

sleep(1);

if (pkt_sz > 0) {

printf("\n Last %d bytes sent\n", pkt_sz);

printf("\n Bucket output Successful \n");

int main() {

int i, op_rate, packet_size;

printf("\n Enter Bucket Size: ");

scanf("%d", &bucket_size);

printf("\n Enter Output rate: ");

scanf("%d", &op_rate);

for (i = 1; i <= 5; i++) {


sleep(1);

packet_size = rand() % 1000; // Using rand() for random packet sizes

printf("\n Packet number [%d] \t Packet size=%d", i, packet_size);

bucket_input(packet_size, op_rate);

return 0;

/*Ouput:

Enter Bucket Size: 500

Enter Output rate: 80

Packet number [1] Packet size=383

80 bytes outputted

80 bytes outputted

80 bytes outputted

80 bytes outputted

Last 63 bytes sent

Bucket output Successful

Packet number [2] Packet size=886

Bucket Overflow

Packet number [3] Packet size=777

Bucket Overflow

Packet number [4] Packet size=915

Bucket Overflow

Packet number [5] Packet size=793

Bucket Overflow

*/

-----------------------------------x-----------------------------x------------------------------x-----------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy