0% found this document useful (0 votes)
18 views58 pages

Computer Network R20 Laqb Manval New

The document is a lab manual for the Computer Networks course at D.N.R. College of Engineering & Technology, detailing exercises for students to implement various networking protocols and techniques. It includes programs for data link layer framing methods, error detection and correction, and network device studies, among others. The manual is prepared by faculty members N. Bharathi and L. Dana Ratna Kishore and is structured to guide students through practical applications in networking.

Uploaded by

alfhisk9
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)
18 views58 pages

Computer Network R20 Laqb Manval New

The document is a lab manual for the Computer Networks course at D.N.R. College of Engineering & Technology, detailing exercises for students to implement various networking protocols and techniques. It includes programs for data link layer framing methods, error detection and correction, and network device studies, among others. The manual is prepared by faculty members N. Bharathi and L. Dana Ratna Kishore and is structured to guide students through practical applications in networking.

Uploaded by

alfhisk9
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/ 58

D.N.R.

COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

CSE (R-20) 3-1sem(2024-2025)

COMPUTER NETWORKS LAB MANUAL

Prepared By:

N.Bharathi M.Tech.,
L.Dana Ratna Kishore M.Tech.,
(Assistant Professor)

D.N.R. COLLEGE OF ENGINEERING &


TECHNOLOGY
Department of Computer Science &
Engineering
BHIMAVARAM
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

TABLE OF CONTENTS
Exercise
Number Program Name

Study of Network devices in detail and connect the computers in Local


1 Area Network.
Write a Program to implement the data link layer farming methods such as
2 i) Character stuffing ii) bit stuffing.

3 Write a Program to implement data link layer farming method checksum.

Write a program for Hamming Code generation for error detection and
4 correction.
Write a Program to implement on a data set of characters the three CRC
5 polynomials – CRC 12, CRC 16 and CRC CCIP.

6 Write a Program to implement Sliding window protocol for Goback N.

Write a Program to implement Sliding window protocol for Selective


7 repeat.

8 Write a Program to implement Stop and Wait Protocol.

9 Write a program for congestion control using leaky bucket algorithm

Write a Program to implement Dijkstra‘s algorithm to compute the Shortest


10 path through a graph.
Write a Program to implement Distance vector routing algorithm by
11 obtaining routing table at each node (Take an example subnet graph with
weights indicating delay between nodes).

12 Write a Program to implement Broadcast tree by taking subnet of hosts.


Wireshark
13 i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv. Analysis and Statistics & Filters.

14 How to run Nmap scan

15 Operating System Detection using Nmap


Do the following using NS2 Simulator
16 i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate & Throughput.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 1

AIM: Study of Network devices in detail and connect the computers in


Local Area Network.

PROGRAM :

Study of Network devices

Repeater
 A Repeater operates at the physical layer of OSI Model
 Its job is to regenerate the signal over the same network before the signal
becomes too weak or corrupted so as to extend the length to which the signal
can be transmitted over the same network.
 An important point to be noted about repeaters is that they do not
amplify(increase the volume) the signal. When the signal becomes weak, they
copy the signal bit by bit and regenerate it at the original strength.
 It is a 2 port device.

Hub
 A hub is basically a multiport repeater.
 A hub connects multiple wires coming from different branches, for example, the
connector in star topology which connects different stations.
 Hubs cannot filter data, so data packets are sent to all connected devices.
 Also, they do not have the intelligence to find out the best path for data packets
which leads to inefficiencies and wastage.
 Also called as ethernet hub, active hub, network hub, repeater hub
 Operates at the physical layer of OSI Model.
Bridge
 A bridge operates at the data link layer.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

 A bridge is a repeater, with add on the functionality of filtering content by


reading the MAC addresses of source and destination.
 It is also used for interconnecting two LANs working on the same protocol.
 It has a single input and single output port, thus making it a 2 port device.
Switch
 A switch is a multiport bridge with a buffer and a design that can boost its
efficiency(a large number of ports imply less traffic) and performance.
 A switch is a data link layer device.
 The switch can perform error checking before forwarding data,which makes it
very efficient as it does not forward packets that have errors and forward good
packets selectively to the correct port only.
Router
 A Router is a device like a switch that routes data packets based on their IP
addresses. i.e. it interconnects two or more computer networks and selectively
interchanges packets of data between them.
 The router is mainly a Network Layer device.
 Routers normally connect LANs and WANs together and have a dynamically
updating routing table based on which they make decisions on routing the data
packets.
 Router divide broadcast domains of hosts connected through it.

Gateway
 A gateway, as the name suggests, is a passage to connect two networks
together that may work upon different networking models.
 They basically work as the messenger agents that take data from one system,
interpret it, and transfer it to another system.
 Gateways are also called protocol converters and can operate at any network
layer.
 Gateways are generally more complex than switches or routers.
 Gateway is also called a protocol converter. Protocol means set of rules that
must be obeyed by both sender and receiver.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Connect the computers in Local Area Network.


