Computer Networks Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

YENEPOYA INSTITUTE OF TECHNOLOGY

(Approved by AICTE, New Delhi and Affiliated to VTU, Belagavi, karnataka)

Thodar, Moodbidri – 574225, D.K.

DEPARTMENT OF ELECTRONICS AND


COMMUNICATION ENGINEERING

VI SEMESTER

LAB MANUAL

COURSE: COMPUTER NETWORKS LAB

COURSE CODE: 18ECL76


Computer Networks Lab

Vision of the Department:

To inspire innovations so as to carve a niche in the field of Electronics and Communication


Engineering by inculcating a spirit of creative thinking and train the students in present
technologies to meet industrial as well as social needs.

Mission of the Department:

1. To keep abreast with current trends and technology that support the students to excel in the
area of Electronics and Communication Engineering.
2. Provide ethical and value based education by promoting activities addressing the social and
industrial needs.
3. Equip students with a broad intellectual spectrum to enable them for continuing education.

Program Educational Objectives (PEOs)

1. Excel in professional career and/or higher education by acquiring required up to date


knowledge in area of Electronics and Communication Engineering.
2. Analyze real life problems, design appropriate systems to provide solutions that are
technically sound, economically feasible and socially acceptable.
3. Exhibit professionalism, ethical attitude, communication skills, team work in their profession
and adapt to current trends by engaging in lifelong learning.

ECE Dept., YIT Page 1


Computer Networks Lab

Course Learning Objectives:

This course will enable students to:

• Choose suitable tools to model a network and understand the protocols at various OSI reference
levels.

• Design a suitable network and simulate using a Network simulator tool.

• Simulate the networking concepts and protocols using C/C++ programming.

• Model the networks for different configurations and analyze the results

Course Outcomes (COs):

At the end of this course student will be able to

1. 18ECL76.1: develop skills for observing and verifying the experimental details and to write
clear lab reports.
2. 18ECL76.2: design a suitable network and simulate using a Network simulator tool.
3. 18ECL76.3: demonstrate the working of various protocols and algorithms using C/C++
programming.
4. 18ECL76.4: gain sound knowledge in the field of Computer Networks.

ECE Dept., YIT Page 2


Computer Networks Lab

Laboratory Experiments

PART-A: Simulation experiments using NS2


1. Implement a point to point network with four nodes and duplex links between them.
Analyze the network performance by setting the queue size and varying the bandwidth.

2. Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. Apply
TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over
TCP and UDP agents changing the parameter and determine the number of packets sent
by TCP/UDP.

3. Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by changing the
error rate and data rate.

4. Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and
obtain congestion window for different sources/ destinations.

5. Implement ESS with transmission nodes in Wireless LAN and obtain the performance
parameters.

6. Implementation of Link state routing algorithm.

PART-B: Implement the following in C/C++

1. Write a program for a HLDC frame to perform the following.


i) Bit stuffing
ii) Character stuffing.

2. Write a program for distance vector algorithm to find suitable path for transmission.

3. Implement Dijkstra’s algorithm to compute the shortest routing path.

4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the program
for the cases
a. Without error
b. With error

5. Implementation of Stop and Wait Protocol and Sliding Window Protocol.

6. Write a program for congestion control using leaky bucket algorithm.

ECE Dept., YIT Page 3


Computer Networks Lab

INTRODUCTION TO COMPUTER NETWORKS LAB


The purpose of this lab is to acquaint the students with an overview of the Computer Networks
from the perspective how the information is transferred from source to destination in real time,
simulation of various topologies and different layers in networks using Network Simulator tool.
Network simulation is an important tool in developing, testing and evaluating network
protocols. Simulation can be used without the target physical hardware, making it economical
and practical for almost any scale of network topology and setup. It is possible to simulate a link
of any bandwidth and delay, even if such a link is currently impossible in the real world. With
simulation, it is possible to set each simulated node to use any desired software. This means that
meaning deploying software is not an issue. Results are also easier to obtain and analyze,
because extracting information from important points in the simulated network is as done by
simply parsing the generated trace files.

1. What is NS2
NS2 stands for Network Simulator Version 2. It is an open-source event-driven simulator
designed specifically for research in computer communication networks.

2. Features of NS2
1. It is a discrete event simulator for networking research and primarily UNIX based.
2. It provides substantial support to simulate bunch of protocols like TCP, FTP, UDP, https
and DSR.
3. It simulates wired and wireless network.
4. Uses TCL as its scripting language, Otcl: Object oriented support.
5. Tclcl: C++ and otcl linkage.
6. Discrete event scheduler

3. Basic Architecture
NS2 consists of two key languages: C++ and Object-oriented Tool Command Language (OTcl).
While the C++ defines the internal mechanism (i.e., a backend) of the simulation objects, the
OTcl sets up simulation by assembling and configuring the objects as well as scheduling discrete
events. The C++ and the OTcl are linked together using Tcl.

ECE Dept., YIT Page 4


Computer Networks Lab

Theory
TCP/IP and OSI model

Summary of layers

