Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

JSS MAHAVIDYAPEETHA

JSS ACADEMY OF TECHNICAL EDUCATION, Bengaluru


Affiliated to Visvesvaraya Technological University, Belagavi, Karnataka,
Approved by AICTE, New Delhi, Government of Karnataka

COMPUTER NETWORKS NOTES (21CS52)


V SEMESTER
DEPARTMENT OF CSE (AI & ML)
LAB MANUAL
Lab Program 1:
Implement three nodes point – to – point networks with duplex links between them. Set the
queue size, vary the bandwidth and find the number of packets dropped.

Topology

#Create Simulator object

set ns [new Simulator]

#Open trace file


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

#Open namtrace file


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

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

#Assign color to the packet


$ns color 1 Blue
$ns color 2 Red

#label nodes
$n0 label "Source/udp0"
$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/null"

#create links, specify the type, nodes, bandwidth, delay and ARQ algorithm for it

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


$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 100Kb 300ms DropTail

#set queue size between the nodes


$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10
$ns queue-limit $n2 $n3 5

#create and attach UDP agent to n0, n1 and Null agent to n3


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3

#attach Application cbr to udp


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

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

#connect the agents


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

#set packet size and interval for cbr1


$cbr1 set packetSize_ 500Mb
$cbr1 set interval_ 0.005

#finish procedure
proc finish { } {
global ns nf nt
$ns flush-trace
exec nam lab1.nam &
close $nt
close $nf
exit 0
}
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
$ns run

AWK file

BEGIN { count=0;
}
{
if ($1=="d")
count++
}
END{
printf("Number of packets dropped is = %d\n", count);
}

Output:
[root@localhost 21cs52]# awk -f lab1.awk lab1.tr
Number of packets dropped is = 4065

Explanation

$ns queue-limit $n0 $n2 10

The command sets a queue limit for a specific link or channel between nodes in the
simulation.

2 $ns: This refers to the network simulator object in ns-2. It's a common prefix for many
commands in ns-2 scripts.
3 queue-limit: This is a command or function used to set the queue limit for a specific link
or channel.
4 $n0 and $n2: These are node identifiers. In ns-2, nodes are typically represented by
variables like $n0, $n1, $n2, etc.
5 10: This is the queue limit value you are setting for the link or channel between nodes
$n0 and $n2. It means that the queue associated with this link will not allow more than 10
packets to be enqueued.

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1

set null3 [new Agent/Null]


$ns attach-agent $n3 $null3

1. We are creating UDP agents and attaching them to nodes in a network simulation using
ns-2.

2. set udp0 [new Agent/UDP]: This line creates a new UDP agent and assigns it to the
variable udp0.

3. $ns attach-agent $n0 $udp0: This line attaches the UDP agent udp0 to the node
represented by the variable $n0. In network simulations, agents are entities responsible
for generating and processing packets.

4. set udp1 [new Agent/UDP]: Similarly, this line creates another UDP agent and assigns
it to the variable udp1.

5. $ns attach-agent $n1 $udp1: This line attaches the UDP agent udp1 to the node
represented by the variable $n1.

6. set null3 [new Agent/Null]: This line creates a new Null agent, which is often used as a
sink or endpoint that discards any packets it receives.

7. $ns attach-agent $n3 $null3: This line attaches the Null agent null3 to the node
represented by the variable $n3. This configuration suggests that node $n3 will act as a
destination that discards incoming packets, possibly serving as a simulation endpoint or
sink.

In summary, the code sets up two nodes ($n0 and $n1) with UDP agents (udp0 and udp1) and
another node ($n3) with a Null agent (null3). This is a basic setup for sending UDP traffic
between nodes in a network simulation.

set cbr0 [new Application/Traffic/CBR]


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

We are creating Constant Bit Rate (CBR) traffic applications and attaching them to the UDP
agents in your ns-2 network simulation.

set cbr0 [new Application/Traffic/CBR]: This line creates a new CBR traffic application and
assigns it to the variable cbr0.