D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 2

AIM: Write a Program to implement the data link layer farming methods such
as i) Character stuffing ii) bit stuffing.

NAME OF THE EXPERIMENT: i) Character Stuffing.


AIM: Implement the data link layer framing methods such as character, character
stuffing. HARDWARE REQUIREMENTS: Intel based Desktop PC:-RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
The framing method gets around the problem of resynchronization after an error by having each
frame start with the ASCII character sequence DLE STX and the sequence DLE ETX. If the destination
ever losses the track of the frame boundaries all it has to do is look for DLE STX or DLE ETX
characters to figure out. The data link layer on the receiving end removes the DLE before the data are
given to the network layer. This technique is called character stuffing

Begin
Step 1: Initialize I and j as 0
Step 2: Declare n and pos as integer and a[20],b[50],ch as character
Step 3: read the string a
Step 4: find the length of the string n, i.e n-strlen(a)
Step 5: read the position, pos
Step 6: if pos > n then
Step 7: print invalid position and read again the position, pos
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 8: end if
Step 9: read the character, ch
Step 10: Initialize the array b , b[0…5] as ’d’, ’l’, ’e’, ’s’ ,’t’ ,’x’
respectively
Step 11: j=6;
Step 12: Repeat step[(13to22) until i<n
Step 13: if i==pos-1 then
Step 14: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’, ‘e’ ,’ch, ’d’, ‘l’,‘e’
respectively
Step 15: increment j by 7, i.e j=j+7
Step 16: end if
Step 17: if a[i]==’d’ and a[i+1]==’l’ and a[i+2]==’e’ then
Step 18: initialize array b, b[13…15]=’d’, ‘l’, ‘e’ respectively
Step 19: increment j by 3, i.e j=j+3
Step 20: end if
Step 21: b[j]=a[i]
Step 22: increment I and j;
Step 23: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’,‘e’ ,’e’,‘t’, ‘x’,‘\0’ respectively
Step 24: print frame after stiuffing
Step 25: print b
End

PROGRAM :

i) Character stuffing
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

char a[20],b[50],ch;
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the character\n");
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==pos-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';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

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("\nframe after stuffing:\n");
printf("%s",b);
}

Output:

NAME OF THE EXPERIMENT: ii)Bit Stuffing.

AIM: Implement the data link layer framing methods such as and bit stuffing.
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
The new technique allows data frames to contain an arbitrary number if bits and allows character
codes with an arbitrary no of bits per character. Each frame begins and ends with special bit pattern,
01111110, called a flag byte. When ever the senders data link layer encounters five consecutive one’s in
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

the data, it automatically stuffs a 0 bit into the out going bit stream. This bit stuffing is analogous to
character stuffing, in which a DLE is stuffed into the out going character stream before DLE in the data
ALGORITHM:
Begin
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0’s and
1’s) i.e.
Step 3: initialize I i=0;
Step 4: read a[i] and increment i
Step 5: Initialize i=0, j=0,count =0
Step 6: repeat step (7 to 22) until i<n
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: Initialize k=i+1;
Step 11: Increment j and b[j]= a[k];
Step 12: Increment count ;
Step 13: if count =5 then
Step 14: increment j,
Step 15: b[j] =0
Step 16: end if
Step 17: i=k;
Step 18: increment k
Step 19: else
Step 20: b[j] = a[i]
Step 21: end if
Step 22: increment I and j
Step 23: print the frame after bit stuffing
Step 24: repeat step (25 to 26) until i< j
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 25: print b[i]


Step 26: increment i
End

ii) Bit stuffing

#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 (0's & 1's 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;
}
i=k;
}

}
else
{
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

b[j]=a[i];

}
i++;
j++;

}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 3
AIM: To Write a Program to implement data link layer farming method
checksum.
NAME OF THE EXPERIMENT: ii)Bit Stuffing.

AIM: Implement the data link layer framing method checksum.

HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB


SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Checksum is the error detection method used by upper layer protocols and is considered to be more
reliable than LRC, VRC and CRC. This method makes the use of Checksum Generator on Sender side
and Checksum Checker on Receiver side.

ALGORITHM:
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0’s and
1’s) i.e.
Step 3: initialize i, j, dl, dil;
Step 4: read data1[i], data2[i] increment i
Step 5: Initialize i=0, j=0
Step 6: repeat step (7 to 22) until i < dl
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: checksum[i] = com(newdata[i]);
Step 11: Increment data1[i], data2[i]
Step 12: newdata[i] = add(newdata[i], checksum[i]);
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 13: if x == 1 && y == 1 && carry == 0


