0% found this document useful (0 votes)
713 views29 pages

Experiment 1:: Write Program To Implement Data Link Layer Stuffing Method

The document describes three data link layer stuffing methods: 1. Bit stuffing inserts extra 0 bits into the data stream when 5 consecutive 1s are detected to ensure sufficient transition for synchronization. 2. Byte stuffing uses special flag bytes to delimit frames and stuffs an escape byte before flag bytes in the data. 3. Character stuffing uses character sequences to delimit frames and stuffs additional characters to avoid misinterpreting control characters in the data. The code provided implements these three stuffing methods to add error detection to data transmission.

Uploaded by

Ronit Kataria
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)
713 views29 pages

Experiment 1:: Write Program To Implement Data Link Layer Stuffing Method

The document describes three data link layer stuffing methods: 1. Bit stuffing inserts extra 0 bits into the data stream when 5 consecutive 1s are detected to ensure sufficient transition for synchronization. 2. Byte stuffing uses special flag bytes to delimit frames and stuffs an escape byte before flag bytes in the data. 3. Character stuffing uses character sequences to delimit frames and stuffs additional characters to avoid misinterpreting control characters in the data. The code provided implements these three stuffing methods to add error detection to data transmission.

Uploaded by

Ronit Kataria
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/ 29

EXPERIMENT 1

AIM: Write program to implement data link layer stuffing method :


1. Bit Stuffing
2.Byte Stuffing
3. Character Stuffing

THEORY: Security and error detection are the most prominent features that are to be provided by
any application which transfer data from one end to the other end.
One of such a mechanism in tracking errors which may add up to the original data during transfer is
known as stuffing. There are two types of stuffing namely Bit Stuffing and Character Stuffing.
In Bit Stuffing, 01111110 is append within the original data while transfer of it.
In Byte Stuffing, DLESTX and DLEETX are used to denote start and end of character data with some
constraints as shown in program below clearly.

Bit stuffing:
 Allows frame to contain arbitrary number of bits and arbitrary character size. The frames are
separated by separating flag.
 Each frame begins and ends with a special bit pattern, 01111110 called a flag byte. When five
consecutive l's are encountered in the data, it automatically stuffs a '0' bit into outgoing bit
stream.
 In this method, frames contain an arbitrary number of bits and allow character codes with an
arbitrary number of bits per character. In his case, each frame starts and ends with a special bit
pattern, 01111110.
 In the data a 0 bit is automatically stuffed into the outgoing bit stream whenever the sender's
data link layer finds five consecutive 1s.
 This bit stuffing is similar to byte stuffing, in which an escape byte is stuffed into the outgoing
character stream before a flag byte in the data.
 When the receiver sees five consecutive incoming i bits, followed by a o bit, it automatically
destuffs (i.e., deletes) the 0 bit. Bit Stuffing is completely transparent to network layer as byte
stuffing. The figure1 below gives an example of bit stuffing.
 This method of framing finds its application in networks in which the change of data into code
on the physical medium contains some repeated or duplicate data. For example, some LANs
encodes bit of data by using 2 physical bits.

Byte stuffing:
 In this method, start and end of frame are recognized with the help of flag bytes. Each frames
starts with and ends with a flag byte. Two consecutive flag bytes indicate the end of one frame
and start of the next one. The flag bytes used in the figure 2 used is named as “ESC” flag byte.
 A frame delimited by flag bytes. This framing method is only applicable in 8-bit character codes
which are a major disadvantage of this method as not all character codes use 8-bit characters
e.g. Unicode.
 Four example of byte sequences before and after stuffing:

Character stuffing: In the second method, each frame starts with the ASCII character sequence DLE
STX and ends with the sequence DLE ETX. This method overcomes the drawbacks of the character
count method. If the destination ever loses synchronization, it only have to look for DLE STX and
DLE ETX characters. If however, binary data is being transmitted then there exists a possibility of
the characters DLE STX and DLE ETX occurring in the data. Since this can interfere with the framing,
a technique called character stuffing is used. The sender's data link layer inserts an ASCII DLE
character just before the DLE character in the data. The receiver's data link layer removes this DLE
before this data is given to the network layer. However, character stuffing is closely associated with
8-bit characters and this is a major hurdle in transmitting arbitrary sized characters.