ECE Dept., YIT Page 5


Computer Networks Lab

Basic networking commands

a. hostname
hostname with no options displays the machines host name.
hostname – d displays the domain name the machine belongs to.
hostname – f displays the fully qualified host and domain name.
hostname – i displays the IP address for the current machine.

b. ping
It sends packets of information to the user-defined source. If the packets are received, the
destination device sends packets back. Ping can be used for two purposes:
1. To ensure that a network connection can be established.
2. Timing information as to the speed of the connection.
If you do ping www.yahoo.com it will display its IP address. Use ctrl+ to stop the test.

c. ifconfig
View network configuration, it displays the current network adapter configuration. It is
handy to determine if you are getting transmit (TX) or receive (RX) errors.

d. netstat
Most useful and very versatile for finding connection to and from the host. You can find
out all the multicast groups (network) subscribed by this host by issuing "netstat -g"
netstat -nap | grep port will display process id of application which is using that port
netstat -a or netstat –all will display all connections including TCP and UDP
netstat --tcp or netstat –t will display only TCP connection
netstat --udp or netstat –u will display only UDP connection
netstat -g will display all multicast network subscribed by this host.

e. nslookup
If you know the IP address it will display hostname. To find all the IP addresses for a
given domain name, the command nslookup is used. You must have a connection to the
internet for this utility to be useful. You can also use nslookup to convert hostname to IP
Address and from IP Address from hostname.

f. traceroute
A handy utility to view the number of hops and response time to get to a remote system
or web site is traceroute. Again you need an internet connection to make use of this tool.

g. finger
View user information, displays a user’s login name, real name, terminal name and write
status. this is pretty old unix command and rarely used now days.

h. telnet
Connects destination host via telnet protocol, if telnet connection establish on any port
means connectivity between two hosts is working fine.

ECE Dept., YIT Page 6


Computer Networks Lab

i. Dig
domain information groper query DNS related information like a Record, CNAME, MX
Record etc. This command mainly uses to troubleshoot DNS related query.

j. Route
shows and manipulate ip routing table. To see default routing table in Linux, type the
following command.

k. host
command to find name to IP or IP to name in IPv4 or IPv6 and also query DNS records.

l. ARP
Address Resolution Protocol is useful to view / add the contents of the kernel’s ARP
tables.

m. system-config-network
to configure network setting and you will get nice Graphical User Interface (GUI) which
may also use to configure IP Address, Gateway, DNS etc.

n. tcpdump
most powerful and widely used command-line packets sniffer or package analyzer tool
which is used to capture or filter TCP/IP packets that received or transferred over a
network on a specific interface.

ECE Dept., YIT Page 7


Computer Networks Lab

„Instructions to the Students‟

1. Should come on time with thorough preparation for the experiment to be


conducted.
2. Should bring the practical record fully completed in all respects pertaining to
the experiment conducted in the previous lab sessions.
3. After completion of the program, certification of the concerned Faculty -in-
charge in the observation book is necessary.
4. Turn off the systems/Fans/Lights after use.
5. Keep the Computer lab premises clean and tidy.
6. Do not misbehave in the computer laboratory.
7. Do not touch, connect or disconnect any plug/cable/flash drive without your
Faculty/laboratory technician’s permission.
8. Do not read or modify other users' files.

9. Replace the materials/equipment in proper place after work.


10.Ask your Faculty-in-charge or lab technician for assistance if you have any
problem.

ECE Dept., YIT Page 8


Computer Networks Lab

Procedure

 Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab1.tcl &

 Save the program

 Open gedit editor and type awk program. Program name should have the extension “.awk ”

[root@localhost ~]# gedit lab1.awk &

 Save the program

 Run the program

[root@localhost~]# ns lab1.tcl

 Here “ns” indicates network simulator. We get the topology as per the program.

 Now press the play button in the simulation window and the simulation will begins.

 After simulation is completed run awk file to see the output ,

[root@localhost~]# awk –f lab1.awk lab1.tr

 To see the trace file contents open the file as ,

[root@localhost~]# gedit lab1.tr &

Trace file contains 12 columns:


Event type, Event time, From Node, To Node, Packet Type, Packet Size, Flags (indicated by -----
---), Flow ID, Source address, Destination address, Sequence ID, Packet ID

ECE Dept., YIT Page 9


Computer Networks Lab

Implementation of wired network topology and wireless network Topology


FOR WIRED TOPOLOGY ALGORITHMS:
(1) Define new simulator.
(2) Create n numbers of nodes.
(3) Set the link between each of the nodes.
(4) Create agent
(5) Attach the agent
(6) Trace the protocols using CBR & FTP.
(7) Define a’ finish’ procedure –to flush trace record in the trace and trace output files.
(8) Schedule events for simulation.
(9) Run the simulation.

FOR WIRELESS TOPOLOGY ALGORITHMS:


(1) Define new simulator.
(2) Define the network components. Such as the type of antenna, the radio- propagation
model, the type of ad-hoc routing protocol used by mobile node.
(3) Create n numbers of nodes.
(4) Set the link between each of the nodes
(5) Create agent.
(6) Attach the agent.
(7) Trace the protocols using application type.
(8) Define a’ finish’ procedure –to flush trace record in the trace and trace output files.
(9) Schedule events for simulation.
(10) Run the simulation.

ECE Dept., YIT Page 10


Computer Networks Lab

Experiments: Part – A
1. Implement a point to point network with four nodes and duplex links between them.
Analyze the network performance by setting the queue size and varying the bandwidth.
File: lab1.tcl
#Create a new Simulation Instance
set ns [ new Simulator ]

#Turn on the Trace and the animation files


set tf [ open lab1.tr w ]
$ns trace-all $tf
set nf [ open lab1.nam w ]
$ns namtrace-all $nf

# create the nodes.


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# Give color to the packets.


$ns color 1 "red"
$ns color 2 "blue"

#Label the nodes


$n0 label "Source/udp0"
$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/Null"

# Create the Topology and Vary the Bandwidth


$ns duplex-link $n0 $n2 10Mb 300ms DropTail
$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 1Mb 300ms DropTail

# set the queue size b/w the nodes


$ns set queue-limit $n0 $n2 10
$ns set queue-limit $n1 $n2 10

ECE Dept., YIT Page 11


Computer Networks Lab

$ns set queue-limit $n2 $n3 5

#The below code is used to attach an UDP agent to n0, UDP agent to n1 and null agent to n3.
#Create a CBR Traffic source and attach to the UDP Agent
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

# set the udp0 packets to red and udp1 packets to blue color
$udp0 set class_ 1
$udp1 set class_ 2

# connect the agents.


$ns connect $udp0 $null3
$ns connect $udp1 $null3

# set the packet size and interval


$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005

#Define the finish procedure to perform at the end of the simulation


proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab1.nam &
close $tf
close $nf
exit 0
}
#Schedule the Events
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"

ECE Dept., YIT Page 12


Computer Networks Lab

$ns at 10.0 "finish"


$ns run

File: lab1.awk

#!/usr/bin/awk -f
BEGIN{
cbrPkt=0;
tcpPkt=0;
}

{
if(($1 == "d")&&($5 == "cbr")) {
cbrPkt = cbrPkt + 1;
}
if(($1 == "d")&&($5 == "tcp")) {
tcpPkt = tcpPkt + 1;
}
}

END {
printf "\nNo. of CBR Packets Dropped %d", cbrPkt;
printf "\nNo. of TCP Packets Dropped %d", tcpPkt;
}

ECE Dept., YIT Page 13


Computer Networks Lab

Screenshots of Output

ECE Dept., YIT Page 14


Computer Networks Lab

2. Implement a four node point to point network with links n0-n2, n1-n2 and n2-n3. Apply
TCP agent between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP
and UDP agents changing the parameter and determine the number of packets sent by
TCP/UDP.

Code:
set ns [new Simulator]
set tf [open lab2.tr w]
$ns trace-all $tf
set nf [open lab2.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# The below code is used to set the color and name's to the nodes.

$ns color 1 "red"


$ns color 2 "blue"
$n0 label "source/TCP"
$n1 label "source/UDP"
$n2 label "Router"
$n3 label "destination"
$ns duplex-link $n0 $n2 100Mb 1ms DropTail
$ns duplex-link $n1 $n2 100Mb 1ms DropTail
$ns duplex-link $n2 $n3 100Mb 1ms DropTail

# The below code is used to set the color and labels to the links.

$ns duplex-link-op $n0 $n2 color "green"


$ns duplex-link-op $n0 $n2 label "from 0-2"
$ns duplex-link-op $n1 $n2 color "green"
$ns duplex-link-op $n1 $n2 label "from 1-2"
$ns duplex-link-op $n2 $n3 color "green"
$ns duplex-link-op $n2 $n3 label "from 2-3"

# The below code is used create TCP and UDP agents and the traffic ftp & cbr respectively.

set tcp0 [new Agent/TCP]

ECE Dept., YIT Page 15


Computer Networks Lab

$ns attach-agent $n0 $tcp0


set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3

#The below code is used to set the packet size of ftp and udp.

$ftp0 set packetSize_ 500


$ftp0 set interval_ 0.001

#The below code is used to increase the data rate(if the interval is more than the more number of
#packets goes to destination).

$cbr1 set packetSize_ 500


$cbr1 set interval_ 0.001

#This code is used give a color red->tcp and blue ->udp.

$tcp0 set class_ 1


$udp1 set class_ 2

# The below code is used connect the agents.

$ns connect $tcp0 $sink3


$ns connect $udp1 $null3
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab2.nam &
close $nf
close $tf
exit 0
}

ECE Dept., YIT Page 16


Computer Networks Lab

$ns at 0.1 "$cbr1 start"


$ns at 0.2 "$ftp0 start"
$ns at 5.0 "finish"
$ns run

