[go: up one dir, main page]

0% found this document useful (0 votes)
40 views8 pages

Compuner Network Programs

The document contains multiple C programs that implement various networking concepts, including CRC code generation and verification, a sliding window protocol for flow control, a broadcast tree for subnet hosts, a distance vector routing algorithm, data encryption and decryption, and congestion control using the leaky bucket algorithm. Each program includes user input for parameters and outputs the results based on the implemented algorithm. The programs demonstrate fundamental networking principles and techniques in C programming.

Uploaded by

Debjit Dutta
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)
40 views8 pages

Compuner Network Programs

The document contains multiple C programs that implement various networking concepts, including CRC code generation and verification, a sliding window protocol for flow control, a broadcast tree for subnet hosts, a distance vector routing algorithm, data encryption and decryption, and congestion control using the leaky bucket algorithm. Each program includes user input for parameters and outputs the results based on the implemented algorithm. The programs demonstrate fundamental networking principles and techniques in C programming.

Uploaded by

Debjit Dutta
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/ 8

Write a program to Generate CRC code for a given data frame

#include<stdio.h>
#include<math.h>
main()
{
int i,j,k,m,n,cl;
char a[10],b[100],c[100];
clrscr();
printf("\n ENTER POLYNANOMIAL:");
scanf("%s",a);
printf("\n ENTER THE FRAME:");
scanf("%s",b);
m=strlen(a);
n=strlen(b);
for(i=0;i<m;i++) /* To eliminat first zeros in polynomial */
{
if(a[i]=='1')
{
m=m-i;
break;
}
Computer Networks Lab Manual
10
}
for(k=0;k<m;k++) /* To Adjust the polynomial */
a[k]=a[k+i];
cl=m+n-1;
for(i=0;i<n;i++) /* To copy the original frame to c[]*/
c[i]=b[i];
for(i=n;i<cl;i++) /* To add n-1 zeros at the end of frame */
c[i]='0';
c[i]='\0'; /*To make it as a string */
for(i=0;i<n;i++) /* To set polynomial remainder at end of c[]*/
if(c[i]=='1')
{
for(j=i,k=0;k<m;k++,j++)
if(a[k]==c[j])
c[j]='0';
else
c[j]='1';
}
for(i=0;i<n;i++) /* To copy original data in c[] */
c[i]=b[i];
printf("\n THE MESSAGE IS: %s",c);
getch();
}

Write a program to Verify the CRC code for a given data frame

#include<stdio.h>
#include<math.h>
main()
{
int i,j,k,m,n,cl;
char a[10],c[100];
clrscr();
printf("\n ENTER POLYNANOMIAL:");
scanf("%s",a);
printf("\n ENTER THE CRC FRAME:");
scanf("%s",c);
m=strlen(a);
cl=strlen(c);
for(i=0;i<m;i++) /* To eliminat first zeros in polynomial */
{
if(a[i]=='1')
{ m=m-i; break; }
}
for(k=0;k<m;k++) /* To Adjust the polynomial */
a[k]=a[k+i];
n=cl-m+1;
for(i=0;i<n;i++) /* To check polynomial remainder is zero or not */
if(c[i]=='1')
{
for(j=i,k=0;k<m;k++,j++)
if(a[k]==c[j])
c[j]='0';
else
c[j]='1';
}
for(i=0;i<cl;i++) /* To copy original data in c[] */
if(c[i]=='1')
{
printf("\n THERE IS SOME ERROR IN MESSAGE :");
break;
}
if(i==cl)
printf("\n MESSAGE IS CORRECT");
getch();
}

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.

#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 %d frames: “,f);
for(i=1;i<=f;i++)
scanf(“%d”,&frames[i]);
printf(“\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames) \n\n”);
printf(“After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n”,w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf(“%d\n”,frames[i]);
printf(“Acknowledgement of above frames sent is received by
sender\n\n”);
}
else
printf(“%d “,frames[i]);
}
if(f%w!=0)
printf(“\nAcknowledgement of above frames sent is received by
sender\n”);
return 0;
}