CODE:
#include<bits/stdc++.h>
using namespace std;
int main()
{
// BIT STUFFING
string s;
cout << "BIT STUFFING:\nEnter the data bits : ";
cin >> s;
int set_bits=0, i;
string stuffed="", delimiter="01111110";
for(i = 0; i < s.length(); i++)
{
if(s[i]=='1')
set_bits++;
else
set_bits=0;
stuffed+=s[i];
if(set_bits==5)
{
stuffed+='0';
set_bits=0;
}
}
stuffed=delimiter+stuffed+delimiter;
string destuffed="";
set_bits=0;
for(i=8; i<stuffed.length()-8; i++)
{
if(stuffed[i]=='1')
set_bits++; else set_bits=0;
destuffed+=stuffed[i];
if(set_bits==5)
{
i++;
set_bits=0;
}
}
cout << "data after stuffing : " << stuffed << endl;
cout << "data after destuffing : " << destuffed << endl;

// BYTE STUFFING
int n;
cout << "\nBYTE STUFFING:\nEnter number of bytes : ";
cin >> n;
cout << "Enter the data stream : ";
vector<string> input, byte_stuffed, byte_destuffed;
for(i=0; i<n; i++)
{
cin >> s;
input.push_back(s);
}
for(i=0; i<n; i++)
{
byte_stuffed.push_back(input[i]);
if(input[i]=="FLAG" || input[i]=="ESC")
byte_stuffed.insert(byte_stuffed.end()-1, "ESC");
}
for(i=0; i<byte_stuffed.size(); i++)
{
if(byte_stuffed[i]=="ESC")
i++;
byte_destuffed.push_back(byte_stuffed[i]);
}
cout << "\ndata after stuffing : ";
for(auto&x:byte_stuffed)
cout << x << " ";
cout << endl;
cout << "\ndata after destuffing : ";
for(auto&x:byte_destuffed)
cout << x << " ";
cout << endl;

//CHARACTER STUFFING
string s;
cin>>s;
int size=s.length();
char ans[1000];
ans[0]='D';
ans[1]='L';
ans[2]='E';
ans[3]=' ';
ans[4]='S';
ans[5]='T';
ans[6]='X';
int k=7;
for(int i=0;i<size;)
{
if(s[i]=='D' && s[i+1]=='L' && s[i+2]=='E')
{
ans[k++]='D';
ans[k++]='L';
ans[k++]='E';
ans[k++]='D';
ans[k++]='L';
ans[k++]='E';
i=i+3;
}
else
{
ans[k++]=s[i];
i++;
}
}
ans[k++]='D';
ans[k++]='L';
ans[k++]='E';
ans[k++]=' ';
ans[k++]='E';
ans[k++]='T';
ans[k++]='X';
ans[k]='\0';
cout<<ans;
return 0;
}

OUTPUT:
FINDING & LEARNING:

 Byte stuffing also referred to as Character Stuffing.


 In Byte Stuffing, DLESTX and DLEETX are used to denote start and end of character data with
some
 constraints as shown in program below clearly.
 The advantage of Bit stuffing is that only a bit not a byte is inserted in the data stream and that
only when the content of the data stream fails to provide a timing signal to the receiver.
 Thus, very nearly 100% of the bits transported are useful data.
 Bit stuffing is used for various purposes, such as for bringing bit streams that do not necessarily
have the same or rationally related bit rates up to a common rate, or to fill buffers or frames.

EXPERIMENT 2

AIM: Write a program to implement Cyclic Redundancy Check(CRC)


THEORY: Cyclic redundancy checking is a method of checking for errors in data that has been
transmitted on a communications link. CRC is a approach to detect if the received frame contains
valid data. This technique involves binary division of the data bits being sent. The divisor is
generated using polynomials. The sender performs a division operation on the bits being sent and
calculates the remainder. Before sending the actual bits, the sender adds the remainder at the end
of the actual bits. Actual data bits plus the remainder is called a codeword. The sender transmits
data bits as codewords.
CRC uses Generator Polynomial which is available on both sender and receiver side. An example
generator polynomial is of the form like x3 + x + 1. This generator polynomial represents key 1011.
Another example is x2 + 1 that represents key 101.

Sender Side (Generation of Encoded Data from Data and Generator Polynomial (or Key)):
1. The binary data is first augmented by adding k-1 zeros in the end of the data
2. Use modulo-2 binary division to divide binary data by the key and store remainder of division.
3. Append the remainder at the end of the data to form the encoded data and send the same

Receiver Side (Check if there are errors introduced in transmission)


Perform modulo-2 division again and if remainder is 0, then there are no errors.
CODE:
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
int fs;
cout<<"Enter bits of Data: ";
cin>>fs;
int f[20];
cout<<"Enter Data: ";
for(i=0;i<fs;i++)
cin>>f[i];

