Monday, August 20, 2012

Computer Networks
AIM:- Implement the data link layer framing methods such as
Character Stuffing.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0;j=0;n,pos;
char a[20],a[50],ch;
clrscr();
printf(“enter string :”);
scanf(“%s”,&a);
n=strlen[a];
printf(“enter position:”);
scanf(“%d”,&pos);
if(pos>n)
{
printf(“invalid position,enter again”);
scanf(“%d”,&pos);
}
printf(“enter the character”);
KITS ENGG COLLEGE Page No:
Computer Networks
ch=getche()
b[0]=’d’;
b[1]=’l’;
b[2]=’e’;
b[3]=’s’;
b[4]=’t’;
b[5]=’x’;
j=6;
while(i<n)
{
if I = = position-1
{
b[j]=’d’;
b[j+1]=’l’;
b[j=2]=’e’;
b[j+3]=’ch’;
b[j+4]=’d’;
b[j+5]=’l’;
b[j+6]=’e’;
then
j=j+7;
}
KITS ENGG COLLEGE Page No:
Computer Networks
If a[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(“frame after stuffing is:”);
printf(“%s”,b);
getch();
}
KITS ENGG COLLEGE Page No:
Computer Networks
Input:
Enter string : a s d l e f g h
Enter position: 8
invalid position
Enter again 3
Enter the character k
Output:
Frame after stuffing is:
d l e s t x a s d l e k d l e d le e f g h d l e e t x
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:- Implement the data link layer framing methods such as
Bit Stuffing.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,i,j,c,count,a[20],b[30];
clrscr();
KITS ENGG COLLEGE Page No:
Computer Networks
printf(“enter the frame length”);
scanf(“%d”,&n);
printf(“enter input frame is 1s,0s only”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
i=0;
count=1;
j=0;
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;
}
KITS ENGG COLLEGE Page No:
Computer Networks
I=k;
}
}
else
{
b[j]=a[i];
}
I++;
J++;
}
printf(“after stuffing the frame is:”);
for(i=0;i<j;i++)
printf(“%d”,b[j]);
getch();
}
Output:
Enter frame length:10
Enter input frame (0’s,1’s)
1010111111
After stuffing the frame:1010111111
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:-Implement On A Data Set Of Characters the Cyclic
Redundancy Check
PROGRAM:
#include<stdio.h>
KITS ENGG COLLEGE Page No:
Computer Networks
#include<conio.h>
int gen[4],gen1,frl,rem[4];
void main( )
{
int I,j,fr[8],dupfr[11],recfr[11],then,flag;
clrscr( );
flr=8;
gen1=4;
printf(“enter frame:”);
for(i=0;i<fr1;i++)
{
scanf(“%d”,&fr[i]);
dupfr[i]=fr[i];
}
printf(“enter generator;”);
for(i=0;i<gen1;i++)
scanf(“%d”,&gen[i]);
tlen=fr1+genl-1;
for(i=fr1;i<tlen;i++)
{
dupfr[i]=0;
}
KITS ENGG COLLEGE Page No:
Computer Networks
remainder(dupfr);
for(i=0;i<fr1;i++)
{
recfr[i]=fr[i];
}
for(i=fr1;j=1;j<gen1;i++,j++)
{
recfr[i]=rem[j];
}
remainder(recfr);
flag=0;
for(i=0;i<4;i++)
{
if(rem[i]!=0)
flag++;
}
if(flag==0)
{
printf(“frame received correctly”);
}
else
{
KITS ENGG COLLEGE Page No:
Computer Networks
printf(“the received frame is wrong”);
}
getch( );
}
remainder (int fr[])
{
int k,kl,I,j;
for(k=0;k<fr1;k++)
{
if(fr[k]==1)
{
kl=k;
for(i=0,j=k,i<gen1;i++,j++)
{
rem[i]=fr[j]^gen[i];
}
for(i=0;i<gen1;i++)
{
fr[k1]=rem[i];
k1++;
}
}
KITS ENGG COLLEGE Page No:
Computer Networks
}}
Output:
Enter frame 111111111
Enter generator 1101
Frame received correctly
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:- To implement dijkstra’s algorithm to compute the
shortest path.
PROGRAM:-
#include<stdio.h>
main()
{
char ch='y',sp[10],nb[10];
int ds[10],j=0,k,sm,i,n;
clrscr();
printf("enter the source node:");
sp[0]=getche();
j++;
do
{
printf("\nhow many neighbours:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
KITS ENGG COLLEGE Page No:
Computer Networks
printf("enter the neighbour name:");
nb[i]=getche();
printf("\n enter distance:");
scanf("%d",&ds[i]);
}
sm=ds[0];
for(i=1;i<n;i++)
{
if(sm>ds[i])
{
sm=ds[i];k=i;
}
}
sp[j]=nb[k];
j++;
sp[j]='\0';
printf("\n shortest path is:");
for(i=0;sp[i]!='\0';i++)
printf("%c",sp[i]);
printf("\n do u want more neighbours?y/n");
ch=getche();
}while(ch=='y');
KITS ENGG COLLEGE Page No:
Computer Networks
getch();
}
OUTPUT:-
enter source node:A
enter how many neighbours:4
enter neighbor name:B
enter distance:2
enter neighbor name:C
enter distance:3
enter neighbor name:D
enter distance:4
enter neighbor name:E
enter distance:1
shortrest path is:AE
do you want more neighbours?y/n:n
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:-Take A 64 Bit Playing Text And Encrypt The Same Using
DES Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char cipher[50],plain[50],key[50];
int cipher I,lp,lk;
clrscr();
while(1)
KITS ENGG COLLEGE Page No:
Computer Networks
printf(“\n menu”);
printf(“\n 1.data encryption”);
printf(“\n2.decryption;”);
printf(“\n 3.exit”);
printf(“\n enter your choice;”);
scanf(“%d”,&choice);
switch(choice)
case1:
printf(“\n data encryption:”);
printf(“\n encryption text ”);
fflush(stdin);
gets(plain);
printf(“\n enter the encryption key;”);
gets(key);
lp=strlen(plain);
lk=strlen(key);
while(lp==lk)
{
printf(“\n invalid key”);
printf(“\n encryption key”);
gets(key);
lk=strlen(key);
KITS ENGG COLLEGE Page No:
Computer Networks
}
for(i=0;plaini!=’10’;i++)
cipher[i]=plain[i]^key[i];
printf(“\n encryptyed text is :”);
capture[i]=’10’;
puts (capture);
break;
case2:
printf(“\n decryption:”);
fflush(stdin);
printf(“\n enter encrypted key:”);
gets(key);
lp=strlen(plain);
lk=strlen(key);
while(lp===lk)
{
printf(“\n invalid key”);
printf(“\n enter encryption key:”);
gets(key);
lk=strlen(key);
}
for(i=0;capture[i]!=’10’;i++)
KITS ENGG COLLEGE Page No:
Computer Networks
plain[i]=capture[i]^key[i];
printf(“\n decrypted text is:”);
puts(plain);
case3:
exit(0);
default:printf(“invalid option”);
}
}
}
Output:
Menu:1.data encryption
2.decryption
3.exit
Enter your choice : 1
KITS ENGG COLLEGE Page No:
Computer Networks
data encryption
enter plain text : kits
enter the encryption key:cse
encrypted text is:
Menu
1.data encryption
2.decryption
3.exit
Enter your choice :2
Decryption
Enter the encrypted key:cse
The decrypted text is:kits
Menu
1.data encryption
2.decryption
3.exit
Enter your choice :3
KITS ENGG COLLEGE Page No:
+
Computer Networks
AIM:-Take An Example Subnet Graph With Weights Indicating
Delay Between Nodes.Now Obtain Routing Table Ar Each Node
Using Diastance Vector Routing Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct vertex
{
int num,delay;
char name;
};
void main()
{
int I,j,ch,n,adj,min,del[20][20];
struct vertex su,varchar[20];
clrcsr();
printf(“enter no of vertex;”);
scanf(“%d”,&n);
printf(“\n enter source vertex name:”);
fflush(stdin);
scanf(“%c”,&su.name);
printf(“enter source vertex number:”);
KITS ENGG COLLEGE Page No:
Computer Networks
scanf(“%d”,&su.number);
fflush(stdin);
printf(“enter adjacent lines for su:”);
scanf(“%d”,&adj);
printf(“enter all line adjacent delay.[vetex_name,delay]:”);
for(i=1;i<=adj;i++)
{
flushall();
scanf(“%c”,”%d”,&var[i].name,&var[i].delay);
}
printf(“enter all routing delays for adjacent nodes for others:”);
for(i=1;i<=adj;i++)
{
printf(“enter routing delay for router number #%d”,i);
for(j=1;j<=n;j++)
scanf(“%d”,&del[I],[j]);
}
for(i=1;i<=n;i++)
{
if(i==su.number)
{
min=0;
KITS ENGG COLLEGE Page No:
Computer Networks
}
else
{
min=3000;
ch=0;
for(j=1;j<=adj;j++)
{
if(min >ver[j].delay+del[i][j])
{
min=var[j].delay+del[j][i];
ch=j;
}
}
}
printf(“in shortest distance \n:”);
if(min!=0)
printf(“%d\t%c\n”,min,var[ch].name);
else
printf(“”%d”\t%c\n”,min,”__”);
}
}
}
KITS ENGG COLLEGE Page No:
Computer Networks
Input:
Enter no of vertex :12
Enter source vertex name:j
Enter source vertex number:10
Enter adjacent lines for source vertex=4
Enter all lines adjacent delay
A 8
I 10
H 12
K 6
Enter all routing delay from adjacent nodes from others
Enter routing delay for router number 1
0 12 25 40 14 23 18 17 21 9 24 29
Enter routing delay for router numer 2
24 26 18 27 7 20 31 20 0 11 22 33 ‘
Enter routing delay for router number 3
20 31 19 8 30 19 6 0 14 7 9 22
Enter routing delay for router number 4
KITS ENGG COLLEGE Page No:
Computer Networks
21 28 36 24 22 40 31 19 22 10 0 9
Output:
Shortest distance
17 i
Shortest distance
30 i
Shortest distance
18 H
Shortest distance
12 H
Shortest distance
10 I
Shortest distance
0 T
Shortest distance
6 K
Shortest distance
KITS ENGG COLLEGE Page No:
Computer Networks
15 K
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:- To encrypt and decrypt a text data using RSA algorithm.
PROGRAM:-
#include<stdio.h>
main()
{
int p,q,n,msg,encrypt=1,decrypt=1,phi,e,d,i;
clrscr();
printf("enter two prime numbers");
scanf("%d %d",&p,&q);
n=p*q;
printf("n:%d\n",n);
phi=(p-1)*(q-1);
printf("Phi:%d\n",phi);
e=e(phi);
d=d(e,phi);
printf("Public key e:%d\n",e);
printf("Private key d:%d\n",d);
printf("enter msg");
scanf("%d",&msg);
for(i=0;i<e;i++)
KITS ENGG COLLEGE Page No:
Computer Networks
{
encrypt*=(msg%n);
encrypt%=n;
}
for(i=0;i<d;i++)
{
decrypt*=encrypt%n;
decrypt%=n;
}
printf("message:%d\n",msg);
printf("encrypted message:%d\n",encrypt);
printf("decrypted message:%d\n",decrypt);
if(msg==decrypt)
printf("Satisfied");
getch();
}
int E(int phi)
{
int i,d;
for(i=2;i<phi;i++)
if(gcd(phi,i) == 1)
printf("\td = %d",i);
KITS ENGG COLLEGE Page No:
Computer Networks
printf("\nSelect e value:");
scanf("%d",&d); return d;
}
int d(int e,int phi)
{
int d=1;
while((e*d)%phi!=1)
d++;return d;
}
int gcd(int a,int b)
{
int c;
while(1)
{
c = a%b;
if(c==0) return b;
a = b;b = c;
}
}
KITS ENGG COLLEGE Page No:
Computer Networks
OUTPUT :
enter two prime numbers 17 11
n:187
phi:160
d=3 d=7 d=9 d=11 d=13 d=17 d=19 d=21 d=23
d=27 d=29 d=31 d=33 d=37 d=39 d=41 d=43 d=47
d=49
d=51 d=53 d=57 d=59 d=61 d=63 d=67 d=69 d=71
d=73
d=77 d=79 d=81 d=83 d=87 d=89 d=91 d=93 d=97
d=99
d=101 d=103 d=107 d=109 d=111 d=113 d=117
d=119 d=121 d=123
d=127 d=129 d=131 d=133 d=137 d=139 d=141
d=143 d=147 d=149
d=151 d=153 d=157 d=159
Select e value:7
KITS ENGG COLLEGE Page No:
Computer Networks
Public key e:7
Private key d:23
enter msg 88
message:88
encrypted message:11
decrypted message:88
Satisfied
KITS ENGG COLLEGE Page No:
Computer Networks
AIM:-Take An Example Of Subnet Hosts. Obtain Broadcast Tree
for It.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int i,j,n,radius=5,temp[5];
int s[10];
int link[10][10];
int l[5][2]={{150,200},{200,150},{200,200},{200,250},
{250,200}};
int gdriver=detect,gmode,errorcode;
clrscr();
printf(“\n enter no of nodes:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
KITS ENGG COLLEGE Page No:
Computer Networks
printf(“%d &di:”,i,j);
scanf(“%d”,&link[I,j]);
}
}
initgraph (&gdriver,&gmode,” “);
printf(“\n\n\n\n\t\t subnet \t\t\t corresponding true \n;”);
printf(“\t\t\t\t -------\t\t --------\n\n”);
for(i=0;i<n<i++)
circle(l[i][0],l[i][1],radius);
for(i=0;i<n;i++)
circle(l[i][0]+250,l[i][1],radius)
for(i=n-1;i>=0;i--)
line(l[i][0],l[i][1],l[j][0],l[j][1]);
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
if(link[i][j]==1&&temp[i]!=1)
{
line(l[i][0]+250,l[i][1],l[j][0]+250,l[j][1])
temp[i]=1
}
KITS ENGG COLLEGE Page No:
Computer Networks
}
}
getch();
}
Input:
Enter No of nodes:3
0 0 1
0 1 1
0 2 1
1 0 1
1 1 1
1 2 1
2 0 1
2 1 1
2 2 1
Output:
Subnet corresponding tree
KITS ENGG COLLEGE Page No:
Computer Networks
l l
l l
l l
KITS ENGG COLLEGE Page No:

0 comments:

Post a Comment