$cbr0 attach-agent $udp0: This line attaches the CBR traffic application cbr0 to the UDP
agent udp0.

This configuration suggests that CBR traffic will be generated by the application and sent
using the UDP agent associated with node $n0.

set cbr1 [new Application/Traffic/CBR]: Similarly, this line creates another CBR traffic
application and assigns it to the variable cbr1.

$cbr1 attach-agent $udp1: This line attaches the CBR traffic application cbr1 to the UDP agent
udp1.
This indicates that CBR traffic will be generated by the application and sent using the UDP
agent associated with node $n1.

$udp0 set class_ 1


$udp1 set class_ 2

We are setting the class parameter for the UDP agents in your ns-2 network simulation. In ns-2, the
"class" parameter is used to categorize or differentiate traffic classes.

$udp0 set class_ 1: This line sets the "class_" parameter of the UDP agent udp0 to the value 1. This
implies that the traffic generated by udp0 belongs to class 1.

$udp1 set class_ 2: Similarly, this line sets the "class_" parameter of the UDP agent udp1 to the value 2.
This indicates that the traffic generated by udp1 belongs to class 2.

Assigning different classes to traffic sources allows for the simulation of multiple classes of traffic with
potentially different characteristics or quality of service requirements. In a network simulation, you
might define different behaviors, such as different delay or loss characteristics, for traffic belonging to
different classes.

$ns connect $udp0 $null3


$ns connect $udp1 $null3
• These lines of code in an ns-2 simulation script establish connections between the UDP agents (udp0
and udp1) and the Null agent (null3). In the context of ns-2, the connect command is typically used to
define links between network components.

• $ns connect $udp0 $null3: This line connects the UDP agent udp0 to the Null agent null3. In a network
simulation, this connection could represent a communication link or path between the source (UDP
agent) and the destination (Null agent). It essentially sets up a path for traffic generated by udp0 to flow
to the Null agent null3.

• $ns connect $udp1 $null3: Similarly, this line connects the UDP agent udp1 to the Null agent null3. It
establishes another communication link or path for the traffic generated by udp1 to reach the Null agent
null3.

$cbr1 set packetSize_ 500Mb


$cbr1 set interval_ 0.005

a) We are configuring the parameters for the CBR traffic application associated with cbr1 in your ns-2
simulation script.

b) $cbr1 set packetSize_ 500Mb: This line sets the "packetSize_" parameter of the CBR traffic
application cbr1 to 500 megabits (Mb). This parameter typically defines the size of each packet
generated by the CBR traffic source. In this case, it implies that each packet will be 500 megabits in
size.

c) $cbr1 set interval_ 0.005: This line sets the "interval_" parameter of the CBR traffic application
cbr1 to 0.005 seconds. The "interval_" parameter usually represents the time interval between
successive packets generated by the CBR source. In this case, it suggests that a new packet will be
generated every 0.005 seconds.

d) These parameter settings define the characteristics of the traffic generated by the CBR source
associated with cbr1. The packet size and inter-packet interval are important factors that influence the
traffic pattern in the simulation. Adjusting these parameters allows you to control the rate and size of the
packets generated by the CBR traffic source.

$ns at 0.1 "$cbr0 start"


$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
We are scheduling events in your ns-2 simulation script using the at command.
$ns at 0.1 "$cbr0 start": This line schedules the start of the CBR traffic application associated with cbr0 at
time 0.1 seconds in the simulation. The start method is used to initiate the generation of CBR traffic from the
specified source.

$ns at 0.1 "$cbr1 start": Similarly, this line schedules the start of the CBR traffic application associated with
cbr1 at time 0.1 seconds. It initiates the generation of CBR traffic from the second source.

$ns at 10.0 "finish": This line schedules the event labeled as "finish" to occur at time 10.0 seconds in the
simulation. The event named "finish" could be a user-defined event or a placeholder for some action or
condition that you want to trigger after a specific duration.

These scheduled events help define the timeline of actions in your simulation. The CBR traffic sources are
started at 0.1 seconds, and something labeled "finish" is triggered at 10.0 seconds.