File: lab2.awk
#!/usr/bin/awk -f
BEGIN {
TCPSend=0;
CBRSend=0;
TCPDrop=0;
CBRDrop=0;
TCPDropRatio=0.0;
UDPDropRatio=0.0;
TCPArrivalRatio=0.0;
CBRArrivalRatio=0.0;
}

{
src=$3;
des=$4;
type=$5;
event=$1;
if((src=="0")&&(des=="2")&&(event=="+")) {
TCPSend++;
}
if((src=="1")&&(des=="2")&&(event=="+")) {
CBRSend++;
}
if((event=="d")&&(type=="tcp")) {
TCPDrop++;
}
if((event=="d")&&(type=="cbr")) {
CBRDrop++;
}
}

END {
printf "\nTCPSend %d", TCPSend;
printf "\nCBRSend %d", CBRSend;
printf "\nTCPDrop %d", TCPDrop;
printf "\nCBRDrop %d", CBRDrop;

TCPArrivalRatio=((TCPSend-TCPDrop)/TCPSend);
TCPDropRatio=(TCPDrop/TCPSend);

ECE Dept., YIT Page 17


Computer Networks Lab

UDPArrivalRatio=((CBRSend-CBRDrop)/CBRSend);
UDPDropRatio=(CBRDrop/CBRSend);

printf "\nTCPArrivalRatio %f", TCPArrivalRatio;


printf "\nTCPDropRatio %f", TCPDropRatio;
printf "\nUDPArrivalRatio %f", UDPArrivalRatio;
printf "\nUDPDropRatio %f", UDPDropRatio;
}

ECE Dept., YIT Page 18


Computer Networks Lab

3. Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by changing
the error rate and data rate.

set ns [new Simulator]


set tf [open lab3.tr w]
$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf

$ns color 0 blue

set n0 [$ns node]


$n0 color "red"
set n1 [$ns node]
$n1 color "red"
set n2 [$ns node]
$n2 color "red"
set n3 [$ns node]
$n3 color "red"
set n4 [$ns node]
$n4 color "purple"
set n5 [$ns node]
$n5 color "purple"
set n6 [$ns node]
$n6 color "purple"
set n7 [$ns node]
$n7 color "purple"

$n1 label "Source/UDP"


$n3 label "Error node"
$n7 label "Destination"

set lan [$ns newLan "$n0 $n1 $n2 $n3" 100Mb 300ms LL Queue/DropTail Mac/802_3]
set lan [$ns newLan "$n4 $n5 $n6 $n7" 100Mb 300ms LL Queue/DropTail Mac/802_3]
$ns duplex-link $n3 $n4 100Mb 300ms DropTail
$ns duplex-link-op $n3 $n4 color "green"

# set error rate. Here ErrorModel is a class and it is single word and space should not be given
#between Error and Model.
# lossmodel is a command and it is single word. Space should not be given between loss and
#model.
set err [new ErrorModel]
$ns lossmodel $err $n3 $n4
$err set rate_ 0.1
# error rate should be changed for each output like 0.1,0.3,0.5…. */

ECE Dept., YIT Page 19


Computer Networks Lab

set udp [new Agent/UDP]


$ns attach-agent $n1 $udp
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set fid_ 0
$cbr set packetSize_ 1000
$cbr set interval_ 0.0001
set null [new Agent/Null]
$ns attach-agent $n7 $null
$ns connect $udp $null

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab3.nam &
exit 0
}
$ns at 0.1 "$cbr start"
$ns at 2.0 "$cbr stop"
$ns at 3.0 "finish"
$ns run

File : lab3.awk

begin {
pkt = 0;
time = 0;
udpacket = 0;
}
{
if($1=="r" && $3=="3" && $4=="4" && $5=="cbr")
{
pkt = pkt+$6;
time=$2;
udpacket++;
}
}
END {
printf "througput : %f MBPS\n",((pkt / time)*(8/1000000));
printf "the packets received is : %d\n",udpacket;
printf "the packets calculated is : %d\n",pkt;
printf "the time duration is : %d\n",time;
}

ECE Dept., YIT Page 20


Computer Networks Lab

ECE Dept., YIT Page 21


Computer Networks Lab

4. Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and
obtain congestion window for different sources/ destinations.

set ns [new Simulator]


set tf [open lab4.tr w]
$ns trace-all $tf
set nf [open lab4.nam w]
$ns namtrace-all $nf

$ns color 1 "blue"


$ns color 2 "green"
set n0 [$ns node]
$n0 color "magenta"
$n0 label "src1"
set n1 [$ns node]
set n2 [$ns node]
$n2 color "magenta"
$n2 label "src2"
set n3 [$ns node]
$n3 color "blue"
$n3 label "dest2"
set n4 [$ns node]
set n5 [$ns node]
$n5 color "blue"
$n5 label "dest1"