Write a C program to Implement broadcast tree for a given subnet of hosts


#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0;
int t[50][2],i,j;
int parent [50], edge [50][50];
main ()
{
printf(“\n Enter the number of nodes”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“%c\t”,65+i);
parent[i]=-1;
}
printf(“\n”);
for(i=0;i<n;i++)
{
printf(“%c”,65+i);
for(j=0;j<n;j++)
scanf(“%d”,&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j]; u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf(“Minimum cost is %d\n Minimum spanning tree is\n”,mincost); for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf(“%c %c %d”, 65+t[i][0], 65+t[i][1], edge[t[i][0]][t[i][1]]);
printf(“\n”);
}
}
sunion(int l,int m)
{
parent[l]=m;
}
find(int l)
{
if(parent[l]>0) l=parent[l];
return l;
}

Write a C program to Implement distance vector routing algorithm for obtaining routing
tables at each node
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}
rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf(“\nEnter the number of nodes : “);
scanf(“%d”,&n);
printf(“Enter the cost matrix :\n”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf(“%d”,&dmat[i][j]);
dmat[i][i]=0;
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(“\nState value for router %d is \n”,i+1);
for(j=0;j<n;j++)
{
printf(“\nnode %d via %d Distance%d”,j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf(“\n”);
}

Write a program to Implement data encryption and data decryption.


#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char pwd[20];
char alpha[26]=”abcdefghijklmnopqrstuvwxyz”;
int num[20],i,n,key;
printf(“\nEnter the password:”);
scanf(“%s”,&pwd);
n=strlen(pwd);
for(i=0;i<n;i++)
num[i]=toascii(tolower(pwd[i]))-‘a’;
printf(“\nEnter the key:”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
num[i]=(num[i]+key)%26;
for(i=0;i<n;i++)
pwd[i]=alpha[num[i]];
printf(“\nThe key is:%d”,key);
printf(“\nEncrypted text is:%s”,pwd);
for(i=0;i<n;i++)
{
num[i]=(num[i]-key)%26;
if(num[i]<0)
num[i]=26+num[i];
pwd[i]=alpha[num[i]];
}
printf(“\nDecrypted text is:%s”,pwd);
}

write a program for congestion control using leaky bucket algorithm


#include<stdio.h>

#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
intrand(int a)
{
intrn = (random() % 10) % a;
returnrn == 0 ? 1 :rn;
}
intmain()
{
intpacket_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf(“\npacket[%d]:%d bytes\t”, i, packet_sz[i]);
printf(“\nEnter the Output rate:”);
scanf(“%d”, &o_rate);
printf(“Enter the Bucket Size:”);
scanf(“%d”, &b_size);
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (packet_sz[i] + p_sz_rm) >b_size)
if(packet_sz[i] >b_size)/*compare the packet siz with bucket size*/
printf(“\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-PACKET
REJECTED”, packet_sz[i], b_size);
else
printf(“\n\nBucket capacity exceeded-PACKETS REJECTED!!”); else
{
p_sz_rm += packet_sz[i];
printf(“\n\nIncoming Packet size: %d”, packet_sz[i]); printf(“\nBytes remaining to Transmit: %d”,
p_sz_rm); p_time = rand(4) * 10;
printf(“\nTime left for transmission: %d units”, p_time); for(clk = 10; clk<= p_time; clk += 10)
{
sleep(1); if(p_sz_rm)
{
if(p_sz_rm<= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0; else
op = o_rate, p_sz_rm -= o_rate; printf(“\nPacket of size %d Transmitted”, op);
printf(“—- Bytes Remaining to Transmit: %d”, p_sz_rm);
}
else
{
printf(“\nTime left for transmission: %d units”, p_time-clk); printf(“\nNo packets to transmit!!”);
}
}
}
}
}

You might also like