Explanation of AWK file

We are using an AWK script to process output data, likely from a network simulation.
1. BEGIN { count=0; }: This is the BEGIN block, which is executed before processing any input lines.
In this case, it initializes a variable count to 0.

2. if ($1=="d") count++: In the main body of the AWK script, it checks if the first field ($1) of the input
line is equal to "d". If true, it increments the count variable. This suggests that the script is counting the
number of lines where the first field is "d," which could indicate dropped packets in a network
simulation.

3. END { printf("Number of packets dropped is = %d\n", count); }: The END block is executed after
processing all input lines. It prints the count of dropped packets using the printf statement.

4. In summary, this AWK script processes input data, likely from a simulation log or output, and counts
the number of lines where the first field is "d," assuming that "d" signifies dropped packets. The result is
printed as the "Number of packets dropped."
Lab Experiment 2
Implement simple ESS and with transmitting nodes in wire -less LAN by simulation and
determine the performance with respect to transmission of packets.
Topology

Code-
#create Simulator class
set ns [new Simulator]

#open trace file


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

#create Topography object


set topo [new Topography]

#define grid size


$topo load_flatgrid 1000 1000

#open namtrace file


set nf [open lab2.nam w]
$ns namtrace-all-wireless $nf 1000 1000

#specify node configuration


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

#create a General Operation Director(god) object that stores the total number of mobile
nodes.
create-god 4

#create nodes and label them


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$n0 label "tcp0"
$n1 label "sink0"
$n2 label "bs1"
$n3 label "bs2"
#give initial x, y, z coordinates to nodes
$n0 set X_ 110
$n0 set Y_ 500
$n0 set Z_ 0
$n1 set X_ 600
$n1 set Y_ 500
$n1 set Z_ 0
$n2 set X_ 300
$n2 set Y_ 500
$n2 set Z_ 0
$n3 set X_ 450
$n3 set Y_ 500
$n3 set Z_ 0
#attach agent and application to nodes and connect them
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

#schedule the event


$ns at 0.5 "$ftp0 start"

#set up a destination for mobile nodes. They move to <x><y> coordinates at <s>m/s.
$ns at 0.3 "$n0 setdest 110 500 10"
$ns at 0.3 "$n1 setdest 600 500 20"
$ns at 0.3 "$n2 setdest 300 500 30"
$ns at 0.3 "$n3 setdest 450 500 30"
$ns at 10.0 "$n0 setdest 100 550 5"
$ns at 10.0 "$n1 setdest 630 450 5"
$ns at 70.0 "$n0 setdest 170 680 5"
$ns at 70.0 "$n1 setdest 580 380 5"

$ns at 120.0 "$n0 setdest 140 720 5"


$ns at 135.0 "$n0 setdest 110 600 5"
$ns at 140.0 "$n1 setdest 600 550 5"
$ns at 155.0 "$n0 setdest 89 500 5"
$ns at 190.0 "$n0 setdest 100 440 5"
$ns at 210.0 "$n1 setdest 700 600 5"
$ns at 240.0 "$n1 setdest 650 500 5"

proc finish { } {
global ns nt nf
$ns flush-trace
exec nam lab42.nam &
close $nt
close $nf
exit 0
}
$ns at 400 "finish"
$ns run

AWK file

BEGIN{
PktsSent=0;
PktsRcvd=0;
PktsAtRTR=0;
}
{
if(($1=="s")&&($4=="RTR")&&($7=="tcp")) PktsAtRTR++;
if(($1=="s")&&($4=="AGT")&&($7=="tcp")) PktsSent++;
if(($1=="r")&&($4=="AGT")&&($7=="tcp")) PktsRcvd++;
}
END{
print " Number of Packets Sent :" PktsSent
print " Number of Packets Received :" PktsRcvd
print " Pacjet Delivery Ratio :" PktsRcvd/PktsSent*100
print " Routing Load :" PktsAtRTR/PktsRcvd
}
Output-
$ns pgm6.tcl