set lan [$ns newLan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3]
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5
$ns connect $tcp0 $sink5
set tcp2 [new Agent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600
$ftp2 set interval_ 0.001
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3

ECE Dept., YIT Page 22


Computer Networks Lab

$tcp0 set fid_ 1


$tcp2 set fid_ 2
set file1 [open file1.tr w]
$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp0 trace cwnd_
$tcp2 trace cwnd_

proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab4.nam &
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"
$ns run

awk file:
begin {
}
{ if ($6=="cwnd_")
printf ("%f\t%f\t\n", $1, $7);
}
end {
}
Command:
-f awk filename.awk filename1.tr > a1
-f awk filename.awk filename2.tr > a2

To get Graph:
xgraph a1 a2

ECE Dept., YIT Page 23


Computer Networks Lab

ECE Dept., YIT Page 24


Computer Networks Lab

5. Implement ESS with transmission nodes in Wireless LAN and obtain the performance
parameters.

set ns [new Simulator]


set tf [open lab8.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open lab8.nam w]
$ns namtrace-all-wireless $nf 1000 1000

$ns node-config -adhocRouting AODV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

$n0 label "tcp0"


$n1 label "sink1/tcp1"
$n2 label "sink2"

$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 600

ECE Dept., YIT Page 25


Computer Networks Lab

$n2 set Y_ 600


$n2 set Z_ 0

$ns at 0.1 "$n0 setdest 50 50 15"


$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2

$ns at 5 "$ftp0 start"


$ns at 5 "$ftp1 start"
$ns at 100 "$n1 setdest 550 550 15"
$ns at 190 "$n1 setdest 70 70 15"

proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab8.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run

ECE Dept., YIT Page 26


Computer Networks Lab

awk File
Begin {
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{if($1=="r" && $3=="_1_" && $4=="AGT")
{count1++
pack1=pack1+$8
time1=$2
}
if($1=="r" && $3=="_2_" && $4=="AGT")
{count2++
pack2=pack2+$8
time2=$2
}
}
END{

ECE Dept., YIT Page 27


Computer Networks Lab

ECE Dept., YIT Page 28


Computer Networks Lab

6. Implementation of Link state routing algorithm.

Code:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]

$ns namtrace-all $nf


proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}

for { set i 0 } { $i < 12} { incr i 1 } {


set n($i) [$ns node]}

for {set i 0} {$i < 8} {incr i} {


$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }

$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail


$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail

set udp0 [new Agent/UDP]


$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0

ECE Dept., YIT Page 29


Computer Networks Lab

set udp1 [new Agent/UDP]


$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0

$ns rtproto LS

$ns rtmodel-at 10.0 down $n(11) $n(5)


$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)

$udp0 set fid_ 1


$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green

$ns at 1.0 "$cbr0 start"


$ns at 2.0 "$cbr1 start"

$ns at 45 "finish"
$ns run

ECE Dept., YIT Page 30


Computer Networks Lab

Nam Topology

Data Transfer & Link Failure

ECE Dept., YIT Page 31


Computer Networks Lab

Data Transfer through Alternate Path

Second Link Fails & Data Transfer through Alternate Path

ECE Dept., YIT Page 32


Computer Networks Lab

Experiments: Part – B
Experiment no 1(a):
Aim:
To write a program for a HLDC frame to perform Character stuffing and de-stuffing and analyze
the output for various input frames
Program for Character stuffing
#include<stdio.h>
#include<string.h>
int main()
{
int i=0,j=0;
char frame[20],stuffframe[50]="\0";
printf("Enter Frame\n");
scanf("%s",frame);
/* Insert the DLESTX Flag*/
strcpy(stuffframe,"DLESTX");
j=strlen("DLESTX");
for(i=0;i<strlen(frame);i++)
{
if(frame [i]=='D' && frame [i+1]=='L' && frame [i+2]=='E')
{
stuffframe [j++]='D';
stuffframe [j++]='L';
stuffframe [j++]='E';
stuffframe [j++]= frame[i++];
stuffframe [j++]= frame [i++];
stuffframe [j++]= frame [i];
}
else
stuffframe [j++]= frame [i];
}
/* Insert the DLEETX Flag*/
strcat(stuffframe,"DLEETX");
printf("\nFrame after stuffing:\n");
printf("%s", stuffframe);
return 0;
}

Expected Output:
Ex. 1: Enter Frame
ABC
Frame after stuffing:

ECE Dept., YIT Page 33


Computer Networks Lab

DLESTXABCDLEETX
Ex. 2: Enter Frame
DLE
Frame after stuffing:
DLESTXDLEDLEDLEETX

Program for Character De-stuffing


#include<stdio.h>
#include<string.h>
int main()
{
int i=0,j=0;

char dframe[20]="\0",recframe[50]="\0";
int d;
printf("Enter Frame to be destuffed\n");
scanf("%s",recframe);
for(i=0;i<strlen(recframe);i++)
{
if
(recframe[i]=='D'&&recframe[i+1]=='L'&&recframe[i+2]=='E'&&recframe[i+3]=='E'&&recfra
me[i+4]=='T'&&recframe[i+5]=='X')
i=i+6;
if
(recframe[i]=='D'&&recframe[i+1]=='L'&&recframe[i+2]=='E'&&recframe[i+3]=='S'&&recfra
me[i+4]=='T'&&recframe[i+5]=='X')
i=i+6;
if (recframe[i]=='D'&&recframe[i+1]=='L'&&recframe[i+2]=='E')
i=i+3;
dframe[j++]=recframe[i];
}
printf("\n Frame after destuffing\n");
printf("\n %s\n",dframe);

return 0;
}

