Sie sind auf Seite 1von 56

Computer Networks Laboratory (15ECL68)

Introduction to Computer Networks


 A computer network (or)data network is a digital telecommunications network which allows
nodes to share resources. In computer networks, computing devices exchange data with each
other using connections between nodes (data links.)
Node Configuration

 NS-2is an event driven simulation tool, which is useful in studying the dynamic nature of
communication networks.
 Simulation of wired as well as wireless network functions and protocols (e.g., routing
algorithms, TCP, UDP) can be done using NS2.
Overview of NS2 Architecture

 Toolkit Command Language [TCL] is a general purpose scripting language. It is an


Interpreter, which interprets the instruction line by line.

Dept. of ECE, BITM, Ballari 1


Computer Networks Laboratory (15ECL68)

 Trace file is a text file that contains events logged during simulation (e.g., Packet drop,
packet reception etc.).
Trace file Analysis
When tracing into an output ascii file, the trace is organized into 12 fields as follows
1 2 3 4 5 6 7 8 9 10 11 12
From To Pkt Pkt Pkt
Event Time Flags Fid Srcaddr Dstaddr Seqnum
node node type size id

Ex: r 40.639943289 _1_ AGT--- 1569 tcp 1032 [a2 1 2 800] ------- [0:0 1:0 32 1] [35 0] 2 0
The meanings of the fields are:
1. The first field is the event type. It is given by one of five possible symbols r,+,−, d, h, which
correspond respectively to receive (at the output of the link), enqueued, dequeued, dropped
and hop.
2. The second field gives the time at which the event occurs.
3. The third field gives the input node of the link at which the event occurs.
4. The fourth field gives the output node of the link at which the event occurs.
5. The fifth field gives the packet type (for example, CBR, or TCP). The type corresponds to
the name that we gave to those applications.
6. The sixth field gives the packet size.
7. Some flags follow
8. This is the flow id (fid) of IPv6 that a user can set for each flow at the input OTcl script.
One can further use this field for analysis purposes; it is also used when specifying stream
color for the NAM display.
9. This is the source address given in the form of “node port".
10. This is the destination address, given in the same form.
11. This is the network layer protocol’s packet sequence number. Even though UDP
implementations in a real network do not use sequence number, NS-2 keeps track of UDP
packet sequence number for analysis purposes.
12. The last field shows the unique id of the packet.
XGRAPH
The xgraph program draws a graph on an x-display given data read from either data file or
from standard input if no files are specified. It can display upto 64 independent data sets using
different colors and line styles for each set. It annotates the graph with a title, axis labels, grid
lines or tick marks, grid labels and a legend.
Syntax: Xgraph file-name.xg
Trace file Analysis for wireless network
1 2 3 4 5 6 7 8 9 10 11 12
IP
Global MAC Src.
Node Pkt. Pkt. TCP
Event Time MAC ---- Seq. Layer ---- &
Number Type Size Info
No. Info. Dst.
addr.

Dept. of ECE, BITM, Ballari 2


Computer Networks Laboratory (15ECL68)

Ex:r 40.639943289 _1_ AGT--- 1569 tcp 1032 [a2 1 2 800] ------- [0:0 1:0 32 1] [35 0] 2 0
 The first field is a letter that can have the values r,s,f,D for “received", “sent", “forwarded"
and “dropped", respectively. It can also be M for giving a location or a movement indication,
this is described later.
 The second field is the time.
 The third field is the node number.
 The fourth field is MAC to indicate if the packet refers to a MAC layer;it is AGT to indicate
the transport layer (e.g., tcp) packet, or RTR if it refers to the routed packet. It can also be
IFQ to indicate events related to the interference priority queue (like drop of packets).
 After the dashes, the global sequence number of the packet (this is not the tcp sequence
number).
 At the next field comes more information on the packet type (e.g., tcp, ack or udp).
 Then the packet size in bytes.
 Mac layer information.
o The first hexadecimal number, a2 (which equals 162 in decimal) specifies the expected
time in seconds to send this data packet over the wireless channel. The second number,
1, stands for the MAC-id of the sending node, and the third, 2, is that of the receiving
node. The fourth number, 800, specifies that the MAC type is ETHERTYPE_IP.
o The next numbers in the second square brackets refer to the IP source and destination
addresses, then the ttl (Time To Live) of the packet.
o The third brackets concern the tcp information: its sequence number and the
acknowledgement number.

Trace File analysis for Movement of node


Ex: M 10.00000 0 (5.00, 5.00, 0.00), (250.00, 250.00), 3.00
1 2 3 4 5 6
Event Time Node no. Original location Destination location Speed

 Where the first number is the time, the second is the node number, then comes the origin and
destination locations, and finally the speed is given.

 Network Animator [NAM]is an animation tool for viewing network simulation traces.
Visualisation Using NAM

Dept. of ECE, BITM, Ballari 3


Computer Networks Laboratory (15ECL68)

1. Orientation

o $ns duplex-link-op $n0 $n1 orient right-down

o $ns duplex-link-op $n1 $n2 orient right-up

2. Coloring the Node

o $n0 color red

o $n1 color blue

3. Shape of Node