$awk -f count.awk lab42.tr

Number of Packets Sent: 6819


Number of Packets Received: 6685
Packet Delivery Ratio: 98.0349
Routing Load: 1.02004

Explanation:

set topo [new Topography]

(a) The line set topo [new Topography] in an ns-2 script creates a new instance of the
Topography class and assigns it to the variable topo.

(b) The Topography class is used to model the spatial layout of nodes and their
connectivity in a network simulation.

(c) set topo: Assigns the newly created Topography object to the variable topo. This
variable can then be used to configure or manipulate the topography of the network in the
simulation.

(d) The Topography class in ns-2 is used in conjunction with nodes, links, and agents to
define the physical characteristics and layout of the simulated network.

$topo load_flatgrid 1000 1000

● The line $topo load_flatgrid 1000 1000 in an ns-2 script uses the load_flatgrid method
of the Topography object ($topo) to create a flat grid topology with specified
dimensions.

● $topo load_flatgrid 1000 1000: This command invokes the load_flatgrid method on the
Topography object ($topo). The method takes two arguments, 1000 and 1000, which
represent the dimensions (width and height) of the flat grid topology.

● The first argument (1000) specifies the width of the grid.

● The second argument (1000) specifies the height of the grid.

● This line essentially sets up a flat grid topology with a width of 1000 units and a height of
1000 units in the network simulation.

set nf [open lab2.nam w]


$ns namtrace-all-wireless $nf 1000 1000

● In an ns-2 script involve the creation of a nam (Network Animator) trace file and
configuring wireless tracing for visualization.

● set nf [open lab2.nam w]: This line opens a file named lab2.nam in write mode (w) and
assigns the file handle to the variable nf. This file will be used to store trace
information for visualization using nam.

● $ns namtrace-all-wireless $nf 1000 1000: This line configures nam to trace all wireless
events and writes the trace information to the file opened earlier (lab2.nam). The 1000
1000 arguments specify the dimensions of the visualization window in nam.

● $ns namtrace-all-wireless: Enables wireless tracing for all nodes and their wireless
communication.
● $nf: The file handle for the nam trace file.
● 1000 1000: Specifies the dimensions of the visualization window in nam. In this case, the
window is set to 1000x1000 units.

#specify node configuration


● Configure the characteristics of the nodes in your wireless ad-hoc network simulation.

-adhocRouting DSDV: Sets the ad-hoc routing protocol to DSDV (Destination Sequenced
Distance Vector). DSDV is a proactive routing protocol used in wireless ad-hoc networks.

-llType LL: Specifies the link layer type as LL (Link Layer).


-macType Mac/802_11: Specifies the Medium Access Control (MAC) layer type as
Mac/802_11, indicating the use of the IEEE 802.11 MAC protocol commonly used in wireless
networks.

-ifqType Queue/DropTail: Sets the interface queue type to Queue/DropTail, which is a simple
drop-tail queue.

-ifqLen 20: Sets the interface queue length to 20.

-phyType Phy/WirelessPhy: Specifies the physical layer type as Phy/WirelessPhy.

-channelType Channel/WirelessChannel: Sets the channel type to Channel/WirelessChannel.

-propType Propagation/TwoRayGround: Specifies the propagation model as


Propagation/TwoRayGround.

-antType Antenna/OmniAntenna: Sets the antenna type to Antenna/OmniAntenna, indicating


an omnidirectional antenna.

-topoInstance $topo: Associates the topology instance ($topo) with the node configuration.

-agentTrace ON: Enables tracing for agents.

-routerTrace ON: Enables tracing for routers.

create god-4
● To create a god object in ns-2. A god object is a special agent that has visibility into the
entire network and can control or monitor the activities of all other nodes in the
simulation.

● The line create-god 4 suggests that we are creating a god object for a network with
four nodes. This god object is used for various purposes, including routing and
monitoring.

$n0 set X_ 110


$n0 set Y_ 500
$n0 set Z_ 0

