CN Lab Manual On 23-09-2021
CN Lab Manual On 23-09-2021
Tech I SEM
Computer Networks Lab List of Experiments
EXPERIMENT NO: 1
In Data Link layer, the stream of bits from the physical layer is divided into data frames. The
data frames can be of fixed length or variable length. In variable - length framing, the size of
each frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark
the end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.
● Byte - Stuffing − A byte or character is stuffed in the message to differentiate from the
delimiter for synchronization. This is also called character-oriented framing.
● Bit - Stuffing − Bit stuffing is the mechanism of inserting one or more non-information bits
into a message to be transmitted, to break up the message sequence, for synchronization
purpose.
In bit-oriented protocols, the message is coded as a sequence of bits, which are interpreted in the
upper layers as text, graphics, audio, video etc. A frame has the following parts −
● Frame Header − It contains the source and the destination addresses of the frame.
● Payload field − It contains the message to be delivered.
● Trailer − It contains the error detection and error correction bits.
● Flags − A bit pattern that defines the beginning and end bits in a frame. It is generally of 8-
bits. Most protocols use the 8-bit pattern 01111110 as flag.
Bit Stuffing Mechanism
In a data link frame, the delimiting flag sequence generally contains six or more consecutive 1s.
In order to differentiate the message from the flag in case of the same sequence, a single bit is
stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the message, an
extra 0 bit is stuffed at the end of the five 1s.
When the receiver receives the message, it removes the stuffed 0s after each sequence of five 1s.
The un-stuffed message is then sent to the upper layers.
Purpose of Byte Stuffing
In Data Link layer, the stream of bits from physical layer is divided into data frames. The data
frames can be of fixed length or variable length. In variable – length framing, the size of each
frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark the
end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.
● Frame Header − It contains the source and the destination addresses of the frame.
● Payload field − It contains the message to be delivered.
● Trailer − It contains the error detection and error correction bits.
● Flags − 1- byte (8-bits) flag at the beginning and at end of the frame. It is a protocol –
dependent special character, signalling the start and end of the frame.
If the pattern of the flag byte is present in the message byte, there should be a strategy so that the
receiver does not consider the pattern as the end of the frame. In character – oriented protocol,
the mechanism adopted is byte stuffing.
In byte stuffing, a special byte called the escape character (ESC) is stuffed before every byte in
the message with the same pattern as the flag byte. If the ESC sequence is found in the message
byte, then another ESC byte is stuffed before it.
Implement the data link layer farming methods such as character, character stuffing, and bit
stuffing.
Program:(bit-stuffing)
#include<stdio.h>
main()
{
int i,k,a,count=0;
char c[50];
printf("\nEnter the data to be send:");
fflush(stdin);
gets(c);
a=strlen(c);
for(i=0;i<a;i++)
{
if(c[i]=='1')
{
count++;
if(count==6)
{
for(k=a;k>i+1;k--)
c[k]=c[k-1];
a=a+1;
count=0;
c[i+1]='0';
}
}
else
count=0;
}
printf("\nData after stuffing:");
puts(c);
count=0;
for(i=0;i<a;i++)
{
if(c[i]=='1')
{
count++;
if(count==5)
{
for(k=i+1;k<a;k++)
c[k]=c[k+1];
count=0;
}
}
}
}
Output:
#include<stdio.h>
main()
{
char a[50],b[50],c[50];
int i,j,k,m=0,count,l1,l2;
printf("\nEnter the data to send:");
fflush(stdin);
gets(a);
printf("\nEnter the delimeter:");
fflush(stdin);
gets(b);
l1=strlen(a);
l2=strlen(b);
c[0]='S';
for(i=0;i<l2;i++)
c[i+1]=b[i];
for(j=0;j<l1;j++)
{
count=0;
for(k=0,m=j;k<l2;k++,m++)
{
if(a[m]!=b[k])
count=1;
}
if(count==0)
{
for(k=l1+l2-1;k>m;k--)
a[k]=a[k-l2];
for(k=0;k<l2;k++)
a[m++]=b[k];
j=j+l2;
l1=l1+l2;
}
}
for(k=0;k<l1;k++)
c[++i]=a[k];
for(k=0;k<l2;k++)
c[++i]=b[k];
c[++i]='E';
printf("\nData after stuffing:");
for(k=0;k<=i;k++)
printf("%c",c[k]);
for(j=0;j<l1-l2;j++)
{
count=0;
m=j;
for(k=0;k<l2;k++)
{
if(a[m++]!=b[k])
count=1;
}
if(count==0)
{
for(k=m;k<l1-l2;k++)
a[k]=a[k+l2];
l1=l1-l2;
}
}
printf("\nData after destuffing:");
for(i=0;i<l1;i++)
printf("%c",a[i]);
}
Output:
EXPERIMENT NO: 2
Error:
A condition when the receiver’s information does not matches with the sender’s
information. During transmission, digital signals suffer from noise that can introduce
errors in the binary bits travelling from sender to receiver. That means a 0 bit may
change to 1 or a 1 bit may change to 0.
Error Detecting Codes (Implemented either at Data link layer or Transport Layer
of OSI Model):
Whenever a message is transmitted, it may get scrambled by noise or data may
get corrupted. To avoid this, we use error-detecting codes which are additional data
added to a given digital message to help us detect if any error has occurred during
transmission of the message.
Basic approach used for error detection is the use of redundancy bits, where additional
bits are added to facilitate detection of errors.
EXAMPLE
ALGORITHM FOR CRC
BEGIN
step 1: declare i,j,keylen,mesglen and char arrays
input[100],temp[30],quot[100],rem[30],key[30],key1[30];
step 2: enter the data and scan the data into input[]
step 3: enter the key and scan the data into key[]
step 4: save the length of input in msglen and length of key in keylen
step 5: copy key[] to key1[]
step 6: initialize i=0 and repeat step 7 until i<keylen-1
step 7: input[msglen+i]=0 and inc i
step 8: initialize i=0 and repeat step 9 until i<keylen
step 9: temp[i]=input[i] and inc i
step 10: initialize i=0 and repeat step 11-22 until i<msglen
step 11: quot[i]=temp[0] and inc i
step 12: if quot[i] equal to 0 then goto step 13 else step 15
step 13: initialize j=0 and repeat step 14 until j<keylen and goto step 17
step 14: key[j]=0 and inc j
step 15: initialize j=0 and repeat step 16 until j<keylen
step 16: key[j]=key1[j] and inc j
step 17: initialize j=keylen-1 and repeat step 18-20 until j>0
step 18: if temp[j] equal to key[j] goto step 19 else step 20
step 19: rem[j-1]='0' and dec j and goto step 21
step 20: rem[j-1]='1' and dec j
step 21: rem[keylen-1]=input[i+keylen]
step 22: copy rem to temp[]
step 23: copy temp[] to rem[]
step 24: print the final data from input[] and rem[]
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k=0;
int flag=1,a[16],g[16],r[20],div[16],n,m;
system(“clear”);
printf(“Enter degree of generator:”);
scanf(“%d”,&n);
printf(“\n Enter the generator:\n”);
for(i=0;i<=n;i++)
scanf(“%d”,&g[i]);
printf(“\nEnter the degree of the frame:”);
scanf(“%d”,&m);
printf(“Enter the frame:\n”);
for(i=0;i<=m;i++)
scanf(“%d”,&a[i]);
if(m<n || g[0]==0)
{
printf(“Not a proper generator:\n”);
exit(0);
}
for(i=m+1;i<=m+n;i++)
a[i]=0;
for(j=0;j<=n;j++)
r[j]=a[j];
for(i=n;i<=m+n;i++)
{
if(i>n)
{
for(j=0;j<n;j++)
r[j]=r[j+1];
r[j]=a[i];
}
if(r[0])
div[k++]=1;
else
{
div[k++]=0;
continue;
}
for(j=0;j<=n;j++)
r[j]=r[j]^g[j];
}
printf(“\n Quotient is:”);
for(j=0;j<k;j++)
printf(“%d”,div[j]);
printf(“\n Remainder is:”);
for(i=1;i<=n;i++)
printf(“%d”,r[i]);
printf(“\n Transmitted frame is:”);
for(i=m+1,j=1;i<=m+n;i++)
printf(“%d”,a[i]);
printf(“\n”);
printf(“\n Enter the degree of frame:”);
scanf(“%d”,&m);
printf(“Enter the frame:\n”);
for(i=0;i<=m;i++)
scanf(“%d”,&a[i]);
for(j=0;j<=n;j++)
r[j]=a[j];
k=0;
for(i=n;i<=m;i++)
{
if(i>n)
{
for(j=0;j<n;j++)
r[j]=r[j+1];
r[j]=a[i];
}
if(r[0])
div[k++]=1;
else
{
div[k++]=0;
continue;
}
for(j=0;j<=n;j++)
r[j]=r[j]^g[j];
}
printf(“\n Quotient is:”);
for(j=0;j<k;j++)
printf(“%d”,div[j]);
printf(“\n Remainder is:”);
for(i=1;i<=n;i++)
printf(“%d”,r[i]);
for(i=1;i<=n;i++)
{
if(r[i])
flag=0;
}
if(flag)
printf(“\n No Error \n”);
else
printf(“\n Error”);
return 0;
}
OUTPUT:
Enter the degree of the generator:3
Enter the generator:
1001
Enter the degree of the frame:7
Enter the frame :
1
1
1
1
1
1
1
1
Quotient is :11100011
Remainder is:011
Transmitted frame is:11111111011
EXPERIMENT NO: 3
3.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.
In computer networks sliding window protocol is a method to transmit data on a network. Sliding
window protocol is applied on the Data Link Layer of OSI model. At data link layer data is in the form of
frames. In Networking, Window simply means a buffer which has data frames that needs to be
transmitted.
Both sender and receiver agrees on some window size. If window size=w then after sending w frames
sender waits for the acknowledgement (ack) of the first frame.
As soon as sender receives the acknowledgement of a frame it is replaced by the next frames to be
transmitted by the sender. If receiver sends a collective or cumulative acknowledgement to sender then it
understands that more than one frames are properly received, for eg:- if ack of frame 3 is received it
understands that frame 1 and frame 2 are received properly.
Image Source
In sliding window protocol the receiver has to have some memory to compensate any loss in transmission
or if the frames are received unordered.
W = Window Size
tx = Transmission time
tp = Propagation delay
It is of two types:-
1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.
2. Go back n: Sender transmits all frames present in the window that occurs after the error bit including
error bit also.
Program:
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
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;
}
Output
Enter window size: 3
Enter number of frames to transmit: 5
Enter 5 frames: 12 5 89 4 6
With sliding window protocol the frames will be sent in the following manner (assuming no corruption of
frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 5 89
Acknowledgement of above frames sent is received by sender
46
Acknowledgement of above frames sent is received by sender
EXPERIMENT NO: 4
During the algorithm execution, we'll mark every node with its minimum
distance to node C (our selected node). For node C, this distance is 0. For the
rest of nodes, as we still don't know that minimum distance, it starts being infinity
(∞):
We'll also have a current node. Initially, we set it to C (our selected node). In the
image, we mark the current node with a red dot.
Now, we check the neighbours of our current node (A, B and D) in no specific
order. Let's begin with B. We add the minimum distance of the current node (in
this case, 0) with the weight of the edge that connects our current node with B (in
this case, 7), and we obtain 0 + 7 = 7. We compare that value with the minimum
distance of B (infinity); the lowest value is the one that remains as the minimum
distance of B (in this case, 7 is less than infinity):
So far, so good. Now, let's check neighbour A. We add 0 (the minimum distance
of C, our current node) with 1 (the weight of the edge connecting our current
node with A) to obtain 1. We compare that 1 with the minimum distance of A
(infinity), and leave the smallest value:
We now need to pick a new current node. That node must be the unvisited
node with the smallest minimum distance (so, the node with the smallest
number and no check mark). That's A. Let's mark it with the red dot:
And now we repeat the algorithm. We check the neighbours of our current node,
ignoring the visited nodes. This means we only check B.
For B, we add 1 (the minimum distance of A, our current node) with 3 (the weight
of the edge connecting A and B) to obtain 4. We compare that 4 with the
minimum distance of B (7) and leave the smallest value: 4.
For B, we obtain 2 + 5 = 7. We compare that value with B's minimum distance (4)
and leave the smallest value (4). For E, we obtain 2 + 7 = 9, compare it with the
minimum distance of E (infinity) and leave the smallest one (9).
As there are not univisited nodes, we're done! The minimum distance of each
node now actually represents the minimum distance from that node to node
C (the node we picked as our initial node)!
Here's a description of the algorithm:
1. Mark your selected initial node with a current distance of 0 and the rest
with infinity.
2. Set the non-visited node with the smallest current distance as the current node C.
3. For each neighbour N of your current node C: add the current distance of C with
the weight of the edge connecting C-N. If it's smaller than the current distance of
N, set it as the new current distance of N.
4. Mark the current node C as visited.
5. If there are non-visited nodes, go to step 2.
Program:
#include<stdio.h>
#include<stdlib.h>
//#include<string.h>
#define M_NODES 10
#define INFINITY 1000
int n,dis[M_NODES][M_NODES];
void main()
{
void spath(int,int);
int i,j,g[100][100],s,t,c; printf("\
nEnter number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
printf("\nEnter weight between %d-->%d\t",i,j);
scanf("%d",&c);
if(c>=0)
dis[i][j]=c;
else
exit(0);
}
}
printf("\nEnter source and destination nodes:");
scanf("%d%d",&t,&s);
spath(s,t);
//getch();
}
void spath(int s,int t)
{
struct state
{
int pre,len;
enum{permanet,tentative} label;
}state[M_NODES];
int i,k,min,path[20],c=0;
Output:
EXPERIMENT NO: 5
THEORY:
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree
and connects all the vertices together. A single graph can have many different spanning
trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted,
connected and undirected graph is a spanning tree with weight less than or equal to the weight of
every other spanning tree. The weight of a spanning tree is the sum of weights given to each
edge of the spanning tree.
A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given
graph.
Now pick all edges one by one from sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.
6. Pick edge 8-6: Since including this edge results in cycle, discard it.
10. Pick edge 1-2: Since including this edge results in cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.
ALGORITHM
step 1: declare variable as int p,q,u,v,n;
step 2: Initialize min=99,mincost=0;
step 3: declare variable as int t[50][2],i,j;
step 4: declare variable as int parent[50],edge[50][50];
step 5: Begin
step 6: write "Enter the number of nodes"
step 7: read "n"
step 8: Initialize i=0
step 9: repeat step(10-12) until i<n
step10: increment i
step11: write"65+i"
step12: Initialize parent[i]=-1
step13:wite "\n"
step14: Initialize i=0
step15: repeat step(15-21) until i<n
step16: increment i
step17: write"65+i"
step18: Initialize j=0
step19: repeat until j<n
step20: increment j
step21: read edge[i][j]
PROGRAM:
#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;
}
OUTPUT
Enter the number of nodes 4
A B C D
A 1 3 5 6
B 6 7 8 9
C 2 3 5 6
D 1 2 3 7
Minimum cost is 9
Minimum spanning tree is
BA6
CA2
DA1
EXPERIMENT NO: 6
Aim: Take an example subnet graph with weights indicating delay between nodes.
Now obtain routing table at each node using vector routing algorithm.
Theory:
https://www.gatevidyalay.com/distance-vector-routing-routing-algorithms/
Program:
#include<stdio.h>
#include<conio.h>
#define MN 100
#define INFINITY 500
char graph[30][30];
struct st
{
int a[20][20];
}st[20];
int n;
void main()
{
int i,j;
void disk(int,int);
//clrscr();
printf("\nEnter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
printf("\nEnter distance between %d -->%d",i,j);
scanf("%d",&graph[i][j]);
graph[j][i]=graph[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
disk(i,j);
}
}
for(i=0;i<n;i++)
{
printf("\nRouting table for node %d",i);
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("%d | %d | %d",j,st[i].a[j][i],st[i].a[j][i+1]);
printf("\n \n");
}
}
}
//getch();
}
void disk(int t,int s)
{
struct state
{
int predecessor;
int length;
enum{permanent,tentative} label;
}
state[MN];
Output
:
EXPERIMENT NO: 7
AIM: Take a 64 bit playing text and encrypt the same using DES
algorithm.
The Data Encryption Standard (DES) is a symmetric-key block cipher published by the National
Institute of Standards and Technology (NIST).
DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block size
is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the
64 bits of the key are not used by the encryption algorithm (function as check bits only). General
Structure of DES is depicted in the following illustration −
Since DES is based on the Feistel Cipher, all that is required to specify DES is −
· Round function
· Key schedule
· Any additional processing − Initial and final permutation
The initial and final permutations are straight Permutation boxes (P-boxes) that are inverses of
each other. They have no cryptography significance in DES. The initial and final permutations
are shown as follows −
Round Function
The heart of this cipher is the DES function, f. The DES function applies a 48-bit key to the
rightmost 32 bits to produce a 32-bit output.
Expansion Permutation Box − Since right input is 32-bit and round key is a 48-bit,· we
first need to expand right input to 48 bits. Permutation logic is graphically depicted in the
following illustration −
The graphically depicted permutation logic is generally described as table in DES· specification
illustrated as shown −
XOR (Whitener). − After the expansion permutation, DES does XOR operation on the
expanded right section and the round key. The round key is used only in this operation.
Substitution Boxes. − The S-boxes carry out the real mixing (confusion). DES uses· 8
S-boxes, each with a 6-bit input and a 4-bit output. Refer the following illustration −
The round-key generator creates sixteen 48-bit keys out of a 56-bit cipher key. The process of
key generation is depicted in the following illustration −
The logic for Parity drop, shifting, and Compression P-box is given in the DES description.
DES Analysis
The DES satisfies both the desired properties of block cipher. These two properties make cipher
very strong.
· Avalanche effect − A small change in plaintext results in the very great change in
the ciphertext.
· Completeness − Each bit of ciphertext depends on many bits of plaintext.
ALGORITHM(DES):
Begin
Step 1: Declare integer i,ch,lp
Step 2: Declare character array’s cipher[50],plain[50] and character key
Step 3: while(TRUE) do step(4-31)
Step 4: write(“\n- - -MENU- - -\n”)
Step 5: write(“\n1:Data encryption\t\n2:Data Decryption\t\n\n:Exit”)
Step 6: write(“\n\nEnter your choice:”)
Step 7: scanf(“%d”,&ch);
Step 8: switch(ch) do
Step 9: case 1:
Step 10:write(“\nData Encryption”)
Step 11:write(“\nEnter plain text”)
Step 12:fflush(stdin);
Step 13:Input the string into plain to encrypt
Step 14:write(“\nEnter theencryption key:”)
Step 15:input the value into key
Step 16:load the length of the key into lp
Step 17:fori=0 to strlen(plain)-1 step do
Step 18:cipher[i]=plain[i]^lp;
Step 19:cipher[i]=’\0’;
Step 20:write(“\nEncrypted text is:”)
Step 21:puts(cipher)
Step 22:break;
Step 23:case 2:
Step 24:write(“\nDataDecryption”)
Step 25:fori=0 to strlen(plain)-1 step do
Step 26:plain[i]=cipher[i]^lp;
Step 27:write(“\nDecrypted text is:”)
Step 28:puts(cipher);
Step 29:break;
Step 30:case 3:
Step 31:exit(0);
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i, ch, lp;
char cipher[50],plain[50];
char key[50];
while(1)
{
printf("\n-----MENU- - - -\n");
printf("\n1:Data Encryption\t\n\n2:Data Decryption\t\n\n3:Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nData Encryption");
printf("\nEnter the plain text:");
fflush(stdin);
scanf("%s",&plain);
printf("\nEnter the encryption key:");
scanf("%s",&key);
lp=strlen(key);
for(i=0;plain[i]!='\0';i++)
cipher[i]=plain[i]^lp
break;
cipher[i]='\0';
printf("\nThe encrypted text is:"); puts(cipher);
case 2: printf("\nData decryption");
for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp;
printf("\nDecrypted text is:");
puts(plain);
break;
case 3: exit(0);
}
}
}
OUTPUT:
EXPERIMENT NO: 7
THEORY:
Data encryption standard was widely adopted by the industry in security products. Plain text is
encrypted in blocks of 64 bits yielding 64 bits of cipher text. The algorithm which is
parameterized by a 56 bit key has 19 distinct stages. The first stage is a key independent
transposition and the last stage is exactly inverse of the transposition. The remaining stages are
functionally identical but are parameterized by different functions of the key. The algorithm has
been designed to allow decryption to be done with the same key as encryption
Program:
#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);
}
Output:
THEORY:
The congesting control algorithms are basically divided into two groups: open loop and closed
loop. Open loop solutions attempt to solve the problem by good design, in essence, to make sure it does
not occur in the first place. Once the system is up and running, midcourse corrections are not made. Open
loop algorithms are further divided into ones that act at source versus ones that act at the destination.
In contrast, closed loop solutions are based on the concept of a feedback loop if there is any
congestion. Closed loop algorithms are also divided into two sub categories: explicit feedback and
implicit feedback. In explicit feedback algorithms, packets are sent back from the point of congestion to
warn the source. In implicit algorithm, the source deduces the existence of congestion by making local
observation, such as the time needed for acknowledgment to come back.
The presence of congestion means that the load is (temporarily) greater than the resources (in part
of the system) can handle. For subnets that use virtual circuits internally, these methods can be used at
the network layer.
Another open loop method to help manage congestion is forcing the packet to be transmitted at a
more predictable rate. This approach to congestion management is widely used in ATM networks and is
called traffic shaping.
The other method is the leaky bucket algorithm. Each host is connected to the network by an
interface containing a leaky bucket, that is, a finite internal queue. If a packet arrives at the queue when it
is full, the packet is discarded. In other words, if one or more process are already queued, the new packet
is unceremoniously discarded. This arrangement can be built into the hardware interface or simulate d by
the host operating system. In fact it is nothing other than a single server queuing system with constant
service time.
The host is allowed to put one packet per clock tick onto the network. This mechanism turns an
uneven flow of packet from the user process inside the host into an even flow of packet onto the network,
smoothing out bursts and greatly reducing the chances of congestion.
ALGORITHM/FLOWCHART:
Step 1: Start
Step 5: Repeat the process of transmission until all packets are transmitted. (Reject packets where its size is greater
than the bucket size)
Step 6: Stop
SOURCE CODE:
#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!!");
}
}
}
}
}
Output
8. Write a program for frame sorting technique used in buffers.
Theory
The data link layer divides the stream of bits received from the network layer into manageable
data units called frames.If frames are to be distributed to different systems on the network, the Data link layer adds
a header to the frame to define the sender and/or receiver of the frame.
Each Data link layer has its own frame format. One of the fields defined in the format is the
maximum size of the data field. In other words, when datagram is encapsulated in a frame, the
total size of the datagram must be less than this maximum size, which is defined by restriction
imposed by the hardware and software used in the network.
The value of MTU differs from one physical network to anotherIn order to make IP protocol
portable/independent of the physical network, the packagers decided to make the maximum length of the IP
datagram equal to the largest Maximum Transfer Unit (MTU) defined so far. However for other physical networks
we must divide the datagrams to make it possible to pass through these networks. This is called fragmentation.
When a datagram is fragmented, each fragmented has its own header. A fragmented datagrammay itself be
fragmented if it encounters a network with an even smaller MTU. In another words, a datagram may be
fragmented several times before it reached the final destination and also, the datagrams referred to as (frames in
Data link layer) may arrives out of order at destination. Hence sorting of frames need to be done at the destination
to recover the original data.
ALGORITHM/FLOWCHART:
Step 1: Start
Step 2: Enter the Message to be transmitted(read in msg)
Step3:Caluculate no of packets
Step4: Goto Step 5,6,7,8
Step5:void shuffle(intNoOfPacket)
Step 5.1: Calculate status,transdata
Step 5.2:for i = 0; i <NoOfPacket;
Step 5.2.1: calculate trans
trans = rand()%NoOfPacket;
Step 5.2.2: if Status[trans]!=1
Copy read data in transmitted data and goto step 6
Step 6: void receive(intNoOfPacket)
Step 6.1:Print the packets received
Step 6.2: Print the frame in sorted order go to Step 7
Step 7: void sortframes(intNoOfPacket)
Step 7.1 : if transdata[j].SeqNum>transdata[j + 1].SeqNumgoto 7.1.1
Step 7.1.1: Sort the packets
Step 8: Stop
Source Code
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define FSize 3
typedefstruct packet
{
intSeqNum;
char Data[FSize+1];
}packet;
void shuffle(intNoOfPacket)
{
int *Status;
int i, j, trans;
randomize();
Status=(int * )calloc(NoOfPacket, sizeof(int));
transdata = (struct packet *)malloc(sizeof(packet) * NoOfPacket);
for (i = 0; i <NoOfPacket;)
{
trans = rand()%NoOfPacket;
if (Status[trans]!=1)
{
transdata[i].SeqNum = readdata[trans].SeqNum;
strcpy(transdata[i].Data, readdata[trans].Data);
i++; Status[trans] = 1;
}
}
free(Status);
}
voidsortframes(intNoOfPacket)
{
packet temp;
int i, j;
for (i = 0; i <NoOfPacket; i++)
for (j = 0; j <NoOfPacket – i-1; j++)
if (transdata[j].SeqNum>transdata[j + 1].SeqNum)
{
temp.SeqNum = transdata[j].SeqNum;
strcpy(temp.Data, transdata[j].Data);
transdata[j].SeqNum = transdata[j + 1].SeqNum;
strcpy(transdata[j].Data, transdata[j + 1].Data);
transdata[j + 1].SeqNum = temp.SeqNum;
strcpy(transdata[j + 1].Data, temp.Data);
}
}
void receive(intNoOfPacket)
{
int i;
printf("\nPackets received in the following order\n");
for (i = 0; i <NoOfPacket; i++)
printf("%4d", transdata[i].SeqNum);
sortframes(NoOfPacket);
printf("\n\nPackets in order after sorting..\n");
for (i = 0; i <NoOfPacket; i++)
printf("%4d", transdata[i].SeqNum);
printf("\n\nMessage received is :\n");
for (i = 0; i <NoOfPacket; i++)
printf("%s", transdata[i].Data);
}
void main()
{
char *msg;
intNoOfPacket;
clrscr();
printf("\nEnter The message to be Transmitted :\n");
scanf("%[^\n]", msg);
NoOfPacket = divide(msg);
shuffle(NoOfPacket);
receive(NoOfPacket);
free(readdata);
free(transdata);
getch();
}
Output
Message received is :
hi, it was nice meeting u on sunday
10. Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.
i. Packet Capture Using Wire shark
1. Click View > Wireless Toolbar. The Wireless Toolbar will appear just below the
Main toolbar.
2. Use the Wireless Toolbar to configure the desired channel and channel width.
3. Under Capture, click on AirPcap USB wireless capture adapter to select the
capture interface.
Note: If the AirPcap isn't listed, press F5 to refresh the list of available packet capture
interfaces.
Note: The AirPcap has been discontinued by RiverBed and is 802.11n only.
You can start Wireshark from the command line, but it can also be started from most Window managers as well. In
this section we will look at starting it from the command line.
Wireshark supports a large number of command line parameters. To see what they are, simply enter the
command wireshark -h and the help information shown in Help information available from Wireshark (or something
similar) should be printed.
Once you have captured some packets or you have opened a previously saved capture file, you can view the packets
that are displayed in the packet list pane by simply clicking on a packet in the packet list pane, which will bring up
the selected packet in the tree view and byte view panes.
You can then expand any part of the tree to view detailed information about each protocol in each packet. Clicking on
an item in the tree will highlight the corresponding bytes in the byte view. An example with a TCP packet selected is
shown in Figure, “Wireshark with a TCP packet selected for viewing”. It also has the Acknowledgment number in
the TCP header selected, which shows up in the byte view as the selected bytes.
Figure 1. Wireshark with a TCP packet selected for viewing
You can also select and view packets the same way while Wireshark is capturing if you selected “Update list of
packets in real time” in the “Capture Preferences” dialog box.
In addition you can view individual packets in a separate window as shown in Figure , “Viewing a packet in a
separate window”. You can do this by double-clicking on an item in the packet list or by selecting the packet in
which you are interested in the packet list pane and selecting View → Show Packet in New Window. This allows you
to easily compare two or more packets, even across multiple files.
Figure2. Viewing a packet in a separate window
iv. Analysis and Statistics & Filters.
To start statistics tools, start Wireshark, and choose Statistics from the main menu.
What you will get is the Capture File Properties window (displayed in the following
screenshot).