Expected Output:
Ex. 1: Enter Frame to be destuffed
DLESTXABCDLEETX
Frame after destuffing
ABC
Ex.2: Enter Frame to be destuffed
DLESTXDLEDLEDLEETX
Frame after destuffing
DLE

ECE Dept., YIT Page 34


Computer Networks Lab

Experiment no 1(b):
Aim: To write a program for a HLDC frame to perform Bit stuffing and de-stuffing and analyze
the output for various input frames

Program for Bit−Stuffing


#include <stdio.h>
int main()
{
int i=0,j=0,count=0;
char frame[100],stufframe[200]="\0";
printf("enter the bits : ");
scanf("%s",frame);

printf("\nAfter bit stuffing \n");


strcpy(stufframe,"01111110");
j=strlen(stufframe);

for(i=0;frame[i]; i++)
{
if(frame[i]=='1')
count++;
else
count=0;
stufframe[j++]=frame[i];
if(count==5)
{
stufframe[j++]='0';
count=0;
}
}
strcat(stufframe,"01111110");
printf("%s",stufframe);

return 0;
}

OUTPUT
Ex 1:
Enter the bits : 111
After bit stuffing
0111111011101111110
Ex 2:
Enter the bits: 111111
After bit stuffing
01111110111110101111110

ECE Dept., YIT Page 35


Computer Networks Lab

Program for Bit de-Stuffing


#include<stdio.h>
#include<string.h>
int main()
{
int i=0,j=0;
chardframe[100]="\0",recframe[100]="\0";
int d;
printf("Enter bits for destuffing\n");
scanf("%s",&recframe);
for(i=0;i<strlen(recframe);i++)
{
if
(recframe[i]=='0'&&recframe[i+1]=='1'&&recframe[i+2]=='1'&&recframe[i+3]=='1'&&recfram
e[i+4]=='1'&&recframe[i+5]=='1' &&recframe[i+6]== '1' &&recframe[i+7]=='0')
i=i+8;
dframe[j++]=recframe[i];
if
(recframe[i]=='1'&&recframe[i+1]=='1'&&recframe[i+2]=='1'&&recframe[i+3]=='1'&&recfram
e[i+4]=='1')

{
i++;
dframe[j++]=recframe[i++];
dframe[j++]=recframe[i++];
dframe[j++]=recframe[i++];
dframe[j++]=recframe[i++];
}

}
printf("\n Bits after destuffing\n");
printf("\n %s\n",dframe);
return 0;
}

ECE Dept., YIT Page 36


Computer Networks Lab

OUTPUT

Ex 1:
Enter bits for destuffing
0111111011101111110
Bits after destuffing
111

Ex 2:
Enter bits for destuffing
01111110111110101111110
Bits after destuffing
111111

Ex 3:
Enter bits for destuffing
01111110111110101111110
Bits after destuffing : 111111

ECE Dept., YIT Page 37


Computer Networks Lab

Experiment no 2
Aim:
To write a program for distance vector algorithm to find suitable path for transmission and
analyse the output for different number of nodes and distance matrix
Program:
#include<stdio.h>
struct node
{
int dist[20];
int from[20];
}router[10];
int main()
{
int dmat[20][20];
int no,i,j,k,count=1;
printf("\nEnter the number of nodes :\n");
scanf("%d",&no);
printf("\nEnter the distance matrix :\n");
for(i=1;i<=no;i++)
for(j=1;j<=no;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
router[i].dist[j]=dmat[i][j];
router[i].from[j]=j;
}
do
{
for(i=1;i<=no;i++)
for(j=1;j<=no;j++)
for(k=1;k<=no;k++)
if(router[i].dist[j]>dmat[i][k]+router[k].dist[j])
{
router[i].dist[j]=router[i].dist[k]+router[k].dist[j];
router[i].from[j]=k;
}
count++;
}while(count<no);
for(i=1;i<=no;i++)
{
printf("\nRouter table for router %c is \n",i+64);
for(j=1;j<=no;j++)
printf("\tNode %d Via %d, Distance : %d\n",j,router[i].from[j],router[i].dist[j]);
}

ECE Dept., YIT Page 38


Computer Networks Lab

return 0;
}