● We are configuring the spatial coordinates of the node $n0 in your ns-2 wireless ad-
hoc network simulation.
● $n0 set X_ 110: Sets the X-coordinate of node $n0 to 110. This likely represents the
horizontal position of the node in the simulation.
● $n0 set Y_ 500: Sets the Y-coordinate of node $n0 to 500. This likely represents the
vertical position of the node in the simulation.
● $n0 set Z_ 0: Sets the Z-coordinate of node $n0 to 0. The Z-coordinate is often used for
the elevation of the node in a three-dimensional space. In this case, the value 0
suggests that the node is at ground level.

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

The snippet sets up a simple TCP connection between two nodes ($n0 and $n1) using
agents and applications.

● set tcp0 [new Agent/TCP]: Creates a new TCP agent named $tcp0.
● $ns attach-agent $n0 $tcp0: Attaches the TCP agent $tcp0 to node $n0. This associates
the TCP agent with the first node in the simulation.
● set ftp0 [new Application/FTP]: Creates a new FTP application named $ftp0.
● $ftp0 attach-agent $tcp0: Attaches the FTP application to the TCP agent $tcp0. This
means that the FTP application will use the TCP agent for communication.
● set sink1 [new Agent/TCPSink]: Creates a TCP sink agent named $sink1. A TCP sink
is used to receive TCP data.
● $ns attach-agent $n1 $sink1: Attaches the TCP sink agent $sink1 to node $n1. This
associates the TCP sink with the second node in the simulation.
● $ns connect $tcp0 $sink1: Connects the TCP agent $tcp0 (sender) to the TCP sink
agent $sink1 (receiver). This establishes a TCP connection between the two nodes.
● In summary, this script sets up a basic TCP connection between nodes $n0 and $n1
using a TCP agent, an FTP application, and a TCP sink.
● The FTP application generates traffic that is sent by the TCP agent on node $n0 and
received by the TCP sink on node $n1. This is a simple example of a client-server
communication setup using TCP in ns-2.
$ns at 0.5 "$ftp0 start"

The line $ns at 0.5 "$ftp0 start" schedules the start of the FTP application ($ftp0) at
simulation time 0.5 seconds.

#set up a destination for mobile nodes. They move to <x><y> coordinates at <s>m/s.

● These lines schedule movements for nodes in your wireless ad-hoc network simulation
using the setdest mobility model.
● At time 0.3 seconds, nodes $n0, $n1, $n2, and $n3 are assigned initial destinations
using the setdest command. The format is "$node setdest X Y Speed", where X and Y
are the coordinates, and Speed is the speed of the node.
● At time 10.0 seconds, nodes $n0 and $n1 are assigned new destinations.
● At time 70.0 seconds, nodes $n0 and $n1 are assigned new destinations.
● At various later times, additional movements are scheduled for nodes $n0 and $n1.

Explanation for AWK file

● BEGIN block: Initializes variables before processing the file.


