Computer Networks Lab
Computer Networks Lab
1. BIT STUFFING:
PROGRAM:
#include<stdio.h>
#include<string.h>
void main(){
int a[20],b[30],i,j,k,count,n;
printf("enter frame length: ");
scanf("%d",&n);
printf("enter input frame(0s and 1s only): ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;j=0;count=1;
while(i<n){
if(a[i]==1){
b[j]=a[i];
for(k=i+1;a[k]==1&&k<n&&count<5;k++){
j++;
b[j]=a[k];
count++;
if(count==5){
j++;
b[j]=0;
}
i=k;
}
}
else
b[j]=a[i];
i++;
j++;
}
printf("after stuffing the frame is: ");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
OUTPUT:
Enter frame length: 7
Enter input frame(0s and 1s only):
1
1
1
1
1
1
0
1
After stuffing the frame: 11111001
2. CHARACTER STUFFING :
PROGRAM:
#include<stdio.h>
#include<string.h>
void main(){
char data[30],stuffed[30]=" ";
char t[3],x[3],s[3];
char esc[1];
int i;
printf("enter characters: ");
scanf("%s",data);
printf("enter escape character: ");
scanf("%s",esc);
x[0]=s[0]=s[1]=esc[0];
x[1]=s[2]='\0';
strcat(stuffed,x);
for(i=0;i<strlen(data);i++){
t[0]=data[i];
t[1]='\0';
if(t[0]==esc[0])
strcat(stuffed,s);
else
strcat(stuffed,t);
}
strcat(stuffed,x);
printf("\n after stuffing: %s",stuffed);
getch();
}
OUTPUT:
2
3. CRC ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
char data[30],check_value[30],gen_poly[20];
int data_length,N,i,j;
void XOR(){
for(j=1;j<N;j++)
check_value[j]=((check_value[j]==gen_poly[j])?'0':'1');
}
void crc(){
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
if(check_value[0]=='1')
XOR();
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
check_value[j]=data[i++];
}while(i<=data_length+N-1);
}
int main(){
printf("\nEnter data to be transmitted: ");
scanf("%s",data);
printf("\nEnter the generating polynomial: ");
scanf("%s",gen_poly);
data_length=strlen(data);
N=strlen(gen_poly);
for(i=data_length;i<data_length+N-1;i++)
data[i]='0';
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length];
printf("\nData to be sent:%s",data);
3
return 0;
OUTPUT:
Data to be sent:1010100000111000010101100111100
4. GO BACK N PROTOCOL:
PROGRAM:
#include<stdio.h>
int main(){
int ws,sent=0,ack,i,f;
printf("enter window size:");
scanf("%d",&ws);
printf("\nEnter frame size:");
scanf("%d",&f);
while(1){
for(i=0;i<ws;i++){
printf("\ntransmitting %d",sent);
sent++;
if(sent==f)
break;
}
printf("\nenter last ack recieved :");
scanf("%d",&ack);
if(ack==f-1)
break;
else
sent=ack;
}
}
OUPUT:
enter window size:3
4
transmitting 0
transmitting 1
transmitting 2
enter last ack recieved :1
transmitting 1
transmitting 2
transmitting 3
enter last ack recieved :3
PROGRAM:
#include<stdio.h>
int main(){
int ws,sent=0,ack,i,f;
printf("enter window size:");
scanf("%d",&ws);
printf("\nEnter frame size:");
scanf("%d",&f);
int frames[f];
for(i=0;i<ws;i++){
printf("\nsending frame %d",sent);
sent++;}
for(i=0;i<ws;i++){
printf("\nEnter acknowledgment for frame %d",i);
scanf("%d",&frames[i]);}
while(1){
if(sent==f)
break;
i=sent-ws;
while(i<sent){
if(frames[i]==0){
printf("sending frame %d\n",i);
printf("\nEnter acknowledgment for frame %d",i);
scanf("%d",&frames[i]);
i++;
break;
}
i++;
5
}
if(i==sent){
printf("sending frame %d\n",i);
printf("\nEnter acknowledgment for frame %d",i);
scanf("%d",&frames[i]);
sent++;
}
}
return 0;
}
OUTPUT:
enter window size:4
Enter frame size:8
sending frame 0
sending frame 1
sending frame 2
sending frame 3
Enter acknowledgment for frame 0:1
6. DIJKSTRA’S ALGORITHM:
PROGRAM:
6
#include<stdio.h>
int minimum(int a,int b){
return a<b?a:b;
}
int main(){
int v,i,j;
printf("\nEnter no of vertices");
scanf("%d",&v);
int m[v][v];
for(i=0;i<v;i++){
for(j=0;j<v;j++)
scanf("%d",&m[i][j]);
}
int dist_m[v];
int visited[v];
for(i=0;i<v;i++){
dist_m[i]=999;
visited[i]=0;
}
int starting_v;
printf("please enter the starting vertex:\n");
scanf("%d",&starting_v);
visited[starting_v]=1;
int selected=starting_v;
for(i=0;i<v;i++)
dist_m[i]=m[selected][i];
for(i=0;i<v;i++){
int min=999,min_index;
for(j=0;j<v;j++){
if(dist_m[j]<min&&visited[j]==0){
min=dist_m[j];
min_index=j;
}
}
selected=min_index;
visited[selected]=1;
//min=999;
for(j=0;j<v;j++)
dist_m[j]=minimum(dist_m[j],(dist_m[selected]+m[selected][j]));
}
7
for(i=0;i<v;i++)
printf("%d",dist_m[i]);
return 0;
}
OUTPUT:
Enter no of vertices4
0
2
5
4
2
0
3
7
5
3
0
3
4
7
3
0
please enter the starting vertex:
1
the distance matrix is:
2036
PROGRAM:
#include<stdio.h>
struct node{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main(){
int costmat[20][20];
int n,i,j,k,count=0;
printf("enter the no of nodes:");
scanf("%d",&n);
printf("\nEnter the cost matrix:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
8
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[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]>rt[i].dist[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\nFor router %d\n",i+1);
for(j=0;j<n;j++)
printf("\t\nnode %d via %d Distance %d
\n\n",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
OUTPUT:
9
999
0
999
1
999
999
4
999
0
6
999
999
999
1
6
0
2
999
999
999
999
2
0
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 5
node 4 via 2 Distance 6
node 5 via 3 Distance 6
node 6 via 5 Distance 8
For router 2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 1 Distance 7
node 4 via 4 Distance 4
node 5 via 1 Distance 8
node 6 via 1 Distance 10
For router 3
node 1 via 1 Distance 5
node 2 via 1 Distance 7
node 3 via 3 Distance 0
node 4 via 5 Distance 7
node 5 via 5 Distance 1
node 6 via 5 Distance 3
10
For router 4
node 1 via 2 Distance 6
node 2 via 2 Distance 4
node 3 via 5 Distance 7
node 4 via 4 Distance 0
node 5 via 5 Distance 6
node 6 via 5 Distance 8
For router 5
node 1 via 3 Distance 6
node 2 via 1 Distance 8
node 3 via 3 Distance 1
node 4 via 4 Distance 6
node 5 via 5 Distance 0
node 6 via 6 Distance 2
For router 6
node 1 via 5 Distance 8
node 2 via 1 Distance 10
node 3 via 5 Distance 3
node 4 via 5 Distance 8
node 5 via 5 Distance 2
node 6 via 6 Distance 0
8. SUBNET OF HOSTS:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
int main(){
printf("Enter the no of vertices: ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix:\n");
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=9999;
}
}
printf("the edges of broadcast tree are \n");
while(ne<n){
for(i=1,min=9999;i<=n;i++){
11
for(j=1;j<=n;j++){
if(cost[i][j]<min){
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v)){
printf("%d edge (%d,%d) = %d\n",ne++,a,b,min);
mincost+=min;
}
cost[a][b]=cost[b][a]=9999;
}
printf("minimum cost=%d\n",mincost);
return 0;
}
int find(int i){
while(parent[i])
i=parent[i];
return i;
}
OUTPUT:
12
0
0
5
0
0
0
0
0
4
0
0
0
0
0
the edges of broadcast tree are
1 edge (1,3) = 2
2 edge (2,3) = 2
3 edge (4,5) = 4
4 edge (3,4) = 5
minimum cost=13
PROGRAM:
#include<stdio.h>
int main(){
int b_s;
printf("enter bucket size:\n");
scanf("%d",&b_s);
int flowrate=200;
int input,bucket=0,packet_loss=0,i=1;
do{
printf("****Iteration %d****\n",i);
i++;
printf("enter no of input packets:\n");
scanf("%d",&input);
if(bucket+input>b_s){
packet_loss=((bucket+input)-b_s);
bucket=b_s;
}
else{
bucket+=input;
packet_loss=0;
13
}
if(bucket<flowrate){
printf("Outflow packet:%d\n",bucket);
bucket=0;
printf("packets lost:%d\n",packet_loss);
printf("packets in bucket:%d\n",bucket);
}
else{
printf("Outflow packet:%d\n",bucket);
bucket-=flowrate;
printf("packets lost:%d\n",packet_loss);
printf("packets in bucket:%d\n",bucket);
}
}while(bucket>=0);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
int main()
{
int i, x;
char str[100];
14
printf("\nPlease choose 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 < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value
case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value
default:
printf("\nError\n");
}
return 0;
}
OUTPUT:
15