int gs;
cout<<"Enter Key size: ";
cin>>gs;
int g[20];
cout<<"Enter bits of Key:";
for(i=0;i<gs;i++)
cin>>g[i];
cout<<"\n Frame: ";
for(i=0;i<fs;i++)
cout<<f[i];
cout<<"\n Key:";
for(i=0;i<gs;i++)
cout<<g[i];
int rs=gs-1;
for (i=fs;i<fs+rs;i++)
f[i]=0;
int temp[20];
for(i=0;i<20;i++)
temp[i]=f[i];
for(i=0; i<fs+rs;i++)
cout<<temp[i];
for(i=0;i<fs;i++)
{
j=0;
k=i;
if (temp[k]>=g[j])
{
for(j=0,k=i;j<gs;j++,k++)
{
if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
temp[k]=0;
else
temp[k]=1;
}
}
}
int crc[15];
for(i=0,j=fs;i<rs;i++,j++)
crc[i]=temp[j];
cout<<"\n CRC bits: ";
for(i=0;i<rs;i++)
cout<<crc[i];
cout<<"\n Transmitted Data: ";
int tf[15];
for(i=0;i<fs;i++)
tf[i]=f[i];
for(i=fs,j=0;i<fs+rs;i++,j++)
tf[i]=crc[j];
for(i=0;i<fs+rs;i++)
cout<<tf[i];
cout<<"\n\n\n\n Received Data: ";
for(i=0;i<fs+rs;i++)
cout<<tf[i];
for(i=0;i<fs+rs;i++)
temp[i]=tf[i];
for(i=0;i<fs+rs;i++)
{
j=0;
k=i;
if (temp[k]>=g[j])
{
for(j=0,k=i;j<gs;j++,k++)
{
if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
temp[k]=0;
else
temp[k]=1;
}
}
}
cout<<"\n Remainder: ";
int rrem[15];
for (i=fs,j=0;i<fs+rs;i++,j++)
rrem[j]= temp[i];
for(i=0;i<rs;i++)
cout<<rrem[i];
int flag=0;
for(i=0;i<rs;i++)
if(rrem[i]!=0) flag=1;
if(flag==0)
cout<<"\n Remainder is 0 - Succesful Transmission";
else
cout<<"\n Remainder is not 0 - Unsucessful Tranmission";
return 0;
}

OUTPUT:

FINDING & LEARNING:

 Cyclic code have a very good performance in detecting single – bit errors, double bit
errors, an odd number of errors.
 They can be easily implemented in the hardware and software.
 They are especially fast when implemented in hardware.
 They has made cyclic codes a good candidate for many networks.
 Though the Cyclic Redundancy Check look like an authentication mechanism, it is non
trivial and easy to crack mechanism. It is not suitable for security purpose.
 Without the error correcting mechanism using CRC alone will be a useless thing.
EXPERIMENT 3

AIM: Write a program to implement the stop and wait protocol.


THEORY: Stop and Wait transmission is the simplest reliability technique and is adequate for a
very simple communications protocol. A stop and wait protocol transmits a Protocol Data Unit
(PDU) of information and then waits for a response. The receiver receives each PDU and sends an
Acknowledgement (ACK) PDU if a data PDU is received correctly, and a Negative Acknowledgement
(NACK) PDU if the data was not received. In practice, the receiver may not be able to reliably
identify whether a PDU has been received, and the transmitter will usually also need to implement
a timer to recover from the condition where the receiver does not respond.

1) Sender A sends a data frame or packet with sequence number 0.


2) Receiver B, after receiving data frame, sends and acknowledgement with sequence number 1
(sequence number of next expected data frame or packet)
There is only one bit sequence number that implies that both sender and receiver have buffer
for one frame or packet only.
CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,noframes,x,x1=10,x2;
//clrscr();
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
i=1;
j=1;
noframes = noframes / 10;
printf("\n number of frames is %d",noframes);
cout<<endl;
getch();
while(noframes>0)
{
printf("\nsending frame %d",i);
cout<<endl;
srand(x1++);
x = rand()%10;
if(x%2 == 0)
{
for (x2=1; x2<2; x2++)
{
printf("waiting for %d seconds\n", x2);
cout<<endl;
//sleep(x2);
}
printf("\nsending frame %d",i);
cout<<endl;
srand(x1++);
x = rand()%10;
}
printf("Acknowledgement for frame %d",j);
cout<<endl;
noframes-=1;
i++;
j++;
}
printf("\n end of stop and wait protocol");
cout<<endl;
getch();
}