● Main block: Processes each line of the input file. The conditions inside the block
identify lines corresponding to the sending of TCP packets (s, AGT, tcp), receiving of
TCP packets (r, AGT, tcp), and packets arriving at the router (s, RTR, tcp). Counters
(PktsSent, PktsRcvd, PktsAtRTR) are updated accordingly.
● END block: Prints the calculated metrics based on the data processed from the trace
file. Metrics include the number of packets sent, the number of packets received,
packet delivery ratio, and routing load.
Lab Program 3 :
Write a program for error detecting code using CRC-CCITT (16- bits).
import java.util.Scanner;
public class CRC
{
public static int n;
public static void main(String[] args)
{
// create scanner class object to take input from user
Scanner sc=new Scanner(System.in);
// To create an instance of a class, we u se the new keyword followed by the constructor of the class
CRC crc=new CRC();
String copy,rec,code,zero="0000000000000000";
System.out.println("enter the dataword to be sent");
code=sc.nextLine();
System.out.println("The dataword is "+code);
n=code.length();
System.out.println("Length of the dataword="+n);

copy=code;
code+=zero;
System.out.println("The modified codeword is"+code);

code=crc.divide(code);
copy=copy.substring(0,n)+code.substring(n);
System.out.print("CRC=");
System.out.println(code.substring(n));
System.out.println("transmitted frame is="+copy);
System.out.println("enter received data:");
rec=sc.nextLine();
if(zero.equals(crc.divide(rec).substring(n)))
System.out.println("correct bits received");
else
System.out.println("received frame contains one or more error");
sc.close(); //Scanner is closed to release resources.

}
public String divide(String s)
{
String div="10001000000100001";
int i,j;
char x;
for(i=0;i<n;i++) // Outer loop for each bit of the dataword
{
x=s.charAt(i);
for(j=0;j<17;j++) // Inner loop for each bit of the CRC polynomial
{
if(x=='1') //If the current bit of the dataword (x) is '1'
{
if(s.charAt(i+j)!=div.charAt(j)) //it checks if the corresponding bits of the dataword and the CRC
polynomial are equal.

s=s.substring(0,i+j)+"1"+s.substring(i+j+1); //it performs an XOR operation and updates the


string s
else
s=s.substring(0,i+j)+"0"+s.substring(i+j+1); // it performs an XOR operation
}
}
}

return s;
}
}

CRC-16-CCITT (Consultative Committee for International Telephony and Telegraphy


x16 + x12 + x5 + 1
Output1:
# java CRC
enter the dataword to be sent
1001
The dataword is 1001
Length of the dataword=4
The modified codeword is10010000000000000000
CRC=1001000100101001
transmitted frame is=10011001000100101001
enter received data:
10011001000100101001
correct bits received
Output2:
# java CRC
enter the dataword to be sent
1100
The dataword is 1100
Length of the dataword=4
The modified codeword is11000000000000000000
CRC=1100000110001100
transmitted frame is=11001100000110001100
enter received data:
11001100000110001100 11
received frame contains one or more error

Explanation

Class and Variables:


● The class is named CRC.

● There is a static variable n representing the length of the dataword.

● The main method is the entry point of the program.

Input:
● The program takes user input for the dataword to be sent.

CRC Calculation:
● The divide method takes a string s (representing the dataword concatenated
with zeros for CRC) and performs the CRC division.
● The polynomial used for CRC is represented by the string div.

● The method iterates through each bit of the dataword, performing XOR
operations based on the CRC polynomial.
Transmission:
● The original dataword is displayed.

● Zeros are appended to the dataword to accommodate the CRC bits.

● The CRC bits are calculated using the divide method.

● The transmitted frame is displayed, consisting of the original dataword followed


by the CRC bits.
Error Detection:
● The program then takes user input for the received data.

● The received data is divided using the divide method.

● If the remainder obtained after division is all zeros, it is assumed that no errors
occurred, and the message indicates correct bits received. Otherwise, an error is
assumed.
Scanner Closure:
● The Scanner is closed to release resources.

Note:

● The CRC polynomial used in this example is "10001000000100001," which


corresponds to the CRC-CCITT polynomial.

The divide method performs the CRC division.


The method takes a string s as input, representing the dataword concatenated with zeros for
CRC.

It initializes a string div representing the CRC polynomial "10001000000100001."

The outer loop (for (i = 0; i < n; i++)) iterates through each bit of the dataword.

Inside the outer loop, the variable x is assigned the i-th bit of the dataword.

The inner loop (for (j = 0; j < 17; j++)) iterates through each bit of the CRC polynomial.

If the current bit of the dataword (x) is '1', it enters the conditional block.

Inside the conditional block, it checks if the corresponding bits of the dataword and the CRC
polynomial are equal.

If they are not equal, it performs an XOR operation ('1' XOR '1' = '0', '1' XOR '0' = '1') and
updates the string s.

If they are equal, it performs an XOR operation and updates the string s accordingly.

The method returns the modified string s after completing the CRC division.

This process essentially simulates the CRC division operation, where XOR operations are
performed based on the CRC polynomial, and the result is updated in the input string. The final
remainder is contained in the modified string s.
Java String charAt() Method

The charAt() method returns the character at the specified index in a string.

The index of the first character is 0, the second character is 1, and so on.

Syntax: public char charAt(int index)


Parameter Values ParameterDescription
index An int value representing the index of the character to return

Lab Experiment 4:
Implement transmission of ping messages/traceroute over a network topology consisting of 6
nodes and find the number of packets dropped due to congestion.

Topology-
Code:
#create Simulator object
set ns [new Simulator]

#open trace file


set nt [open Lab4.tr w]
$ns trace-all $nt

#open namtrace file


set nf [open Lab4.nam w]
$ns namtrace-all $nf

#create 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 nodes

$n0 label "ping0"


$n1 label "ping1"
$n2 label "ping2"
$n3 label "ping3"
$n4 label "ping4"
$n5 label "router"
#create links, specify the type, nodes, bandwidth, delay and ARQ algorithm for it
$ns duplex-link $n0 $n5 1Mb 10ms DropTail
$ns duplex-link $n1 $n5 1Mb 10ms DropTail
$ns duplex-link $n2 $n5 1Mb 10ms DropTail
$ns duplex-link $n3 $n5 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail

#set queue length


$ns queue-limit $n0 $n5 5
$ns queue-limit $n1 $n5 5
$ns queue-limit $n2 $n5 2
$ns queue-limit $n3 $n5 5
$ns queue-limit $n4 $n5 2
$ns color 2 Red
$ns color 3 Blue
$ns color 4 Green
$ns color 5 Yellow
#define ‘recv’ function for class Agent/Ping
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from $from with round-trip time $rtt ms"
}