o $n1 shape box (or instead of “box" one can use “hexagon" or “circle")

4. Coloring the link

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

Keywords
 set : to assign a value to a variable
 unset : to remove the value assigned for variable
 proc : to define a procedure
 $ : used with the variable name
 # : comment
 Basic Unix commands
ls List your files
cd Change directory
mv filename1 filename2 Moves a file
cp filename1 filename2 Copies a file
chmod Change the mode
geditfilename.ext Create or edit the file
grep Find the particular data
awk Performs mathematical operations
gccfilename.ext Compile C/C++ program
./a.out Run the C Program

Dept. of ECE, BITM, Ballari 4


Computer Networks Laboratory (15ECL68)

PART-A
PART-A:

Simulation experiments using NS2/ NS3/ OPNET/ NCTUNS/


NetSim/QualNet/ Packet Tracer or any other equivalent tool

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

Dept. of ECE, BITM, Ballari 5


Computer Networks Laboratory (15ECL68)

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.

Description:
 A duplex data transmission means that data can be transmitted in both directions on a signal
carrier at the same time.
 Network performance refers to measures of service quality of a network.
 There are many different ways to measure the performance of a network, they are
o Bandwidth commonly measured in bits/second is the maximum rate that information can be
transferred
o Throughput is the actual rate that information is transferred
o Latency the delay between the sender and the receiver decoding it, this is mainly a function of the
signals travel time, and processing time at any nodes the information traverses
o Jitter variation in packet delay at the receiver of the information
o Error rate the number of corrupted bits expressed as a percentage or fraction of the total sent
 CBR stands for constant bitrate, and is an encoding method that keeps the bitrate the same.

Design:

Source code:
#Create a simulator object
set ns [new Simulator]

#Open thetrace file


set f [open lab1.tr w]
$ns trace-all $f

#Open the nam trace file


setnf [open lab1.nam w]
$ns namtrace-all $nf

Dept. of ECE, BITM, Ballari 6


Computer Networks Laboratory (15ECL68)

#Define a ‘finish’ procedure


proc finish {} {
global f nf ns
$ns flush-trace
#Close the trace file
close $f
close $nf
#Executenam on the trace file
execnam lab1.nam &
exit 0
}

#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create a duplex link between the nodes


$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7
$ns duplex-link $n1 $n2 0.3Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7
$ns duplex-link $n2 $n3 0.3Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7

# Set the queue size


$ns queue-limit $n0 $n1 20
$ns queue-limit $n1 $n2 20
$ns queue-limit $n2 $n3 20

#Create a UDP agent & attach it to node n0


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

#Create a CBRtraffic source & attach it to udp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005

#Create a Null agent (a traffic sink) & attach it to node n3


set null0 [new Agent/Null]

Dept. of ECE, BITM, Ballari 7


Computer Networks Laboratory (15ECL68)

$ns attach-agent $n3 $null0

#Connect the traffic source with traffic sink


$ns connect $udp0 $null0

#Schedule events for the CBR agent


$ns at 0.1 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

Steps for execution:


 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab1.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab1.tcl
 Here “ns” indicates network simulator. We get the topology shown in the network animator.
Now press the play button in the simulation window and the simulation will begins.

Nam output window

Dept. of ECE, BITM, Ballari 8


Computer Networks Laboratory (15ECL68)

Calculation of network performance

Total number of receivedpackets size ∗ 8


Network performance =
Single packet transmission time ∗ Number of packets sent
Units in bps (bits per second)
 For calculatingtotal number of received packets size, execute the following command
[root@localhost~]#grep ^r lab1.tr| grep “2 3”| awk ‘{s+=$6}END{print s}’
1 2 3 4 5 6 7 8 9 10 11 12
Event Time From To Pkt Pkt Flags Fid Src Dst Seq Pkt
Node Node Type Size Addr Addr Num Id

 For calculating the single packet transmission time,

Packet size
Single packet transmission time =
Bandwidth

 Write the value of network performance in observation sheet. Repeat the above step by
changing the bandwidth to [0.3Mb, 0.4Mb, 0.5Mb, 0.7Mb]
Sl.No. Bandwidth Network Performance
1. 0.3
2. 0.4
3. 0.5
4. 0.7

Dept. of ECE, BITM, Ballari 9


Computer Networks Laboratory (15ECL68)

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.

Description:
 The TCP and UDP protocols are two different protocols that handle data communications
between terminals in an IP network (the Internet).
 Transmission Control Protocol(TCP) is one of the main protocols in TCP/IP networks.
Whereas the IP protocoldeals only with packets, TCP enables two hosts to establish a
connection and exchange streams of data. TCP guarantees delivery of data and also
guarantees that packets will be delivered in the same order in which they were sent.
 User Datagram Protocol (UDP) is part of the Internet Protocol suite used by programs
running on different computers on a network. UDP is used to send short messages called
datagrams but overall, it is an unreliable, connectionless protocol.
 File Transfer Protocol (FTP) is the commonly used protocol for exchanging files over
the Internet. FTP uses the Internet's TCP/IPprotocols to enable data transfer. FTP
promotes sharing of files via remote computers with reliable and efficient data transfer

Design:

Dept. of ECE, BITM, Ballari 10


Computer Networks Laboratory (15ECL68)

Source code:
#Create a simulator object
set ns [new Simulator]

#Open thetrace file


set f [open lab2.tr w]
$ns trace-all $f

#Open the nam trace file


setnf [open lab2.nam w]
$ns namtrace-all $nf

#Define different colors for data flows


$ns color 1 "Blue"
$ns color 2 "Red"

#Define a ‘finish’ procedure


proc finish {} {
global ns f nf
$ns flush-trace
#Close the trace file
close $f
close $nf
#Executenam on the trace file
execnam lab2.nam &
exit 0
}
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create a duplex link between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 2.75Mb 20ms DropTail

# Set the queue size


$ns queue-limit $n2 $n3 50

Dept. of ECE, BITM, Ballari 11


Computer Networks Laboratory (15ECL68)

#Create a TCP agent & attach it to node n0


set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$tcp0 set class_ 1

#Create a FTP Application & attach it to tcp0


set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0

#Create a TCP sinkagent & attach it to node n3


set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
#Connect the traffic source with traffic sink
$ns connect $tcp0 $sink

#Create a UDP agent & attach it to node n1


set udp0 [new Agent/UDP]
$ns attach-agent $n1 $udp0
$udp0 set class_ 2

#Create a CBR traffic source & attach it to udp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set interval_ 0.005

#Create a Null agent (a traffic sink) & attach it to node n3


set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
#Connect the traffic source with traffic sink
$ns connect $udp0 $null0

#Scheduling the events


$ns at 0.1 "$cbr0 start"
$ns at 1.0 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"
#Run the simulation
$ns run

Dept. of ECE, BITM, Ballari 12


Computer Networks Laboratory (15ECL68)

Steps for execution:


 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab2.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab2.tcl

Nam output window

Execute the following command to check


Packets Sent/ Received – TCP/UDP/ ACK/ TOTAL

To calculate the number of packets sent by TCP.


[root@localhost~]#grep ^r lab2.tr | grep “tcp” -c

To calculate the number of packets sent by UDP.


[root@localhost~]#grep ^r lab2.tr | grep “cbr” –c

To calculate the number of TCP-ACK received.


[root@localhost~]#grep ^r lab2.tr | grep “ack” –c

To calculate the number of packets received/sent.


[root@localhost~]#grep ^r lab2.tr –c

Dept. of ECE, BITM, Ballari 13


Computer Networks Laboratory (15ECL68)

3. Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by


changing the error rate and data rate.

Description:
 Ethernet is the name of the most commonly used LAN today.
 A LAN (Local Area Network) is a network of computers that covers a small area like a
room, an office, a building or a campus.
 Ethernet is a network protocol that controls how data is transmitted over a LAN.
Technically it is referred to as the IEEE 802.3 protocol.
 The protocol has evolved and improved over time and can now deliver at the speed of a
gigabit per second.

Design:

Source code:
#Create a simulator object
set ns [new Simulator]

#Open thetrace file


settrf [open lab3.tr w]

$ns trace-all $trf

#Open the nam trace file


setnaf [open lab3.nam w]
$ns namtrace-all $naf

Dept. of ECE, BITM, Ballari 14


Computer Networks Laboratory (15ECL68)

#Define a ‘finish’ procedure


proc finish { } {
globalnf ns tf
#Executenam on the trace file
execnam lab3.nam &
#Close the trace file
close $naf
close $trf
exit 0
}
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

#Label the nodes


$n1 label "Source"

$n2 label "Error Node"


$n5 label "Destination"

#Create a LAN
$ns make-lan "$n0 $n1 $n3 $n2" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n5 $n6 $n4" 10Mb 10ms LL Queue/DropTail Mac/802_3

#Create a duplex link between the nodes


$ns duplex-link $n2 $n6 30Mb 100ms DropTail
#Give node position (or) orientation of node
$ns duplex-link-op $n2 $n6orient right

#Create a UDP agent & attach it to node n1


set udp0 [new Agent/UDP]
$ns attach-agent $n1 $udp0

#Create a CBR traffic source & attach it to udp0


set cbr0 [ new Application/Traffic/CBR]
$cbr0 attach-agent $udp0

Dept. of ECE, BITM, Ballari 15


Computer Networks Laboratory (15ECL68)

$cbr0 set packetSize_ 100


$cbr0 set interval_ 0.001
$udp0 set class_ 1

#Create a Null agent (a traffic sink) & attach it to node n5


set null5 [new Agent/Null]
$ns attach-agent $n5 $null5
#Connect the traffic source with traffic sink
$ns connect $udp0 $null5

#Create a Error model


set err [new ErrorModel]
$ns lossmodel $err $n2 $n6
$err set rate_ 0.8 #vary error rate 0, 0.4, 0.8 and 1.0

#Scheduling the events


$ns at 0.1 "$cbr0 start"
$ns at 4.0 "$cbr0 stop"

#Call the finish procedure after 6 seconds of simulation time


$ns at 6.0 "finish"
#Run the simulation
$ns run
Steps for execution:
 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab3.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab3.tcl

Nam output window

Dept. of ECE, BITM, Ballari 16


Computer Networks Laboratory (15ECL68)

Calculation of Throughput

Total number of received packets size ∗ 8


Throughput =
Single packet transmission time ∗ Number of packets sent
Units in bps (bits per second)
 For calculating total number of received packets size, execute the following command
[root@localhost~]#grep ^r lab3.tr| grep “2 6”| awk ‘{s+=$6}END{print s}’
1 2 3 4 5 6 7 8 9 10 11 12
Event Time From To Pkt Pkt Flags Fid Src Dst Seq Pkt
Node Node Type Size Addr Addr Num Id

 For calculating the single packet transmission time,

Packet size
Single packet transmission time =
Bandwidth

 Write the value of throughput in observation sheet. Repeat the above step by changing the
error rate to the following line of the program
$err set rate_ 0.7 #vary error rate 0.1, 0.4, 0.5 and 0.7
Sl. Error rate Throughput
No.
1. 0.1
2. 0.4
3. 0.5
4. 0.7

Dept. of ECE, BITM, Ballari 17


Computer Networks Laboratory (15ECL68)

4. Implement Ethernet LAN using n nodes and assign multiple traffic to the
nodes and obtain congestion window for different sources/ destinations.
Description:
 Congestion means Blockage of traffic (or) Heavy traffic (or) Crowded traffic.
 During “Multiple Traffic Transmission”, the congestion of traffic is occurred. Hence the
throughput is decreased substantially.
 Here Congestion window determines the number of bytes outstanding at a time. Its size
varies according to the network state.
 TCP follows the following steps to avoid the Congestion:
o Initially the window size increases rapidly
o When it reaches a large value, it grows slowly.
o When congestion is detected, the window size decreases drastically.
o This mechanism allows resolving congestion rapidly and yet using efficiently the
network’s bandwidth.
Design:

Source code:
#Create a simulator object
set ns [new Simulator]

#Open thetrace file


set f [open lab4.tr w]

$ns trace-all $f

#Open the nam trace file


setnf [open lab4.nam w]

Dept. of ECE, BITM, Ballari 18


Computer Networks Laboratory (15ECL68)

$ns namtrace-all $nf

#Define a ‘finish’ procedure


proc finish {} {
global ns f nf outFile1 outFile2
$ns flush-trace
#Close the trace file
close $f
close $nf
#Executenam on the trace file
execnam lab4.nam &
#Execute xgraph

exec xgraph Congestion1.xg Congestion2.xg -geometry 400x400 &


exit 0

#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Label the nodes


$n0 label "Src1"
$n4 label "Dst1"
$n1 label "Src2"
$n5 label "Dst2"

#Create a LAN
$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 " 10Mb 30ms LL Queue/DropTail Mac/802_3#vary
bandwidth 1, 10

#Create a TCP agent & attach it to node n0


set tcp1 [new Agent/TCP]
$ns attach-agent $n0 $tcp1
#Create a FTP Application & attach it to tcp0
set ftp1 [new Application/FTP]

Dept. of ECE, BITM, Ballari 19


Computer Networks Laboratory (15ECL68)

$ftp1 attach-agent $tcp1


#Create a TCP sinkagent & attach it to node n4
set sink1 [new Agent/TCPSink]
$ns attach-agent $n4 $sink1
$ftp1 set maxPkts_ 1000
#Connect the traffic source with traffic sink
$ns connect $tcp1 $sink1

#Create a TCP agent & attach it to node n1


set tcp2 [new Agent/TCP/Reno]

$ns attach-agent $n1 $tcp2


#Create a FTP Application & attach it to tcp1
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
#Create a TCP sinkagent & attach it to node n4
set sink2 [new Agent/TCPSink]
$ns attach-agent $n5 $sink2
$ftp2 set maxPkts_ 1000
#Connect the traffic source with traffic sink
$ns connect $tcp2 $sink2

#Open the congestion file


set outFile1 [open Congestion1.xg w]
set outFile2 [open Congestion2.xg w]
procfindWindowSize {tcpSourceoutFile} {
global ns
set now [$ns now]
setcWindSize [$tcpSource set cwnd_]

puts $outFile "$now $cWindSize"


$ns at [expr $now + 0.1] "findWindowSize $tcpSource $outFile"
}

#Scheduling the events


$ns at 0.0 "findWindowSize $tcp1 $outFile1"
$ns at 0.1 "findWindowSize $tcp2 $outFile2"
$ns at 0.3 "$ftp1 start"
$ns at 0.5 "$ftp2 start"
$ns at 50.0 "$ftp1 stop"
$ns at 50.0 "$ftp2 stop"
#Call the finish procedure after 50 seconds of simulation time

Dept. of ECE, BITM, Ballari 20


Computer Networks Laboratory (15ECL68)

$ns at 50.0 "finish"


#Run the simulation
$ns run

Steps for execution:


 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab4.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab4.tcl

Nam output window

Xgraph output window

No Congestion(For 10Mb Bandwidth)

Dept. of ECE, BITM, Ballari 21


Computer Networks Laboratory (15ECL68)

Congestion(For 1Mb Bandwidth)

Dept. of ECE, BITM, Ballari 22


Computer Networks Laboratory (15ECL68)

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

Description:
 Wireless LAN:It is a type of network where the data is not transmitted via cables butover the
air through the use of wireless transmitters and receivers. Most modern WLANs are based on
IEEE 802.11 standards.
 Important Parameters related to wireless network:
o Stations [STA]: All components that can connect into a wireless medium in a network arereferred to
as stations.
o Basic service set [BSS]: The basic service set (BSS) is a set of all stations that cancommunicate with
each other at PHY layer.
o Extended service set [ESS]: An extended service set (ESS) is a set of connectedBSSs.
 List of basic parameters which need to be assigned (configured) for wireless node
o Channel type- Wireless Channel
o Radio Propagation type- TwoRayGround
o Routing Protocol- DSDV
o Network Interface type- WirelessPhy
o
(Physical Layer Type)
o Link layer type- LL Mac Type- 802_11
o Antenna Type- Omni Antenna
o Interface Queue Type- Queue/DropTail/PriQueue
o Queue Length- 50
o Number of mobile nodes- 2
o agentTrace ON
o routerTrace ON
 In wired nodes, connectivity is determined by wires (defined by duplex-link, simplex-
link). But, the connectivity of wireless nodes is determined by their distance from one
another.
 Because wireless nodes can MOVE, their distances from one another change over time
 To keep track of their positions in the topology grid, we need to create a General
Operations Director (GOD) object.
 The God object stores:

o the total number of mobile nodes

o a table of shortest number of hops required to reach from one node to another.

Design:

Dept. of ECE, BITM, Ballari 23


Computer Networks Laboratory (15ECL68)

Source code:
#Create a simulator object
set ns [new Simulator]

#Open thetrace file


settf [open lab5.tr w]
$ns trace-all $tf

#Create a flatgrid topography


settopo [new Topography]
$topoload_flatgrid 1300 1300

#Open the nam trace file


setnf [open lab5.nam w]
$ns namtrace-all-wireless $nf 1300 1300

#Node Configuration
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue\
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \

-ifqLen 50 \
-phyTypePhy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON
#Create God object
create-god 3

Dept. of ECE, BITM, Ballari 24


Computer Networks Laboratory (15ECL68)

#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

#Label the nodes


$n0 label "ESS"
$n1 label "mob1"
$n2 label "mob2"

#Setting the initial position of the nodes


$n0 set X_ 10

$n0 set Y_ 600


$n0 set Z_ 0
$n1 set X_ 80
$n1 set Y_ 600
$n1 set Z_ 0
$n2 set X_ 1200
$n2 set Y_ 600
$n2 set Z_ 0

#Giving mobility to the nodes


$ns at 0.1 "$n0 setdest 10 600 15"
$ns at 0.1 "$n1 setdest 80 600 25"
$ns at 0.1 "$n2 setdest 1200 600 25"

#Create a TCP agent & attach it to node n0


set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0


#Create a FTP Application & attach it to tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#Create a TCP sinkagent & attach it to node n1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
#Connect the traffic source with traffic sink
$ns connect $tcp0 $sink1

#Create a TCP agent & attach it to node n0

Dept. of ECE, BITM, Ballari 25


Computer Networks Laboratory (15ECL68)

set tcp1 [new Agent/TCP]


$ns attach-agent $n0 $tcp1
#Create a FTP Application & attach it to tcp0
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
#Create a TCP sinkagent & attach it to node n2
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
#Connect the traffic source with traffic sink
$ns connect $tcp1 $sink2

#Scheduling the events


$ns at 2 "$ftp0 start"
$ns at 15 "$ftp1 start"
$ns at 3 "$n1 setdest 1000 600 250"
$ns at 3 "$n2 setdest 80 600 250"

#Define a ‘finish’ procedure


proc finish { } {
global ns nftf
$ns flush-trace
#Executenam on the trace file
execnam lab5.nam &
#Close the trace file
close $tf
exit 0
}
#Call the finish procedure after 20 seconds of simulation time
$ns at 20 "finish"
#Run the simulation

$ns run
Steps for execution:
 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab5.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab5.tcl

Dept. of ECE, BITM, Ballari 26


Computer Networks Laboratory (15ECL68)

Nam output window

 To calculate the throughput. Execute the following command.


 For calculating number of received packets

[root@localhost~]#grep ^r lab5.tr | grep “AGT” | grep “tcp” | awk ‘{s+=$8}END{print s}’


 For calculating total time

[root@localhost~]#grep ^r lab5.tr | grep “AGT” | grep “tcp” | awk ‘{s+=$2}END{print s}’


1 2 3 4 5 6 7 8 9 10 11 12
IP
Global MAC Src.
Node Pkt. Pkt. TCP
Event Time MAC ---- Seq. Layer ---- &
Number Type Size Info
No. Info. Dst.
addr.

Throughput = (Total Packet received/ Total Time) (bps)

Dept. of ECE, BITM, Ballari 27


Computer Networks Laboratory (15ECL68)

6. Implementation of Link state routing algorithm

Description:
 A router is a networking device that forwards data packets between computer networks.
 When a data packet comes in on one of the lines, the router reads the network address
information in the packet to determine the ultimate destination.
 Routing is the process of selecting paths in a network along which to send network traffic.
 Goals of routing are correctness, simplicity, robustness, stability, fairness & optimality.
 Routing is performed for many kinds of network, including the telephone network,
electronic data network & transportation networks.
 Routing algorithms can be classified based on the following:
o Static or Dynamic routing
o Distributed or Centralized
o Single path or Multi-path
o Flat or Hierarchical
o Intra Domain or Inter Domain
o Link State or Distance vector

 The basic concept of link-state routing is that every node constructs a map of the connectivity
to the network, in the form of a graph, showing which nodes are connected to which other
nodes.
 Each node then independently calculates the next best logical path from it to every possible
destination in the network. Each collection of best paths will then form each node's routing
table.

Design:

Dept. of ECE, BITM, Ballari 28


Computer Networks Laboratory (15ECL68)

Source code:
#Create a simulator object
set ns [new Simulator]

#Tell the simulator to use link state routing


$ns rtproto LS

#Open thetrace file


setnf [open lab6.nam w]
$ns namtrace-all $nf

#Define a ‘finish’ procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Executenam on the trace file
execnam lab6.nam &
exit 0
}
#Create 7 nodes
for {set i 0} {$i < 7} {incr i} {

set n($i) [$ns node]


}
#Create a duplex link between the nodes
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

#Create a UDP agent & attach it to node n0


set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0

#Create a CBR traffic source & attach it to udp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

Dept. of ECE, BITM, Ballari 29


Computer Networks Laboratory (15ECL68)

#Create a Null agent (a traffic sink) & attach it to node n3


Set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0

#Connect the traffic source with traffic sink


$ns connect $udp0 $null0

#Schedule events for the CBR agent


$ns at 0.5 "$cbr0 start"
#Consider a case if Link between node1 & node2 fails
$ns rtmodel-at 1.0 down $n(1) $n(2)
# Failure Link between node1 & node2 is recovered
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation


$ns run
Steps for execution:
 Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab6.tcl
 Save the program and quit.
 Run the simulation program
[root@localhost~]# ns lab6.tcl
Nam output window

Dept. of ECE, BITM, Ballari 30


Computer Networks Laboratory (15ECL68)

Dept. of ECE, BITM, Ballari 31


Computer Networks Laboratory (15ECL68)

PART-B
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
i) Without error
ii) With error
5. Implementation of Stop and Wait Protocol and Sliding Window
Protocol
6. Write a program for congestion control using leaky bucket algorithm.