Output:
Ex.1: Enter the number of nodes : Ex.2: Enter the number of nodes :
3 4
Enter the distance matrix : Enter the distance matrix :
027 0102
201 1030
710 0301
Router table for router A is 2010
Node 1 Via 1, Distance : 0 Router table for router A is
Node 2 Via 2, Distance : 2 Node 1 Via 1, Distance : 0
Node 3 Via 2, Distance : 3 Node 2 Via 2, Distance : 1
Node 3 Via 3, Distance : 0
Router table for router B is Node 4 Via 2, Distance : 1
Node 1 Via 1, Distance : 2 Router table for router B is
Node 2 Via 2, Distance : 0 Node 1 Via 1, Distance : 1
Node 3 Via 3, Distance : 1 Node 2 Via 2, Distance : 0
Node 3 Via 1, Distance : 1
Router table for router C is Node 4 Via 4, Distance : 0
Node 1 Via 2, Distance : 3 Router table for router C is
Node 2 Via 2, Distance : 1 Node 1 Via 1, Distance : 0
Node 3 Via 3, Distance : 0 Node 2 Via 1, Distance : 1
Node 3 Via 3, Distance : 0
Node 4 Via 4, Distance : 1
Router table for router D is
Node 1 Via 2, Distance : 1
Node 2 Via 2, Distance : 0
Node 3 Via 3, Distance : 1
Node 4 Via 4, Distance : 0

ECE Dept., YIT Page 39


Computer Networks Lab

Experiment No 3
Aim: Write and implement a C program to Implement Dijkstra’s algorithm to compute the
shortest routing path.

Program
#include<stdio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n, int rootnode);

int main()
{
int cost[MAX][MAX], i, j, n, u;
printf("Enter the Number of nodes:");
scanf("%d",&n);
printf("\nEnter the cost matrix:\n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);

for(i=0;i<n;i++)
{
printf("\n\nDistance from the router %d:",i);
u=i;
dijkstra(cost,n,u);
}
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int rootnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[rootnode][i];
pred[i]=rootnode;
visited[i]=0;
}

ECE Dept., YIT Page 40


Computer Networks Lab

distance[rootnode]=0;
visited[rootnode]=1;
count=1;

while(count<n-1)
{
mindistance=INFINITY;

for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=rootnode)
{
printf("\nDistance from %d to node%d=%d",rootnode,i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=rootnode);
}
}
OUTPUT
Enter the Number of nodes: 5
Enter the cost matrix:
03060
30200
02050
60507
00070
Distance from the router 0: Distance from the router 2:

ECE Dept., YIT Page 41


Computer Networks Lab

Distance from 0 to node1=3 Distance from 2 to node0=5


Path=1<-0 Path=0<-1<-2
Distance from 0 to node2=5 Distance from 2 to node1=2
Path=2<-1<-0 Path=1<-2
Distance from 0 to node3=6 Distance from 2 to node3=5
Path=3<-0 Path=3<-2
Distance from 0 to node4=13 Distance from 2 to node4=12
Path=4<-3<-0 Path=4<-3<-2

Distance from the router 1: Distance from the router 3:


Distance from 1 to node0=3 Distance from 3 to node0=6
Path=0<-1 Path=0<-3
Distance from 1 to node2=2 Distance from 3 to node1=7
Path=2<-1 Path=1<-2<-3
Distance from 1 to node3=7 Distance from 3 to node2=5
Path=3<-2<-1 Path=2<-3
Distance from 1 to node4=14 Distance from 3 to node4=7
Path=4<-3<-2<-1 Path=4<-3

Distance from the router 4:


Distance from 4 to node0=13
Path=0<-3<-4
Distance from 4 to node1=14
Path=1<-2<-3<-4
Distance from 4 to node2=12
Path=2<-3<-4
Distance from 4 to node3=7Path=3<-4

ECE Dept., YIT Page 42


Computer Networks Lab

Experiment No 4

Aim: Write and implement a C program so that for the given data, CRC-CCITT polynomial is
used to obtain CRC code. Verify the program for the cases a. Without error b. With error

Program:
#include<stdio.h>
#include<string.h>
#define N strlen(genpoly)

char data[50],checksum[50],genpoly[]="1010";
int a, e, c;
void xor(){
for(c = 1;c < N; c++)
checksum[c] = (( checksum[c] == genpoly[c])?'0':'1');
}

void crc(){
for(e=0;e<N;e++)
checksum[e]=data[e];
do{
if(checksum[0]=='1')
xor();
for(c=0;c<N-1;c++)
checksum[c]=checksum[c+1];
checksum[c]=data[e++];
}while(e<=a+N-1);
}

int main()
{
printf("\nEnter data : ");
scanf("%s",data);
printf("\n\nGeneratng polynomial : %s",genpoly);
a=strlen(data);
for(e=a;e<a+N-1;e++)
data[e]='0';
printf("\n\nModified data is : %s",data);
crc();
printf("\n\nChecksum is : %s",checksum);
for(e=a;e<a+N-1;e++)
data[e]=checksum[e-a];
printf("\n\nFinalcodeword is : %s",data);
printf("\n\nTest error detection 0(yes) 1(no)? : ");
scanf("%d",&e);
if(e==0)

ECE Dept., YIT Page 43


Computer Networks Lab

{
do{
printf("\nEnter the position where error is to be inserted : ");
scanf("%d",&e);
}while(e==0 || e>a+N-1);
data[e-1]=(data[e-1]=='0')?'1':'0';
printf("\n\nErroneous data : %s\n",data);
}
crc();
for(e=0;(e<N-1) && (checksum[e]!='1');e++);
if(e<N-1)
printf("\nError detected\n\n");
else
printf("\nNo error detected\n\n");
return 0;
}