#create ping agent and attach them to node


set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0
$p0 set class_ 1

set p1 [new Agent/Ping]


$ns attach-agent $n1 $p1
$p1 set class_ 2

set p2 [new Agent/Ping]


$ns attach-agent $n2 $p2
$p2 set class_ 3

set p3 [new Agent/Ping]


$ns attach-agent $n3 $p3
$p3 set class_ 4

set p4 [new Agent/Ping]


$ns attach-agent $n4 $p4
$p4 set class_ 5
#connect 2 agents
$ns connect $p2 $p4
$ns connect $p3 $p4

proc sendPingPacket { } {
global ns p2 p3

set intervalTime 0.001


set now [$ns now]
$ns at [expr $now + $intervalTime] "$p2 send"
$ns at [expr $now + $intervalTime] "$p3 send"
$ns at [expr $now + $intervalTime] "sendPingPacket"
}

proc finish { } {
global ns nt nf
$ns flush-trace
close $nt
close $nf
exec nam Lab4.nam &
exit 0
}
$ns at 0.1 "sendPingPacket"
$ns at 2.0 "finish"
$ns run

Awk file
BEGIN{
count=0;
}
{
if($1=="d")
count++
}
END{
printf ("Number of packets dropped is = %d\n",count);

Output:
node 3 received ping answer from 4 with round-trip time 66.4 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
node 3 received ping answer from 4 with round-trip time 66.5 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
node 3 received ping answer from 4 with round-trip time 66.5 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
#awk -f lab4.awk lab4.tr
Number of packets dropped is = 41

Explanation:

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id] received ping answer from $from with round-trip time $rtt ms"
}

● defines the recv method for the Agent/Ping class.

● Agent/Ping instproc recv {from rtt}: This line defines a method named recv for the Agent/Ping class.
The method takes two parameters: from and rtt.

● $self instvar node_: This line declares the variable node_ as an instance variable of the current object
($self). This variable is expected to hold a reference to the node associated with the Agent/Ping
instance.

● puts "node [$node_ id] received ping answer from $from with round-trip time $rtt ms": This line prints a
message to the console using the puts command. The message includes information about the node that
received the ping answer ([$node_ id]), the source of the ping answer ($from), and the round-trip time
($rtt).

● In summary, this recv method is responsible for handling received ping responses. It prints a message
indicating which node received a ping answer, the source of the answer, and the round-trip time.

set p0 [new Agent/Ping]