Step 14: carry = 1
Step 15: return 0
Step 16: (x == 1 && y == 1 && carry == 1
Step 17 carry = 1

Step 18: return 1


Step 19: else if
Step 20: x == 1 && y == 0 && carry == 0
Step 21: carry = 0
Step 22: return 1
Step 23: if (a == 0)

Step 24 : return 1

Step 25: else


Step 26: return 0
End

PROGRAM :

#include<stdio.h>
#include<conio.h>
int add(int, int);
int com(int);
void main()
{
int i, j, dl, dil;
int data1[10], data2[10], newdata[10], comp[10], checksum[10];
printf("\n Enter the data length=");
scanf("%d", &dl);
printf("\n Enter the data1 : \n");
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

for (i = 0; i < dl; i++)


scanf("%d", &data1[i]);
printf("\n Enter the data2 : \n");
for (i = 0; i < dl; i++)
scanf("%d", &data2[i]);
for (i = dl - 1; i >= 0; i--) {
newdata[i] = add(data1[i], data2[i]);
}

printf("\n\n Data 1 : ");


for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf("\n Data 2 : ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);

printf("\n\n The new data is : ");


for (i = 0; i < dl; i++) {
printf("%d", newdata[i]);
}
printf("\n Checksum : ");
for (i = 0; i < dl; i++) {
checksum[i] = com(newdata[i]);
printf("%d", checksum[i]);
}

printf("\n\n Receiver Side : \n");


printf("\n Data : ");
for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", checksum[i]);

printf("\n Addition : ");


for (i = dl - 1; i >= 0; i--) {
newdata[i] = add(newdata[i], checksum[i]);
}
for (i = 0; i < dl; i++) {
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

printf("%d", newdata[i]);
}
printf("\n Compliment : ");
for (i = 0; i < dl; i++) {
comp[i] = com(newdata[i]);
printf("%d", comp[i]);
}
}

int add(int x, int y) {


static int carry = 0;
if (x == 1 && y == 1 && carry == 0) {
carry = 1;
return 0;
} else if (x == 1 && y == 1 && carry == 1) {
carry = 1;
return 1;
} else if (x == 1 && y == 0 && carry == 0) {
carry = 0;
return 1;
} else if (x == 1 && y == 0 && carry == 1) {
carry = 1;
return 0;
} else if (x == 0 && y == 1 && carry == 0) {
carry = 0;
return 1;
} else if (x == 0 && y == 1 && carry == 1) {
carry = 1;
return 0;
} else if (x == 0 && y == 0 && carry == 0) {
carry = 0;
return 0;
} else {
carry = 0;
return 1;
}
}
int com(int a) {
if (a == 0)
return 1;
else
return 0;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 4

AIM: Write a program for Hamming Code generation for error detection
and correction

NAME OF THE EXPERIMENT: Hamming Code generation for error detection and correction
AIM: Implement a program for Hamming Code generation for error detection and correction
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Hamming code is an error-correcting code used to ensure data accuracy during transmission or
storage. Hamming code detects and corrects the errors that can occur when the data is moved or stored from
the sender to the receiver. This simple and effective method helps improve the reliability of communication
systems and digital storage. It adds extra bits to the original data, allowing the system to detect and correct
single-bit errors.

ALGORITHM:

Step 1: Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc).
Step 2: All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4,
8, etc).
Step 3: All the other bit positions are marked as data bits.
Step 4: Each data bit is included in a unique set of parity bits, as determined its bit position in binary form
Step 5: Parity bit 1 covers bits positions binary 1 in the least significant position (1, 3, 5, 7, 9, 11, etc).
Step 6: Parity bit 2 bits positions a 1 in the second position least significant bit (2, 3, 6, 7, 10, 11, etc).
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 7:Parity bit 4 bits a 1 in the third position from the least significant bit (4–7, 12–15, 20–23, etc).

Step 8: Parity bit 8 covers all the bits positions whose binary representation includes a 1 in the fourth
position from the least significant bit bits (8–15, 24–31, 40–47, etc).
Step 9: Each parity bit covers all bits where the bitwise AND of the parity position and the bit position is
non-zero.
Step 10: Since we check for even parity set a parity bit to 1 if the total number of ones in the positions it
checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.

PROGRAM :
#include <math.h>
#include <stdio.h>
// Store input bits
int input[32];
// Store hamming code
int code[32];
int ham_calc(int, int);
void solve(int input[], int);
// Function to calculate bit for
// ith position
int ham_calc(int position, int c_l)
{
int count = 0, i, j;
i = position - 1;
// Traverse to store Hamming Code
while (i < c_l) {

for (j = i; j < i + position; j++) {

// If current boit is 1
if (code[j] == 1)
count++;
}
// Update i
i = i + 2 * position;
}
if (count % 2 == 0)
return 0;
else
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

return 1;
}
// Function to calculate hamming code
void solve(int input[], int n)
{
int i, p_n = 0, c_l, j, k;
i = 0;
// Find msg bits having set bit
// at x'th position of number
while (n > (int)pow(2, i) - (i + 1)) {
p_n++;
i++;
}
c_l = p_n + n;
j = k = 0;
// Traverse the msgBits
for (i = 0; i < c_l; i++) {

// Update the code


if (i == ((int)pow(2, k) - 1)) {
code[i] = 0;
k++;
}

// Update the code[i] to the


// input character at index j
else {
code[i] = input[j];
j++;
}
}

// Traverse and update the


// hamming code
for (i = 0; i < p_n; i++) {
// Find current position
int position = (int)pow(2, i);
// Find value at current position
int value = ham_calc(position, c_l);
// Update the code
code[position - 1] = value;
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

// Print the Hamming Code


printf("\nThe generated Code Word is: ");
for (i = 0; i < c_l; i++) {
printf("%d", code[i]);
}
}

// Driver Code
void main()
{
// Given input message Bit
input[0] = 0;
input[1] = 1;
input[2] = 1;
input[3] = 1;
int N = 4;
// Function Call
solve(input, N);
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 5

AIM: Write a Program to implement on a data set of characters the three


CRC polynomials – CRC 12, CRC 16 and CRC CCIP.

NAME OF THE EXPERIMENT: Cyclic Redundancy Check.


AIM: Implement on a data set of characters the three CRC polynomials – CRC 12,
CRC 16 and CRC CCIP.
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

THEORY:
CRC method can detect a single burst of length n, since only one bit per column will be changed,
a burst of length n+1 will pass undetected, if the first bit is inverted, the last bit is inverted and all other
bits are correct. If the block is badly garbled by a long burst or by multiple shorter burst, the probability
that any of the n columns will have the correct parity that is 0.5. so the probability of a bad block being
expected when it should not be 2 power(-n). This scheme some times known as Cyclic Redundancy
Code
ALGORITHM/FLOWCHART:
Begin
Step 1: Declare I,j,fr[8],dupfr[11],recfr[11],tlen,flag,gen[4],genl,frl,
rem[4] as integer
Step 2: initialize frl=8 and genl=4
Step 3: initialize i=0
Step 4: Repeat step(5to7) until i<frl
Step 5: read fr[i]
Step 6: dupfr[i]=fr[i]
Step 7: increment i
Step 8: initialize i=0
Step 9: repeat step(10to11) until i<genl
Step 10: read gen[i]
Step 11: increment i
Step 12: tlen=frl+genl-1
Step 13: initialize i=frl
Step 14: Repeat step(15to16) until i<tlen
Step 15: dupfr[i]=0
Step 16: increment i
Step 17: call the function remainder(dupfr)
Step 18: initialize i=0
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 19: repeat step(20 to 21) until j<genl


Step 20: recfr[i]=rem[j]
Step 21: increment I and j
Step 22: call the function remainder(dupfr)
Step 23: initialize flag=0 and i=0
Step 24: Repeat step(25to28) until i<4
Step 25: if rem[i]!=0 then
Step 26: increment flag
Step 27: end if
Step 28: increment i
Step 29: if flag=0 then
Step 25: print frame received correctly
Step 25: else
Step 25: print the received frame is wrong
End
Function: Remainder(int fr[])
Begin
Step 1: Declare k,k1,I,j as integer
Step 2: initialize k=0;
Step 3: repeat step(4 to 14) until k< frl
Step 4: if ((fr[k] == 1) then
Step 5: k1=k
Step 6: initialize i=0, j=k
Step 7: repeat step(8 to 9) until i< genl
Step 8: rem[i] =fr[j] exponential gen[i]
Step 9: increment I and j
Step 10: initialize I = 0
Step 11: repeat step(12to13) until I <genl
Step 12: fr[k1] = rem[i]
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 13: increment k1 and i


Step 14: end if
End

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50],c[50],d[50];
int i,j,k,l;
printf("Enter frame:"); /*read the frame*/
gets(a);
strcpy(d,a);
printf("Enter generated frame:"); /*read the generated frame*/
gets(b);
l=strlen(b); /*finding length of the generator*/
for(i=0;i<l-1;i++)
strcat(a,"0");
k=strlen(a); /*finding length of the frame*/
for(i=0;i<l;i++)
c[i]=a[i];
for(i=l;i<k;i++)
{
if(c[0]=='1')
{ /* performing modulo-2 operation*/
for(j=0;j<l;j++)
c[j]=c[j]^b[j];
}
else
{
for(j=0;j<1;j++)
{
if(c[j]=='0')
c[j]='0';
else
c[j]='1';
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

}
printf("\n");
if(i!=k)
{
/* performing last modulo-2 operation and finding reminder*/
for(j=0;j<(l-1);j++)
c[j]=c[j+1];
c[j]=a[i];
}
}
printf("CRC /reminder: \n");
for(i=1;i<l;i++)
printf("%c",c[i]); /*displays the crc*/
printf("\n The transmitted frame:"); /*displays the Transmitted frame*/
printf("%c",d[i]);
for(i=1;i<l;i++)
printf("%c",c[i]);
getch();
}

Output:

EXERCISE 6

AIM: Write a Program to implement Sliding window protocol for GoBack N.

NAME OF THE EXPERIMENT: Sliding window protocol for Go Back N.


D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

AIM: implement Sliding window protocol for GoBack N.CRC 16 and


HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Go Back-N ARQ or Go Back Automatic Repeat Request is a way to implement sliding window
protocol. This protocol is used for flow control in networking and is a part of the data-link layer. The
sender’s window is of size N and the receiver’s window size is always one.

ALGORITHM/FLOWCHART:

Step1:Initializewindowsize,sent=0,ack,;
Step 2: Set(next_seq_num) to 0
Step 3: Set(window_start) to 0.
Step 4: While true
Step 5: Send frames in the window
Step 6: For i = window_start to WS-1:
Step 7: sent++;
Step 8: if(sent == windowsize)

Step 9: break;
Step 10: if(ack == windowsize)

Step 11: break;

Step 12: else


Step 13: sent = ack
Step 14: return 0;

PROGRAM :

#include<stdio.h>
int main()
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 7

AIM: Write a Program to implement Sliding window protocol for Selective


repeat.

NAME OF THE EXPERIMENT: Sliding window protocol for Selective repeat

AIM: implement Sliding window protocol for Selective repeat


D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB


SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
The go-back-n protocol works well if errors are less, but if the line is poor it wastes a lot of
bandwidth on retransmitted frames. An alternative strategy, the selective repeat protocol, is to allow the
receiver to accept and buffer the frames following a damaged or lost one. Selective Repeat attempts to
retransmit only those packets that are actually lost

ALGORITHM/FLOWCHART:

Step1:Initialize char sender[50],receiver[50];


Step 2: int i,winsize;

Step 3: ENTER THE WINDOWS SIZE


Step 4: While true
Step 5: Send frames in the window
Step 6: ENTER THE DATA TO BE SENT
Step 7: fflush(stdin);

Step 8: gets(sender);

Step 9: for(i=0;i<winsize;i++)

Step 10: receiver[i]=sender[i];

Step 11: receiver[i]=NULL;

Step 12: for(i=0;i<winsize;i++);

Step 13: puts(receiver);

Step 14: return 0;

PROGRAM :
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 8

AIM: Write a Program to implement Stop and Wait Protocol.

NAME OF THE EXPERIMENT: Implement Stop and Wait Protocol

AIM: implement Stop and Wait Protocol


HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.

THEORY:
This program defines a `frame_t` struct to represent a frame in the protocol, and three functions:
`send_frame` to send a new frame over the network and wait for an ACK, `receive_ack` to mark a frame as
acknowledged, and `stop_and_wait` to implement the Stop and Wait algorithm

ALGORITHM/FLOWCHART:
Step1: Initialize the window size (WS) to 1.

Step 2: Set the next sequence number (next_seq_num) to 0.

Step 3: While true:


Step 4: Send a frame:
Step 5: Create a new frame with data and seq_num = next_seq_num.
Step 6: . Send the frame over the network
Step 7: Wait for ACK.

Step 8: Receive ACK:

Step 9: Mark the frame as acknowledged.

Step 10: Increment next_seq_num modulo 2.

Step 11: Repeat steps 6 and 7 until all frames have been sent and acknowledged.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

PROGRAM :

SENDER.C
// ARQ - client server
// sender.c
//connection if he ready to receive else wait
// till he does not ready or timeout fails .
/*server.c and client.c - implementation of go-back-n ARQ in C
Server.c implements a reliable data transfer over UDP in C
client.c implements a reliable data transfer client over UDP in C
Both of these programs use the go-back-n ARQ, that is lost data is
automatically resent. These programs are hardcoded to transfer
to compile:
gcc -o server server.c
gcc -o client client.c
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAXLINE 80
char buffer[255] ;
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT_NUM 6000
#define t_max_retransmission 3
typedef int bit_32_var ;
typedef char bit_8_var ;
typedef struct pdu_field
{
bit_32_var SN ;
bit_8_var data[MAXLINE] ;
bit_8_var status ;
}PDU_FIELD;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

void str_cli ( FILE *fp , bit_32_var sfd )


{
bit_32_var no_of_data , counter_1 , counter_2 ;
static bit_32_var retransmission_counter , intial_readycheck_counter ;
PDU_FIELD send_data[MAXLINE] ,recv_data[MAXLINE] ;
printf ( "enter how many data you have to send : " ) ;
scanf ( "%d", &no_of_data ) ;
for ( counter_1 = 0 ; counter_1 < no_of_data ; counter_1++ )
{
printf ( "enter %d'th data : ",counter_1 ) ;
scanf ( "%s", send_data[counter_1].data ) ;
send_data[counter_1].SN = counter_1 ;
send_data[counter_1].status = 0 ;
}
for ( counter_1 = 0 ; counter_1 < no_of_data ; counter_1++ )
{
write ( sfd , &send_data[counter_1] , sizeof ( send_data[counter_1] ) ) ;
read ( sfd , &recv_data[counter_1] , sizeof ( recv_data[counter_1] ) ) ;
if ( counter_1 == 0 && ( strcmp ( recv_data[counter_1].data , "yes" ) != 0 ) )
{
if ( intial_readycheck_counter == t_max_retransmission )
{
printf ( "client : receiver not there , better to exit : \n" ) ;
exit ( 1 ) ;
}
intial_readycheck_counter++ ;
printf ( "client : receiver is not ready : wait 10sec... \n" ) ;
sleep ( 10 ) ;
counter_1 = counter_1- 1 ;
}

if ( counter_1 > 0 )
{
if ( recv_data[counter_1].SN == send_data[counter_1].SN + 1 )
printf ( "\n server responding = %s ", recv_data[counter_1].data ) ;
else
{
if ( retransmission_counter < t_max_retransmission )
{
printf ( "incorrect ack - sending the same data - \n" ) ;
counter_1 -= 1 ;
retransmission_counter++ ;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

}
else
{
printf ( "Time out : sending next data \n " ) ;
retransmission_counter = 0 ;
}

}
}
}
write ( sfd , "Nothingtotransmit:\n" , 10 ) ;
printf ( "Transmission finished : \n" ) ;
exit ( 1 ) ;
}
int main ()
{
bit_32_var socket_fd ;
struct sockaddr_in config_client ;
socket_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ;
if ( socket_fd < 0 )
{
printf ( "client : failed to create socket \n" ) ;
exit ( 1 ) ;
}
memset ( &config_client , 0 , sizeof ( struct sockaddr_in )) ;
config_client.sin_family = AF_INET ;
config_client.sin_port = htons ( SERVER_PORT_NUM ) ;
inet_aton ( SERVER_ADDR , &config_client.sin_addr ) ;
connect ( socket_fd , (struct sockaddr *)&config_client , sizeof ( config_client ) ) ;
printf ( "connect successfully\n" ) ;
system ( "clear" ) ;
str_cli ( stdin , socket_fd ) ;
exit ( 0 );
}

RECEIVE.C

Server.c implements a reliable data transfer over UDP in C


client.c implements a reliable data transfer client over UDP in C
Both of these programs use the go-back-n ARQ, that is lost data is
automatically resent. These programs are hardcoded to transfer
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

to compile:
gcc -o server server.c
gcc -o client client.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define SUCCESS 1
#define FAILURE 0
#define NO_OF_FRAMES 10
typedef int BIT_32_VAR_INT ;
typedef char BIT_8_VAR_CHAR ;
void error ( BIT_8_VAR_CHAR *msg )
{
perror ( msg ) ;
exit ( 1 ) ;
}
typedef struct pdu_data
{
BIT_32_VAR_INT SN ;
BIT_8_VAR_CHAR data[80] ;
BIT_32_VAR_INT status ;
}PDU_DATA;
int main () {
PDU_DATA send_data[10] , recv_data[10] ;
BIT_32_VAR_INT socket_fd , newsocket_fd , port_no , client_len ;
// char buffer[256] ;
struct sockaddr_in serv_addr , cli_addr ;
int sizeof_read_data , counter_1 ;
//char buf[256] ;
socket_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ;
if ( socket_fd < 0 )
{
error ( "error in socket opening " ) ;
}

bzero ( ( char * ) &serv_addr , sizeof ( serv_addr ) ) ;


port_no = atoi ( "7100" ) ;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = INADDR_ANY ;
serv_addr.sin_port = htons ( 6000 ) ;

if ( bind ( socket_fd , ( struct sockaddr * ) &serv_addr , sizeof ( serv_addr ) ) < 0 )


error ( "error in binding" ) ;

listen ( socket_fd , 5 ) ;

client_len = sizeof ( cli_addr ) ;


newsocket_fd = accept ( socket_fd , ( struct sockaddr * ) &cli_addr , &client_len ) ;

if ( newsocket_fd < 0 )
error ( "error in accept " ) ;

// bzero ( buffer , 256 ) ;

while ( 1 )
{

for ( counter_1 = 0 ; counter_1 < NO_OF_FRAMES ; counter_1++ )


{

sizeof_read_data = read ( newsocket_fd , &recv_data[counter_1] , sizeof ( recv_data[counter_1]) ) ;

if ( sizeof_read_data < 0 )
error ( "error in reading from socket " ) ;

printf ( "client sended : %s with SN %d :\n" , recv_data[counter_1].data , recv_data[counter_1].SN) ;

printf ( "enter a reply to client : " ) ;


scanf ( "%s",send_data[counter_1].data ) ;

printf ( "\nenter a SN for next you want to receive : " ) ;


scanf ( "%d",&send_data[counter_1].SN ) ;

if ( send_data[counter_1].SN <= recv_data[counter_1].SN )


counter_1 -= 1 ;

send_data[counter_1].status = SUCCESS ;
write ( newsocket_fd , &send_data[counter_1] , sizeof ( send_data[counter_1]) ) ;
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

bzero ( &recv_data[counter_1].data , sizeof ( recv_data[counter_1].data ) ) ;


read ( newsocket_fd , &recv_data[counter_1] , sizeof ( recv_data[counter_1] ) ) ;
printf ( "it's end Thank YOu : \n" ) ;
exit ( 1 ) ;
}
return 0 ;
}

Output:

EXERCISE 9

AIM: Write a program for congestion control using leaky bucket algorithm

NAME OF THE EXPERIMENT: congestion control using leaky bucket algorithm


AIM: implement congestion control using leaky bucket algorithm
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

SOFTWARE REQUIREMENTS: Turbo C / Borland C.

THEORY:
In the network layer, before the network can make Quality of service guarantees, it must know what
traffic is being guaranteed. One of the main causes of congestion is that traffic is often bursty.

ALGORITHM/FLOWCHART:

Step1: Initialize the bucket size (BUCKET_SIZE) and leak rate (LEAK_RATE).
Step 2: Add data to the bucket at a constant rate (DATA_RATE).

Step 3: If the bucket is full (i.e., the amount of data in the bucket is greater than or equal to
BUCKET_SIZE), discard any additional data.

Step 4: Remove data from the bucket at a constant rate (LEAK_RATE).

Step 5: If the bucket is empty, no data is transmitted.

Step 6: Transmit data if the bucket is not empty


Step 7: Repeat steps 2-6 until the program is terminated

Step 11: Repeat steps 6 and 7 until all frames have been sent and acknowledged
PROGRAM :

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{ int
packets[8],i,j,clk,b_size,o_rate,i_rate,p_sz_rm=0,p_sz,p_time;
for(i=0;i<5;++i)
{ packets[i]=rand()%10;
if(packets[i]==0) --i;
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

printf("Enter output rate:");


scanf("%d",&o_rate);
printf("\nEnter bucket size:");
scanf("%d",&b_size);
for(i=0;i<5;++i)
{ if((packets[i]+p_sz_rm) > b_size)
{
if(packets[i]>b_size)
printf("\nIncoming packet size:%d greater than bucket capacity\n",packets[i]);
else
printf("Bucket size exceeded\n");
}
else
{
p_sz=packets[i];
p_sz_rm+=p_sz;
printf("\n--------------------------------------------------\n");
printf("Incoming packet:%d",p_sz);
printf("\nTransmission left:%d\n",p_sz_rm);
p_time=rand()%10;
printf("Next packet will come at %d",p_time);
for(clk=0;clk<p_time&&p_sz_rm>0;++clk)
{
printf("\nTime left %d---No packets to transmit!!\n",p_time-clk);
sleep(1);
if(p_sz_rm)
{ printf("Transmitted\n");
if(p_sz_rm<o_rate)
p_sz_rm=0;
else p_sz_rm-=o_rate;
printf("Bytes remaining:%d\n",p_sz_rm);
}
else printf("No packets to transmit\n");
}
}
}
getch();
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 10
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

AIM: Write a Program to implement Dijkstra ‘s algorithm to compute the


shortest path through a graph.

NAME OF THE EXPERIMENT: Shortest Path.


AIM: Implement Dijkstra‘s algorithm to compute the Shortest path thru a given graph.

HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB


SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:

ALGORITHM/FLOWCHART:
Begin
Step1: Declare array path [5] [5], min, a [5][5], index, t[5];
Step2: Declare and initialize st=1,ed=5
Step 3: Declare variables i, j, stp, p, edp
Step 4: print “enter the cost “
Step 5: i=1
Step 6: Repeat step (7 to 11) until (i<=5)
Step 7: j=1
Step 8: repeat step (9 to 10) until (j<=5)
Step 9: Read a[i] [j]
Step 10: increment j
Step 11: increment i
Step 12: print “Enter the path”
Step 13: read p
Step 14: print “Enter possible paths”
Step 15: i=1
Step 16: repeat step(17 to 21) until (i<=p)
Step 17: j=1
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 18: repeat step(19 to 20) until (i<=5)


Step 19: read path[i][j]
Step 20: increment j
Step 21: increment i
Step 22: j=1
Step 23: repeat step(24 to 34) until(i<=p)
Step 24: t[i]=0
Step 25: stp=st
Step 26: j=1
Step 27: repeat step(26 to 34) until(j<=5)
Step 28: edp=path[i][j+1]
Step 29: t[i]= [ti]+a[stp][edp]
Step 30: if (edp==ed) then
Step 31: break;
Step 32: else
Step 33: stp=edp
Step 34: end if
Step 35: min=t[st]
Step 36: index=st
Step 37: repeat step( 38 to 41) until (i<=p)
Step 38: min>t[i]
Step 39: min=t[i]
Step 40: index=i
Step 41: end if
Step 42: print” minimum cost” min
Step 43: print” minimum cost pth”
Step 44: repeat step(45 to 48) until (i<=5)
Step 45: print path[index][i]
Step 46: if(path[idex][i]==ed) then
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step 47: break


Step 48: end if
End

PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],path[5][5],st=1,stp,ed=5,edp,i,j,p,t[5],min,index;
printf("enter the cost matrix:\n"); /*reading the cost matrix*/
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths:\n"); /*reading the no.of paths*/
scanf("%d",&p);
printf("enter the possible paths:\n"); /*reading the possible paths*/
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
ed=path[i][j+1];
t[i]=t[i]+path[stp][edp]; /*calculating the distance*/
if(edp==ed) /*checking if already transformed*/
break;
else
stp=edp;
}
}
min=t[st];
index=st;
for(i=1;i<=p;i++) /*finding the minimum distance path*/
{
if(min>t[i])
{
min=t[i];
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

index=i;
}
}
printf("\n minimum cost %d",min);
printf("\n minimum cost path:");
for(i=1;i<=p;i++)
{
printf("-->%d",path[index][i]); /*displaying minimum cost path*/
if(path[index][i] == ed)
break;
}
getch();
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 11

AIM: Write a Program to implement Distance vector routing algorithm by


obtaining routing table at each node (Take an example subnet graph with
weights indicating delay between nodes).

AIM: Obtain Routing table at each node using distance vector routing algorithm for a
given subnet.

HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB


SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Distance Vector Routing Algorithms calculate a best route to reach a destination based solely on
distance. E.g. RIP. RIP calculates the reach ability based on hop count. It’s different from link state
algorithms which consider some other factors like bandwidth and other metrics to reach a destination.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Distance vector routing algorithms are not preferable for complex networks and take longer to converge.

ALGORITHM:
Begin
Step1: Create struct node unsigned dist[20],unsigned from[20],rt[10]
Step2: initialize int dmat[20][20], n,i,j,k,count=0,
Step3: write "the number of nodes "
Step4: read the number of nodes "n"
Step5: write" the cost matrix :"
Step6: intialize i=0
Step7: repeat until i<n
Step8: increment i
Step9: initialize j=0
Step10: repeat Step(10-16)until j<n
Step11: increment j
Step12:read dmat[i][j]
Step13:intialize dmat[i][j]=0
Step14:intialize rt[i].dist[j]=dmat[i][j]
Step15:intialize rt[i].from[j]=j
Step16:end
Step17:start do loop Step (17-33)until
Step18:intilialize count =0
Step19:initialize i=0
Step20:repeat until i<n
Step21:increment i
Step22:initialize j=0
Step23:repeat until j<n
Step24:increment j
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

Step25:initialize k=0
Step26:repeat until k<n
Step27:increment k
Step28:if repeat Step(28-32) until rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]
Step29:intialize rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]
Step30:intialize rt[i].from[j]=k;
Step31:increment count
Step32:end if
Step33:end do stmt
Step34:while (count!=0)
Step35:initialize i=0
Step36:repeat Steps(36-44)until i<n
Step37:increment i
Step38:write ' state values for router',i+1
Step39:initialize j=0
Step40:repeat Steps ( 40-43)until j<n
Step41:increment j
Step42:write 'node %d via %d distance % ',j+1,rt[i].from[j]+1,rt[i].dist[j]
Step43:end
Step44:end
PROGRAM :

