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

CN Lab Internal

Cn lab internal imp programs

Uploaded by

sakshiravte4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

CN Lab Internal

Cn lab internal imp programs

Uploaded by

sakshiravte4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Bit Stuffing

#include<stdio.h>
int main()
{
int a[50],b[100],i,j,n,count;
printf("\nEnter the frame size:");
scanf("%d",&n);
printf("\nEnter the data message with 0's and 1's only:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nActual data message is:");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
j=0;
count=0;
for(i=0;i<n;i++)
{
b[j++]=a[i];
if(a[i]==1)
{
count++;
if(count==5)
{
b[j++]=0;
count=0;
}
}
else
count=0;
}
printf("\n");
printf("\nData message after bit stuffing:");
for(i=0;i<j;i++)
{
printf("%d",b[i]);
}
}

OUTPUT:

Enter the frame size:8


Enter the data message with 0's and 1's only:1 0 1 1 1 1 1 1
Actual data message is:10111111
Data message after bit stuffing:101111101
2. Byte Stuffing

#include<stdio.h>
#include<string.h>
void main()
{
char frame[50][50], str[50][50];
char flag[10];
strcpy(flag,"flag");
char esc[10];
strcpy(esc,"esc");
int i,j,n;
int k=0;
strcpy(frame[k++],"flag");
printf("\nEnter the length of string:");
scanf("%d",&n);
printf("\nEnter the string:");
for(i=0;i<=n;i++)
{
gets(str[i]);
}
printf("\nYou entered:\n");
for(i=0;i<=n;i++)
{
puts(str[i]);
}
printf("\n");
for(i=1;i<=n;i++)
{
if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0)
{
strcpy(frame[k++],str[i]);
}
else
{
strcpy(frame[k++],"esc");
strcpy(frame[k++],str[i]);
}
}
strcpy(frame[k++],"flag");
printf("\n--------------------------------------------------------------\n\
n");
printf("Byte stuffing at sender side:");
printf("\n--------------------------------------------------------------\n\
n");
for(i=0;i<k;i++)
{
printf("%s\t",frame[i]);
}
}

OUTPUT:

Enter the length of string:5


Enter the string: A
flag
esc
flag
B
You entered:

A
flag
esc
flag
B
--------------------------------------------------------------
Byte stuffing at sender side:
--------------------------------------------------------------
flag A esc flag esc esc esc flag B flag
3. GO Back-N

#include<stdio.h>
#include<stdlib.h>
void main()
{
int x=0,N,fr[10],i=1,j,nf,tr=0;
printf("Enter the size of window:");
scanf("%d",&N);
printf("Enter no of frames:");
scanf("%d",&nf);
while(i<=nf)
{
int x=0;
for(int j=i;j<i+N && j<=nf;j++)
{
printf("\n Sent Frame %d",j);
}
for(int j=i;j<i+N && j<=nf;j++)
{
int flag = rand()%2;
if(!flag)
{
printf("\n Acknowledgment for Frame %d",j);
x++;
}
else
{ printf("\n Frame %d Not Received",j);
printf("\n Retransmitting Window");
break;
}
}
i+=x;
}
}

OUTPUT:

Enter the size of window:3


Enter no of frames:4

Sent Frame 1
Sent Frame 2
Sent Frame 3
Frame 1 Not Received
Retransmitting Window
Sent Frame 1
Sent Frame 2
Sent Frame 3
Frame 1 Not Received
Retransmitting Window
Sent Frame 1
Sent Frame 2
Sent Frame 3
Acknowledgment for Frame 1
Acknowledgment for Frame 2
Frame 3 Not Received
Retransmitting Window
Sent Frame 3
Sent Frame 4
Acknowledgment for Frame 3
Acknowledgment for Frame 4
4. Distance Vector Routing (DVR)

#include<stdio.h>
struct node{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main(){
int n,a[20][20],i,j,k,c=0;
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("Enter adjacency matrix:");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
rt[i].dist[j]=a[i][j];
rt[i].from[j]=j;
}
}
do{
c=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
for(k=0;k<n;k++)
if(rt[i].dist[j]>a[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
c++;
}
}
}
}
while(c!=0);
for(i=0;i<n;i++){
printf("\nRouter %d",i+1);
for(j=0;j<n;j++)
printf("\t\n node is %d via %d distance %d",j+1,rt[i].from[j]
+1,rt[i].dist[j]);
}
printf("\n");
}