$ns attach-agent $n0 $p0
$p0 set class_ 1

set p1 [new Agent/Ping]


$ns attach-agent $n1 $p1
$p1 set class_ 2

set p2 [new Agent/Ping]


$ns attach-agent $n2 $p2
$p2 set class_ 3

set p3 [new Agent/Ping]


$ns attach-agent $n3 $p3
$p3 set class_ 4
set p4 [new Agent/Ping]
$ns attach-agent $n4 $p4
$p4 set class_ 5

● This code is responsible for creating instances of the Agent/Ping class, attaching them to specific
nodes, and setting a class_ attribute for each agent.

● set p0 [new Agent/Ping]: Creates a new instance of the Agent/Ping class and assigns it to the variable
p0.

● $ns attach-agent $n0 $p0: Attaches the Agent/Ping instance p0 to the node $n0.

● $p0 set class_ 1: Sets the class_ attribute of p0 to the value 1.

● This pattern is repeated for agents p1 through p4, each associated with a specific node ($n1 through $n4)
and assigned a unique class_ value ranging from 2 to 5.

● In the context of network simulation, these agents are likely used to generate ping traffic between nodes,
and the class_ attribute may be used to differentiate or categorize different types of agents. The
class_ attribute is a custom attribute that is being used in the simulation for a specific purpose.

proc sendPingPacket { } {
global ns p2 p3

set intervalTime 0.001


set now [$ns now]
$ns at [expr $now + $intervalTime] "$p2 send"
$ns at [expr $now + $intervalTime] "$p3 send"
$ns at [expr $now + $intervalTime] "sendPingPacket"
}

● The sendPingPacket procedure is responsible for scheduling the sending of ping packets by agents
p2 and p3.

● global ns p2 p3: Declares that the variables ns, p2, and p3 are global, meaning they can be accessed
and modified within the procedure.
● set intervalTime 0.001: Sets the intervalTime variable to 0.001 seconds. This variable represents the
time interval between successive ping packet transmissions.

● set now [$ns now]: Retrieves the current simulation time using the $ns now command and assigns it
to the variable now.

● $ns at [expr $now + $intervalTime] "$p2 send": Schedules the send method of agent p2 to be
executed after the current simulation time ($now) plus the specified intervalTime.

● $ns at [expr $now + $intervalTime] "$p3 send": Similar to the previous line, schedules the send method
of agent p3 to be executed after the specified time interval.

● $ns at [expr $now + $intervalTime] "sendPingPacket": Schedules the sendPingPacket procedure to be


called again after the specified time interval, creating a loop for periodic ping packet transmissions.

● In summary, this procedure is used to create a continuous loop that schedules the sending of ping
packets by agents p2 and p3 at regular intervals during the ns-2 simulation. The specific time
interval is controlled by the intervalTime variable. This mechanism simulates the periodic behavior of
ping traffic in the network.

$ns at 0.1 "sendPingPacket"

● The line $ns at 0.1 "sendPingPacket" in the ns-2 script schedules the execution of the sendPingPacket
procedure after a delay of 0.1 seconds (100 milliseconds)

● $ns at 0.1 "sendPingPacket": This command schedules the execution of the sendPingPacket procedure at
simulation time 0.1 seconds.

● The simulation time is an important aspect of discrete event simulation systems like ns-2, and it
represents the elapsed time in the simulated environment..

● In this context, it means that after 0.1 seconds into the simulation, the sendPingPacket procedure will
be executed, initiating the periodic sending of ping packets by agents p2 and p3 at the specified
interval.
$ns at 2.0 "finish"

● The line $ns at 2.0 "finish" schedules the execution of the finish procedure at simulation time 2.0
seconds.
● $ns at 2.0 "finish": This command schedules the execution of the finish procedure at simulation time
2.0 seconds.
● The finish procedure, as described in the script, is responsible for flushing traces, closing trace files,
opening the nam visualizer, and exiting the simulation. This ensures that the simulation results are
properly handled and visualized after the specified simulation time.

You might also like