OUTPUT:
FINDING & LEARNING:
 It uses link between sender and receiver as half duplex link
 Throughput = 1 Data packet/frame per RTT
 If Bandwidth*Delay product is very high, then stop and wait protocol is not so useful. The
sender has to keep waiting for acknowledgements before sending the processed next packet.
 It is an example for “Closed Loop OR connection oriented “ protocols
 It is an special category of SWP where its window size is 1

EXPERIMENT 4
AIM: Write a program to implement sliding window protocol.
THEORY: A sliding window protocol is a feature of packet-based data transmission protocols.
Sliding window protocols are used where reliable in-order delivery of packets is required, such
as in the Data Link Layer(OSI model) as well as in the Transmission Control Protocol (TCP).
Conceptually, each portion of the transmission (packets in most data link layers, but bytes in TCP)
is assigned a unique consecutive sequence number, and the receiver uses the numbers to place
received packets in the correct order, discarding duplicate packets and identifying missing ones.
The problem with this is that there is no limit on the size of the sequence number that can be
required.

Sliding Window
Sliding window refers to an imaginary boxes that hold the frames on both sender and receiver side.
• It provides the upper limit on the number of frames that can be transmitted before
requiring an acknowledgment.
• Frames may be acknowledged by receiver at any point even when window is not full on receiver
side.
• Frames may be transmitted by source even when window is not yet full on sender side.

Sliding Window on Sender Side

At the beginning of a transmission, the sender's window contains n-l frames.

• As the frames are sent by source, the left boundary of the window moves inward, shrinking the
size of window. This means if window size is w, if four frames are sent by source after the last
acknowledgment, then the number of frames left in window is w-4.

• When the receiver sends an ACK, the source's window expand i.e. (right boundary moves
outward) to allow in a number of new frames equal to the number of frames acknowledged by
that ACK.

• For example, Let the window size is 7 (see diagram (a)), if frames 0 through 3 have been sent
and no acknowledgment has been received, then the sender's window contains three frames -
4,5,6.
Sliding Window on Receiver Side

• At the beginning of transmission, the receiver's window contains n-1 spaces for frame but
not the frames.

• As the new frames come in, the size of window shrinks.

• Therefore the receiver window represents not the number of frames received but the
number of frames that may still be received without an acknowledgment, ACK must be
sent.

• Given a window of size w, if three frames are received without an ACK being returned, the
number of spaces in a window is w-3.

• As soon as acknowledgment is sent, window expands to include the number of frames equal
to the number of frames acknowledged.

• For example, let the size of receiver's window is 7 as shown in diagram. It means window
contains spaces for 7 frames.

• With the arrival of the first frame, the receiving window shrinks, moving the boundary from
space 0 to 1. Now, window has shrunk by one, so the receiver may accept six more frame
before it is required to send an ACK.

• If frames 0 through 3 have arrived but have DOC been acknowledged, the window will contain
three frame spaces.
CODE:
#include <stdio.h>
#include<stdlib.h>

int main()
{
int temp1,temp2,temp3,temp4,i,winsize=8,noframes,moreframes;
char c;
int reciever(int);
int simulate(int);
temp4=0,temp1=0,temp2=0,temp3=0;
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
noframes = 200;
printf("\n Number of frames to Send is %d",noframes);
moreframes=noframes;
while(moreframes>=0)
{
temp1=simulate(winsize);
winsize-=temp1;
temp4+=temp1;
if(temp4 >noframes)
temp4 = noframes;
for(i=temp3+1;i<=temp4;i++)
printf("\nSending Frame %d",i);
temp2=reciever(temp1);
temp3+=temp2;
if(temp3 > noframes)
temp3 = noframes;
printf("\n Acknowledgement for the frames up to %d",temp3);
moreframes-=temp2;
temp4=temp3;
if(winsize<=0)
winsize=8;
}
printf("\nEnd of sliding window protocol");
return 0;
}

int reciever(int temp1)