OUTPUT

Ex. 1:
Enter data : 1111111111111111

GeneratIngpolynomial : 10001000000100001

Modified data is : 11111111111111110000000000000000

Checksum is : 0001110100001111

Final code word is : 11111111111111110001110100001111

Test error detection 0(yes) 1(no)? : 1

No error detected

Ex. 2:
Enter data : 11111
Generating polynomial : 10001000000100001

Modified data is : 111110000000000000000

Checksum is : 1110001111011110

Final code word is : 111111110001111011110

Test error detection 0(yes) 1(no)? : 0

ECE Dept., YIT Page 44


Computer Networks Lab

Enter the position where error is to be inserted : 2

Erroneous data : 101111110001111011110

Error detected

ECE Dept., YIT Page 45


Computer Networks Lab

Experiment No 5
PROGRAM
(a)Stop and Wait Protocol
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n,r;
struct frame
{
char ack;
int data;
}frm[10];

int sender();
void recvfrm();
void resend();

int main()
{
int c;
printf("\n Selective repeat Sliding window protocol\n");
sender();
recvfrm();
resend();

printf("\nAll packets sent successfully");


}
int sender()
{
int i;
printf("\nEnter the no. of packets to be sent:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter data for packets[%d]",i);
scanf("%d",&frm[i].data);
frm[i].ack='y';
}
return 0;
}
void recvfrm()
{
int i;
rand();
r=rand()%n;

ECE Dept., YIT Page 46


Computer Networks Lab

frm[r].ack='n';
for(i=0;i<n;i++)
{
if(frm[i].ack=='n')
printf("\nThe packet number %d is not received\n",r);
}
}

void resend()
{
printf("\nresending packet %d",r);
sleep(2);
frm[r].ack='y';
printf("\nThe received packet is %d",frm[r].data);
}

Output:

ECE Dept., YIT Page 47


Computer Networks Lab

(b)Sliding Window Protocol


#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
//#include<windows.h>
int main()
{
int i, j,noframes=7,x,x1=10, x2,a, ack;
for (i=0;i<200;i++)
rand();
i=1;
j=1;
printf("\nNo of frames is %d\n", noframes);
getc;
for(noframes=0;noframes<=7;noframes++)
{
srand(x1++);
x=rand()%10;
if(x%2==0)
{
for(x2=1;x2<2;x2++)
{
printf("\nsending frame %d\n",i);
printf("waiting for 10 seconds for acknolegement");
sleep(10);
ack=receive(i);
if (ack==1)
{
printf("\n retransmitting frame %d ",i);
sleep(10);
ack=receive(i);
}
printf("\nAcknoledgement received for frame %d\n\n",i);

}
noframes-=1;
i++;
j++;

}
}getc;
printf("END of stop and wait protocol\n");
getc;
return 0;
}

ECE Dept., YIT Page 48


Computer Networks Lab

int receive(int recframe)


{
int a;
a=rand()%15;
if(a>10)
{
printf("\nThe frame is delayed or corrupted");
return 1;
}

else
{
printf("\nReceived frame no %d ", recframe);
return 0;
}
}

OUTPUT

Selective repeat Sliding window protocol

Enter the no. of packets to be sent: 5

Enter data for packets[0] 3

Enter data for packets[1] 4

Enter data for packets[2] 5

Enter data for packets[3] 6

Enter data for packets[4] 7

The packet number 1 is not received

resending packet 1
The received packet is 4
All packets sent successfully

ECE Dept., YIT Page 49


Computer Networks Lab

6. Write a program for congestion control using leaky bucket algorithm.


#include<stdio.h>
#include<stdlib.h>
#define NOF_PACKETS 10
//#include <windows.h>
int rand1(int a)
{
int rn = (rand() % 10) % a;
return rn == 0 ? 1 :rn;
}
int main()
{
int queue[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
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)
queue[i] = rand1(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, queue[i]);
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (queue[i] + p_sz_rm) >b_size)
{
if(queue[i] >b_size)
printf("\n\nIncoming packet size (%d bytes) is Greater than bucket capacity
(%d bytes)-PACKET REJECTED", queue[i], b_size);

}
else{
p_sz_rm += queue[i];
printf("\n\nIncoming Packet size: %d", queue[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rand1(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)
op = p_sz_rm, p_sz_rm = 0;
else

ECE Dept., YIT Page 50


Computer Networks Lab

op = o_rate, p_sz_rm -= o_rate;


printf("\nPacket of size %d Transmitted", op);
printf("\nBytes Remaining to Transmit: %d", p_sz_rm);
}
else{
printf("\nTime left for transmission: %d units", p_time - clk);
printf("\nNo packets to transmit!!");
}
}
}
}
}

Output:

ECE Dept., YIT Page 51

You might also like