OUTPUT:
Enter no.of nodes:4
Enter adjacency matrix:
0 8 10 5
8 0 2 13
10 2 0 3
5 13 3 0

Router 1
node is 1 via 1 distance 0
node is 2 via 2 distance 8
node is 3 via 4 distance 8
node is 4 via 4 distance 5
Router 2
node is 1 via 1 distance 8
node is 2 via 2 distance 0
node is 3 via 3 distance 2
node is 4 via 3 distance 5
Router 3
node is 1 via 4 distance 8
node is 2 via 2 distance 2
node is 3 via 3 distance 0
node is 4 via 4 distance 3
Router 4
node is 1 via 1 distance 5
node is 2 via 3 distance 5
node is 3 via 3 distance 3
node is 4 via 4 distance 0
5. Frame Sorting

#include <stdio.h>
#define MAX_FRAMES 100
void bubbleSort(int frames[], int contents[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (frames[j] > frames[j + 1]) {
int tempFrame = frames[j];
frames[j] = frames[j + 1];
frames[j + 1] = tempFrame;
int tempContent = contents[j];
contents[j] = contents[j + 1];
contents[j + 1] = tempContent;
}
}
}
}
int main() {
int frames[MAX_FRAMES];
int contents[MAX_FRAMES];
int n;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter sequence numbers followed by their contents:\
n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &frames[i], &contents[i]);
}
bubbleSort(frames, contents, n);
printf("\nSorted Frame Details:\n");
printf("Sequence number:\tContents of frame: ");
for (int i = 0; i < n; i++) {
printf("\n\t%d \t\t\t %d\n", frames[i], contents[i]);
}
}

OUTPUT:

Enter the number of frames: 4


Enter sequence numbers followed by their contents:
3 9
2 10
4 15
1 5

Sorted Frame Details:


Sequence number: Contents of frame:
1 5

2 10

3 9
4 15

6. Data encryption and decryption

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

int main() {
int i, key, x, l;
char str[100];

printf("Please enter a string:\t");


gets(str);
l=strlen(str);

printf("Enter key: ");


scanf("%d", &key);

printf("\nPlease choose one of the following options:\n");


printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);

switch(x) {
case 1:
for(i = 0; i < l; i++) {
str[i] = str[i] + key;
}
printf("\nEncrypted string: %s\n", str);
break;
case 2:
for(i = 0; i < l; i++) {
str[i] = str[i] - key;
}
printf("\nDecrypted string: %s\n", str);
break;
default:
printf("\nError: Invalid option.\n");
}
return 0;
}

OUTPUT:

Please enter a string: hello


Enter key: 3
Please choose one of the following options:
1 = Encrypt the string.
2 = Decrypt the string.
1
Encrypted string: khoor
--------------------------------------------------------------------------------
Please enter a string: khoor
Enter key: 3
Please choose one of the following options:
1 = Encrypt the string.
2 = Decrypt the string.
2
Decrypted string: hello

7. Leaky Bucket algorithm

#include<stdio.h>
int main()
{
int incoming,outgoing,buck_size,n,store=0;
printf("Enter bucket size,outgoing rate and no. of inputs:");
scanf("%d %d %d",&buck_size,&outgoing,&n);
while(n!=0)
{
printf("\nEnter the incoming packet size:");
scanf("%d",&incoming);
printf("Incoming packet size %d\n",incoming);
if(incoming<=(buck_size-store))
{
store+=incoming;
printf("Bucket buffer size %d out of %d\n",store,buck_size);
}
else
{
printf("Dropped %d no of packets\n",incoming-(buck_size-
store));
store=buck_size;
printf("Bucket buffer size %d out of %d\n",store,buck_size);
}
store=store-outgoing;
printf("After outgoing %d packets left out of %d in buffer\
n",store,buck_size);
n--;
}
}

OUTPUT:

Enter bucket size,outgoing rate and no. of inputs:


15
3
3

Enter the incoming packet size:10


Incoming packet size 10
Bucket buffer size 10 out of 15
After outgoing 7 packets left out of 15 in buffer

Enter the incoming packet size:7


Incoming packet size 7
Bucket buffer size 14 out of 15
After outgoing 11 packets left out of 15 in buffer

Enter the incoming packet size:5


Incoming packet size 5
Dropped 1 no of packets
Bucket buffer size 15 out of 15
After outgoing 12 packets left out of 15 in buffer

You might also like