{
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
}
int simulate(int winsize)
{
int temp1,i;
for(i=1;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i = temp1%winsize;
if(i==0)
return winsize;
else
return temp1%winsize;
}
OUTPUT:

FINDING & LEARNING:


 Multiple packets can be transmitted without waiting for acknowledgement
 Piggibacking
 No limit of the size or sequence number required in this protocol
 The bandwidth may be wasted in some special situations
EXPERIMENT 5
AIM: Show Net id , host id and network class of the address.

THEORY: IP address is an address having information about how to reach a specific host,
especially outside the LAN. An IP address is a 32 bit unique address having an address space of 2 32.
Generally, there are two notations in which IP address is written, dotted decimal notation and
hexadecimal notation.

Dotted Decimal Notation

Hexadecimal Notation

Classful Addressing
The 32 bit IP address is divided into five sub-classes. These are:
 Class A
 Class B
 Class C
 Class D
 Class E
Each of these classes has a valid range of IP addresses. Classes D and E are reserved for
multicast and experimental purposes respectively. The order of bits in the first octet
determine the classes of IP address.
IPv4 address is divided into two parts:
 Network ID
 Host ID

The class of IP address is used to determine the bits used for network ID and host ID and the
number of total networks and hosts possible in that particular class. Each ISP or network
administrator assigns IP address to each device that is connected to its network.

Class A
IP address belonging to class A are assigned to the networks that contain a large number of hosts.
 The network ID is 8 bits long.
 The host ID is 24 bits long.
Class B
IP address belonging to class B are assigned to the networks that ranges from medium-sized
to large-sized networks.
 The network ID is 16 bits long.
 The host ID is 16 bits long.

Class C
IP address belonging to class C are assigned to small-sized networks.
 The network ID is 24 bits long.
 The host ID is 8 bits long.

Class D
IP address belonging to class D are reserved for multi-casting. The higher order bits of the first
octet of IP addresses belonging to class D are always set to 1110. The remaining bits are for
the address that interested hosts recognize.
Class E
IP addresses belonging to class E are reserved for experimental and research
purposes.

CODE:

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

char findClass(char str[ ])


{
char arr[4];
int i = 0;
while (str[i] != '.')
{
arr[i] = str[i];
i++;
}
i--;
int ip = 0, j = 1;
while (i >= 0)
{
ip = ip + (str[i] - '0') * j;
j = j * 10;
i--;
}
if (ip >=1 && ip <= 126) return 'A';
else if (ip >= 128 && ip <= 191) return 'B';
else if (ip >= 192 && ip <= 223) return 'C';
else if (ip >= 224 && ip <= 239) return 'D';
else return 'E';
}
void separate(char str[ ], char ipClass)

{
char network[12], host[12];
for (int k = 0; k < 12; k++)
network[k] = host[k] = '\0';
if (ipClass == 'A')
{
int i = 0, j = 0;
while (str[j] != '.')
network[i++] = str[j++];
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
else if (ipClass == 'B')
{

int i = 0, j = 0, dotCount = 0;
while (dotCount < 2)
{
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
else if (ipClass == 'C')
{
int i = 0, j = 0, dotCount = 0;
while (dotCount < 3)
{
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
else
printf("In this Class, IP address is not divided into Network and Host ID\n");
}
int main( )
{
char str[ ] = "124.180.14.16";
char ipClass = findClass(str);
printf("Given IP address belongs to Class %c\n",ipClass);
separate(str, ipClass);
return 0;
}

OUTPUT:

FINDINGS AND LEARNINGS:


 Within any network, the host ID must be unique to that network.
 Hosts that are located on the same physical network are identified by the network ID, as
all host on the same physical network are assigned the same network ID
 The network ID cannot start with 127 because 127 belongs to class A address and is
reserved for internal loop-back functions.
 Host IDs are used to identify a host within a network.
EXPERIMENT 6
AIM: Implement Distance Vector Routing Algorithm.

THEORY: The Distance vector algorithm is iterative, asynchronous and distributed.


 Distributed: It is distributed in that each node receives information from one or more of its
directly attached neighbors, performs calculation and then distributes the result back to its
neighbors.
 Iterative: It is iterative in that its process continues until no more information is available to be
exchanged between neighbors.
 Asynchronous: It does not require that all of its nodes operate in the lock step with each other.
The Distance vector algorithm is a dynamic algorithm. It is mainly used in ARPANET, and RIP. Each
router maintains a distance table known as Vector.

Knowledge about the whole network: Each router shares its knowledge through the entire network.
The Router sends its collected knowledge about the network to its neighbors.
Routing only to neighbors: The router sends its knowledge about the network to only those routers
which have direct links. The router sends whatever it has about the network through the ports. The
information is received by the router and uses the information to update its own routing table.
Information sharing at regular intervals: Within 30 seconds, the router sends the information to the
neighboring routers.

Let dx(y) be the cost of the least-cost path from node x to node y. The least costs are related by
Bellman-Ford equation,
dx(y) = minv{c(x,v) + dv(y)}

Where the minv is the equation taken for all x neighbors. After traveling from x to v, if we consider
the least-cost path from v to y, the path cost will be c(x,v)+dv(y). The least cost from x to y is the
minimum of c(x,v)+dv(y) taken over all neighbors.

CODE:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
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<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[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<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
printf("\n\n");
getch();
return 0;
}
OUTPUT:

FINDINGS & LEARNINGS:


 Knowledge about the whole network: Each router shares its knowledge through the entire
network. The Router sends its collected knowledge about the network to its neighbors.
 Routing only to neighbors: The router sends its knowledge about the network to only those
routers which have direct links. The router sends whatever it has about the network through
the ports. The information is received by the router and uses the information to update its own
routing table.
 Information sharing at regular intervals: Within 30 seconds, the router sends the information to
the neighboring routers.
 It is simpler to configure and maintain than link state routing.

Disadvantages of Distance Vector routing –


 It is slower to converge than link state.
 It is at risk from the count-to-infinity problem.
 It creates more traffic than link state since a hop count change must be propagated to all
routers and processed on each router. Hop count updates take place on a periodic basis, even if
there are no changes in the network topology, so bandwidth-wasting broadcasts still occur.
 For larger networks, distance vector routing results in larger routing tables than link state since
each router must know about all other routers. This can also lead to congestion on WAN links.
EXPERIMENT 7
AIM: Implement Link State Routing Algorithm.

THEORY:
Link state routing is a technique in which each router shares the knowledge of its neighborhood
with every other router in the internetwork.
The three keys to understand the Link State Routing algorithm:
 Knowledge about the neighborhood: Instead of sending its routing table, a router sends the
information about its neighborhood only. A router broadcast its identities and cost of the
directly attached links to other routers.
 Flooding: Each router sends the information to every other router on the internetwork except
its neighbors. This process is known as Flooding. Every router that receives the packet sends the
copies to all its neighbors. Finally, each and every router receives a copy of the same
information.
 Information sharing: A router sends the information to every other router only when the
change occurs in the information.
Link State Routing has two phases:
Reliable Flooding
 Initial state: Each node knows the cost of its neighbors.
 Final state: Each node knows the entire graph.
Route Calculation
Each node uses Dijkstra's algorithm on the graph to calculate the optimal routes to all nodes.
 The Link state routing algorithm is also known as Dijkstra's algorithm which is used to find the
shortest path from one node to every other node in the network.
 The Dijkstra's algorithm is an iterative, and it has the property that after k th iteration of the
algorithm, the least cost paths are well known for k destination nodes.

CODE:
#include <stdio.h>
#include <string.h>
int main()
{
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100], dist[100], last[100];
int flag[100];
printf("\tEnter the no of routers: ");
scanf("%d",&count);
printf("\tEnter the cost matrix values: \n");
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
printf("\t%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)
cost_matrix[i][j]=1000;
}
}
printf("\n\tEnter the source router: ");
scanf("%d",&src_router);
for(v=0;v<count;v++)
{
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
}
flag[src_router]=1;
for(i=0;i<count;i++)
{
min=1000;
for(w=0;w<count;w++)
{
if(!flag[w])
if(dist[w]<min)
{
v=w;
min=dist[w];
}
}
flag[v]=1;
for(w=0;w<count;w++)
{
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
{
dist[w]=min+cost_matrix[v][w];
last[w]=v;
}
}
}
for(i=0;i<count;i++)
{
printf("\n\t%d==>%d:Path taken: %d",src_router,i,i);
w=i;
while(w!=src_router)
{
printf("\n\t<--%d",last[w]);
w=last[w];
}
printf("\n\tShortest path cost: %d",dist[i]);
}
}

OUTPUT:

FINDINGS & LEARNINGS:


Features of link state routing protocols –
 Link state packet – A small packet that contains routing information.
 Link state database – A collection information gathered from link state packet.
 Shortest path first algorithm (Dijkstra algorithm) – A calculation performed on the database
results into shortest path
 Routing table – A list of known paths and interfaces.
Link State protocols in comparison to Distance Vector protocols have:
1. It requires large amount of memory.
2. Shortest path computations require many CPU circles.
3. If network use the little bandwidth ; it quickly reacts to topology changes
4. All items in the database must be sent to neighbors to form link state packets.
5. All neighbors must be trusted in the topology.
6. Authentication mechanisms can be used to avoid undesired adjacency and problems.
7. No split horizon techniques are possible in the link state routing.

You might also like