#include<stdio.h>
#include<conio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

printf("\n Enter no of nodes:");


scanf("%d",&n);
printf("\n Enter cost matrix:");
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("\n state value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\n node %d via %d distance %d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n");
getch();
}

Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

EXERCISE 12

AIM: Write a Program to implement Broadcast tree by taking subnet of


hosts.
NAME OF THE EXPERIMENT: Broadcast Tree.
AIM: Implement broadcast tree for a given subnet of hosts
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
This technique is widely used because it is simple and easy to understand. The idea of
this algorithm is to build a graph of the subnet with each node of the graph representing a router and
each arc of the graph representing a communication line. To choose a route between a given pair of
routers the algorithm just finds the broadcast between them on the graph.

ALGORITHM/FLOWCHART:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

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]
step22: Initialize i=0
step23: repeat step(23-43) until i<n
step24: increment i
step25: Initialize j=0
step26: repeat until j<n
step27: increment j
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

step28: if'edge[i]j]!=99
step29: if'min>edge[i][j] repeat step (29-32)
step30: intialize min=edge[i][j]
step31: intialize u=i
step32: intialize v=j
step33: calling function p=find(u);
step34: calling function q=find(v);
step35: if'P!=q repeat steps(35-39)
step36: intialize t[i][0]=u
step37: intialize t[i][1]=v
step38: initialize mincost=mincost+edge[u][v]
step39: call function sunion(p,q)
step40: else repeat steps(40-42)
step41: Intialize t[i][0]=-1;
step42: Intialize t[i][1]=-1;
step43: intialize min=99;
step44; write"Minimum cost is %d\n Minimum spanning tree is",mincost
step45: Initialize i=0
step46: repeat until i<n
step47: increment i
step48: if't[i][0]!=-1 && t[i][1]!=-1'repeat step(48-50)
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

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;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

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:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)

You might also like