Dept. of ECE, BITM, Ballari 32


Computer Networks Laboratory (15ECL68)

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


i) Bit stuffing

Description:
 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.
 Whenever the sender’s data link layer encounters five consecutive one’s in the data, it
automatically stuffs a 0 bit into the outgoing bit stream.
 This bit stuffing is analogous to character stuffing, in which a DLE is stuffed into the
outgoing character stream before DLE in the data

Source code:

#include<stdio.h>

#include<string.h>

#define MAX 1000

main()
{

int si=0, di=0, one_count =0,i,msg_size, src_len, temp_len, j;

char flag_byte[] = "01111110";


char src[MAX], dest[MAX], original[MAX], temp[MAX];

printf("Enter frame data in binary: \n");


gets(src);
msg_size = strlen(src);
printf("\n Message size (in bits) is %d \n", msg_size);
strcpy(dest,flag_byte);
di = strlen(flag_byte);
printf("\n Length of bit string (or di is pointig to) is %d \n", di);
src_len = strlen(src);
printf("\n Length of the message (in bits) is %d \n", src_len);

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


{

Dept. of ECE, BITM, Ballari 33


Computer Networks Laboratory (15ECL68)

if(src[si] == '1')
one_count++;
else
one_count = 0;

dest[di++] = src[si++];

if(one_count ==5)
{
dest[di++] = '0';
one_count = 0;

dest[di] = '\0'; // store a NUL character in the last location pointed by di


strcat(dest, flag_byte);
printf("\n Bit Stuffed frame is: \n %s \n\n", dest);

//================================
// Destuffing
//=================================

j=0;
int destLength = strlen(dest);//-16;

printf("\n Length of dest string is %d \n", destLength);

one_count=0

for(i=8; i < strlen(dest)-8;i++)


{

Dept. of ECE, BITM, Ballari 34


Computer Networks Laboratory (15ECL68)

if(dest[i] == '0')
{
one_count=0;

temp[j++] = dest[i];

else
{
if(dest[i] == '1' && one_count!=5)
{

one_count++;
temp[j++] = dest[i];

}
if(one_count==5)
{
i++;
one_count=0;
printf("\n 3 Value of i and one_count are %d and %d \n", i, one_count);

}
}

} // for ends

// Stuff null
temp[j] = '\0';

temp_len = strlen(temp);
printf("\n Destuffed frame is: \n %s \n\n", temp);
printf("\n Size of Destuffed frame is %d \n", temp_len);

}// main ends here...

Dept. of ECE, BITM, Ballari 35


Computer Networks Laboratory (15ECL68)

Execution:

[root@localhost code]# gcc 1a.c


[root@localhost code]# ./a.out

Enter frame data in binary:


1011111101

Message size (in bits) is 10

Bit Stuffed frame is:


011111101011111010101111110

Length of dest string is 27

Destuffed frame is:


1011111101

Size of Destuffed frame is 10

Dept. of ECE, BITM, Ballari 36


Computer Networks Laboratory (15ECL68)

ii) Character stuffing.

Description:

 The framing method gets around the problem of resynchronization after an error by having
eachframe start with the ASCII character sequence DLE STX and the sequence DLE ETX.
 If the destinationever losses the track of the frame boundaries all it has to do is look for DLE
STX or DLE ETXcharacters to figure out.
 The data link layer on the receiving end removes the DLE before the data aregiven to the
network layer.
 This technique is called character stuffing

Source code:

#include<stdio.h>

#include<string.h>

#define MAX 1000

main()
{
int si=0, di=0, i, ptr=0;

char begin[] = "DLE STX", end[] = "DLE ETX";


char src[MAX], dest[MAX], de[MAX], destuffed_frame[MAX];

printf("Enter frame data in ASCII: \n");


gets(src);

strcpy(dest,begin);

di = strlen(begin);

while(src[si] != '\0')
{

if(src[si] == 'D' && src[si+1] == 'L' && src[si+2] == 'E')


{
dest[di+0] = 'D', dest[di+1] = 'L', dest[di+2] = 'E';
dest[di+3] = 'D', dest[di+4] = 'L', dest[di+5] = 'E';
di += 6;
si += 3;

Dept. of ECE, BITM, Ballari 37


Computer Networks Laboratory (15ECL68)

}
else
dest[di++] = src[si++];

dest[di] = '\0';
strcat(dest,end);
printf("\n Character stuffed frame is: %s \n\n", dest);

strcpy(de, dest);
printf("\n de's contents are %s \n", de);

for(i=7;i< strlen(dest)-7;i++)
{
if(dest[i+0] == 'D' && dest[i+1] == 'L' && dest[i+2] == 'E')
{

de[i+0] = ' ', de[i+1] = ' ', de[i+2] = ' ';


i +=3;
}

destuffed_frame[ptr++] = de[i];
//printf("\n Destuffed frame (original frame) is: %c \n", de[i]);

printf("\n Character destuffed frame (i.e., the original frame) is:");


puts(destuffed_frame);

printf("\n\n");

EXECUTION :

[root@localhost code]# gcc 1b.c


[root@localhost code]# ./a.out

Dept. of ECE, BITM, Ballari 38


Computer Networks Laboratory (15ECL68)

Enter frame data in ASCII:


hello

Character stuffed frame is: DLE STXhelloDLE ETX

de's contents are DLE STXhelloDLE ETX

Character destuffed frame (i.e., the original frame) is:hello

Dept. of ECE, BITM, Ballari 39


Computer Networks Laboratory (15ECL68)

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

Description:

 Distance vector routing algorithms operate by having each router maintain a table (i.e.,
vector) giving the best known distance to each destination and which line to get there.
 These tables are updated by exchanging information with the neighbors.

Source code:

#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];

int main()
{

int dmat[20][20];
int n, i, j, k, count=0;

printf("\n Enter the number of nodes : ");


scanf("%d", &n);
printf("\n Enter the cost matrix (999 for no link):\n");
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])
{

Dept. of ECE, BITM, Ballari 40


Computer Networks Laboratory (15ECL68)

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\n State value for router %d is \n",i+1);


for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d", j+1, rt[i].from[j]+1, rt[i].dist[j]);
}
}
printf("\n\n");
}

Execution:
[root@localhost code]# gcc 2.c
[root@localhost code]# ./a.out
Enter the number of nodes : 4

Enter the cost matrix (999 for no link):

0 2 999 1
2052
999 5 0 6
1260

State value for router 1 is

node 1 via 1 Distance0

node 2 via 2 Distance2


node 3 via 2 Distance7
node 4 via 4 Distance1

State value for router 2 is

node 1 via 1 Distance2


node 2 via 2 Distance0

Dept. of ECE, BITM, Ballari 41


Computer Networks Laboratory (15ECL68)

node 3 via 3 Distance5


node 4 via 4 Distance2

State value for router 3 is

node 1 via 2 Distance7


node 2 via 2 Distance5
node 3 via 3 Distance0
node 4 via 4 Distance6

State value for router 4 is

node 1 via 1 Distance1


node 2 via 2 Distance2
node 3 via 3 Distance6
node 4 via 4 Distance0

Dept. of ECE, BITM, Ballari 42


Computer Networks Laboratory (15ECL68)

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

Description:
 Dijkstra algorithm is also called single source shortest path algorithm.
 The algorithm maintains a list visited [ ] of vertices, whose shortest distance from the source
is calculated.

Source code:
#include<stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
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[startnode][i];
pred[i]=startnode;

Dept. of ECE, BITM, Ballari 43


Computer Networks Laboratory (15ECL68)

visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=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!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}

Execution:

[root@localhost code]# gcc 3.c

Dept. of ECE, BITM, Ballari 44


Computer Networks Laboratory (15ECL68)

[root@localhost code]# ./a.out


Enter no. of vertices:5

Enter the adjacency matrix:

0 10 0 30 100
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
100 0 10 60 0

Enter the starting node:0

Distance of node1=10
Path=1<-0
Distance of node2=50
Path=2<-3<-0
Distance of node3=30
Path=3<-0

Distance of node4=60
Path=4<-2<-3<-0

Dept. of ECE, BITM, Ballari 45


Computer Networks Laboratory (15ECL68)

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
Description:
 CyclicRedundancy Check is error detection method.
 It is used to find that the transmitted data contains error (or) not.
 This is done by polynomial generation method.

Source code:

#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[28],cs[28],g[]="1101";
int data_length,modified_length,i,e_d;
void xor()
{
for(i=1;i<N;i++)
cs[i]=((cs[i]==g[i])?'0':'1');
}

void crc()
{
for(modified_length=0;modified_length<N;modified_length++);
cs[modified_length]=t[modified_length];
do
{
if(cs[0]=='1')
xor();
for(i=0;i<N-1;i++)
cs[i]=cs[i+1];
cs[i]=t[modified_length++];
}
while(modified_length<=data_length+N-1);
}

int main()
{
printf("\n Enter data: ");
scanf("%s",t);
printf("\n-----------------------------------------");
printf("\n generating polynomial:%s",g);
data_length=strlen(t);
for(modified_length=data_length;modified_length<data_length+N-1;modified_length++)
t[modified_length]='0';
printf("\n-----------------------------------------");

Dept. of ECE, BITM, Ballari 46


Computer Networks Laboratory (15ECL68)

printf("\n modified data is: %s",t);


printf("\n------------------------------------------");
crc();
printf("\n checksum is : %s",cs);
for(modified_length=data_length;modified_length<data_length+N-1;modified_length++)

t[modified_length]=cs[modified_length-data_length];
printf("\n-----------------------------------------");
printf("final codeword is:%s",t);
printf("\n------------------------------------------");
printf("\n Test error detection 0(yes) 1(no)?: ");
scanf("%d",&e_d);
if(e_d==0)
{
do
{
printf("\n enter the position where error is to be inserted: ");
scanf("%d",&e_d);
}
while
(e_d==0 || e_d>data_length+N-1);
t[e_d-1]=(t[e_d-1]=='0')?'1':'0';
printf("\n-----------------------------------------");
printf("\n Erroneous data:%s \n",t);
}
crc();
for(e_d=0;(e_d<N-1)&&(cs[e_d]!='1');e_d++);
if(e_d<N-1)
printf("\n error detected \n\n");
else
printf("/ No error is deteced \n\n");

printf("\n-------------------------------\n");
return 0;
}

Execution:

[root@localhost code]# gcc 4.c


[root@localhost code]# ./a.out

Dept. of ECE, BITM, Ballari 47


Computer Networks Laboratory (15ECL68)

Enter poly : 1011101

Generating Polynomial is : 10001000000100001

Modified t[u] is : 10111010000000000000000

Checksum is : 1000101101011000

Final Codewordis : 10111011000101101011000

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

Enter position where you want to insert error : 3

Errorneous data : 10101011000101101011000


Error detected.

Enter poly : 1011101

Generating Polynomial is : 10001000000100001

Modified t[u] is : 10111010000000000000000

Checksum is : 1000101101011000

Final Codewordis : 10111011000101101011000

Test Error detection 0(yes) 1(no) ? : 1 0No Error Detected.

Dept. of ECE, BITM, Ballari 48


Computer Networks Laboratory (15ECL68)

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

5a. Stop and Wait protocol

Source code:

# include <stdio.h>
# include <stdlib.h>

void main()
{

int i,j,noframes,x,x1=10,x2;
//for(i=0;i< 5 ;i++)
// rand();

//noframes= 80; //rand()/100000;

i=1;
j=1;
noframes= 8;

printf("\n no of frames is %d\n\n",noframes);


printf("\n ---------------------------\n");
while(noframes>0)
{
printf("\n sending frame %d ",i);
srand(x1++);

x=rand()%10;
printf("\n value of x with srand %d ",x);
if(x%2==0)
{
for(x2=1;x2<2;x2++)
{
printf("\n waiting for %d seconds",x2);
printf("\n Either frame or ACK is lost ");
sleep(x2);
}
printf("\n So, resending frame %d",i);
srand(x1++);
x=rand()%10;
}

printf("\nReceived ACK for frame %d\n",j);


noframes-=1;
i++;

Dept. of ECE, BITM, Ballari 49


Computer Networks Laboratory (15ECL68)

j++;
printf("\n ---------------------------\n");
}// While ends
printf("\n end of stop and wait protocol\n\n");

}// Main ends

Execution:

[root@localhost code]# gcc 5b.c


[root@localhost code]# ./a.out

no of frames is 8
---------------------------

sending frame 1
value of x with srand 5
Received ACK for frame 1

---------------------------
sending frame 2
value of x with srand 3
Received ACK for frame 2

---------------------------
sending frame 3
value of x with srand 0
waiting for 1 seconds
Either frame or ACK is lost
So, resending frame 3
Received ACK for frame 3

---------------------------

sending frame 4
value of x with srand 3
Received ACK for frame 4

---------------------------
sending frame 5
value of x with srand 3
Received ACK for frame 5

---------------------------

sending frame 6
value of x with srand 4
waiting for 1 seconds

Dept. of ECE, BITM, Ballari 50


Computer Networks Laboratory (15ECL68)

Either frame or ACK is lost


So, resending frame 6
Received ACK for frame 6

---------------------------
sending frame 7
value of x with srand 8
waiting for 1 seconds
Either frame or ACK is lost
So, resending frame 7
Received ACK for frame 7

---------------------------

sending frame 8
value of x with srand 1
Received ACK for frame 8

---------------------------

end of stop and wait protocol

Dept. of ECE, BITM, Ballari 51


Computer Networks Laboratory (15ECL68)

5b. Sliding window protocol program

Source code:

#include<stdio.h>
int main()
{
int w, i, f, frames[50];
printf("Enter window size: ");
scanf("%d", &w);

printf("\n Enter number of frames to transmit:");


scanf("%d", &f);
printf("\n Enter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d", &frames[i]);

printf("\n With sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgementsent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;

Execution:

[root@localhost code]# gcc 5b.c

Dept. of ECE, BITM, Ballari 52


Computer Networks Laboratory (15ECL68)

[root@localhost code]# ./a.out


Enter window size: 3

Enter number of frames to transmit: 5

Enter 5 frames: 12 20 87 65 4

With sliding window protocol the frames will be sent in the following manner (assuming
no corruption of frames)

After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver

12 20 87
Acknowledgement of above frames sent is received by sender

65 4
Acknowledgement of above frames sent is received by sender

Dept. of ECE, BITM, Ballari 53


Computer Networks Laboratory (15ECL68)

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

Description:
 In Leaky bucket, each host is connected to the network by an interface containing a leaky
bucket that is a finite internal queue.
 Ifa packet arrives at the queue when it is full, the packet is discarded.

Source code:

#include<stdio.h>

#include<stdlib.h>
#include<unistd.h>

intbucket_size;

voidbucket_input ( intpkt_sz, intop_rt )

if(pkt_sz>bucket_size )

printf(" \n\nBucket overflow\n ");

else

sleep(1);

while ( pkt_sz>op_rt )

printf(" \n %d bytes outputted ", op_rt );

pkt_sz-= op_rt;

sleep(1);

Dept. of ECE, BITM, Ballari 54


Computer Networks Laboratory (15ECL68)

if ( pkt_sz> 0 )

printf(" \nLast %d bytes sent\n", pkt_sz );


printf(" \n Bucket output successful \n" ); }

int main()

int i, op_rate, packet_size;

printf("\n Enter Bucket Size: " );

scanf( "%d", &bucket_size );

printf(" \n Enter output rate: " );

scanf( "%d", &op_rate );

for( i=1; i<=5; i++ )

sleep(1);

packet_size = random()%1000;

printf(" \n Packet number [%d] \t Packet size = %d ", i, packet_size );


bucket_input( packet_size, op_rate ); }

return 0;

Execution:

Dept. of ECE, BITM, Ballari 55


Computer Networks Laboratory (15ECL68)

[root@localhost code]# gcc 6.c

[root@localhost code]# ./a.out

Enter Bucket Size: 500

Enter output rate: 80

Packet number [1] Packet size = 383


80 bytes outputted
80 bytes outputted
80 bytes outputted
80 bytes outputted
Last 63 bytes sent

Bucket output successful

Packet number [2] Packet size = 886

Bucket overflow

Packet number [3] Packet size = 777

Bucket overflow

Packet number [4] Packet size = 915

Bucket overflow

Packet number [5] Packet size = 793

Bucket overflow

Dept. of ECE, BITM, Ballari 56

Das könnte Ihnen auch gefallen