Experiment - 1: AIM: Installation of Network Simulator (NS2) On Ubuntu 14.04
Experiment - 1: AIM: Installation of Network Simulator (NS2) On Ubuntu 14.04
Experiment - 1: AIM: Installation of Network Simulator (NS2) On Ubuntu 14.04
Most simulators that are available today are based on a GUI application like the NCTUNS while some
others incl. NS2 are CLI based. Simulating the network involves configuring the state elements like links,
switches, hubs, terminals, etc. and also the events like packet drop rate, delivery status and so on. The
most important output of the simulations are the tracfiles. Trace files log every packet, every event that
occurred in the simulation and are used for analysis. Network simulators can also provide other tools to
facilitate visual analysis of trends and potential trouble spots. Most of the simulation is performed in
discrete time intervals where events that are in the queue are processed one after the other in an order.
Since simulation is a complex task, we cannot guarantee that all the simulators can provide exact or
accurate results for all the different type of information. Examples of network simulators are: ns,
NCTUNS, NetSim, etc.
ns2 is a name for series of discrete event network simulators like ns-1, ns-2 and ns-3. All of them are
discrete-event network simulators, primarily used in research and teaching. ns2 is free software, publicly
available under the GNU GPLv2 license for research, development, and use. This post deals with the
installation of "ns2" also called the "network simulator 2" in Ubuntu 14.04.
cd ~/
tar -xvzf ns-allinone-2.35.tar.gz
All the files will be extracted into a folder called "ns-allinone-2.35".
Once the installation is over, we have to make a change in the "ls.h" file. Use the following steps to make
the changes:
Navigate to the folder "linkstate", use the following command. Here it is assumed that the ns folder
extracted is in the home folder of your system.
cd ~/ns-allinone-2.35/ns-2.35/linkstate
Now open the file named "ls.h" and scroll to the 137th line. In that change the word "error" to "this>error". The image below shows the line 137 (highlighted in the image below) after making the changes
to the ls.h file.To open the file use the following command:
gedit ls.h
Now there is one more step that has to be done. We have to tell the ns which version of GCC will be used.
To do so, go to your ns folder and type the following command:
In the file, change Change CC= @CC@ to CC=gcc-4.4, as shown in the image below.
4. Installation:
Now we are ready to install ns2. To do so we first require root privileges and then we can run the
install script. Use the following two commands:
sudo su cd ~/ns-allinone-2.35/./install
The following is a snap of these commands:
It took almost 6 minutes to build and install ns2 on my system. But before we run it, we need to add
the build path to the environment path.
# LD_LIBRARY_PATH
OTCL_LIB=/home/akshay/ns-allinone-2.35/otcl-1.14
NS2_LIB=/home/akshay/ns-allinone-2.35/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB:$X11_LIB:
$USR_LOCAL_LIB
# TCL_LIBRARY
TCL_LIB=/home/akshay/ns-allinone-2.35/tcl8.5.10/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB
# PATH
XGRAPH=/home/akshay/ns-allinone-2.35/bin:/home/akshay/ns-allinone2.35/tcl8.5.10/unix:/home/akshay/ns-allinone-2.35/tk8.5.10/unix
#the above two lines beginning from xgraph and ending with unix should come on the same line
NS=/home/akshay/ns-allinone-2.35/ns-2.35/
NAM=/home/akshay/ns-allinone-2.35/nam-1.15/
PATH=$PATH:$XGRAPH:$NS:$NAM
Once the changes have been made, save the file and restart the system.
6 . Running NS2:
Once the system has restarted, open a terminal and start ns2 by using the following command:
ns
If the installation is correct then the terminal looks like the image below :
EXPERIMENT- 2
AIM: PC to PC communication.
OUTPUT
EXPERIMENT 3
AIM: Implementation of ring topology
OUTPUT
EXPERIMENT NO. - 4
OUTPUT:
EXPERIMENT NO. -5
AIM: To show link failure between nodes of a given topology.
set ns [new Simulator]
$ns rtproto DV
set nf [open out.nam w]
$ns namtrace-all $nf
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
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]
set n7 [$ns node]
set n8 [$ns node]
set n9 [$ns node]
set n10 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n6 1Mb 10ms DropTail
$ns duplex-link $n6 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n7 1Mb 10ms DropTail
$ns duplex-link $n7 $n8 1Mb 10ms DropTail
$ns duplex-link $n8 $n9 1Mb 10ms DropTail
$ns duplex-link $n9 $n10 1Mb 10ms DropTail
$ns duplex-link $n10 $n3 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $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 $n3 $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n1 $n2
$ns rtmodel-at 1.5 down $n5 $n6
$ns rtmodel-at 2.0 up $n5 $n6
$ns rtmodel-at 2.5 up $n1 $n2
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
OUTPUT:
OUTPUT:
EXPERIMENT NO. -6
Write a program to draw X-graph.
set ns [new Simulator]
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]
set n0 [$ns node]
set n1 [$ns node]
OUTPUT:
EXPERIMENT NO. 7
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
#Create three nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#Define a 'recv' function for the 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 two ping agents and attach them to the nodes n0 and n2
set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0
set p1 [new Agent/Ping]
$ns attach-agent $n2 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.6 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 1.0 "finish"
#Run the simulation
$ns run
OUTPUT:
EXPERIMENT NO. 8
$0 ~/^f.* RTR/ {
fowardLine ++ ;
}
END {
printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine,
(recvLine/sendLine),fowardLine;
}
OUTPUT:
EXPERIMENT NO. 9
# Create HA and FA
set HA [$ns_ node 1.0.0]
set FA [$ns_ node 2.0.0]
$HA random-motion 0
$FA random-motion 0
# Position (fixed) for base-station nodes (HA & FA).
$HA set X_ 1.000000000000
$HA set Y_ 2.000000000000
$HA set Z_ 0.000000000000
$FA set X_ 650.000000000000
$FA set Y_ 600.000000000000
$FA set Z_ 0.000000000000
# create a mobilenode that would be moving between HA and FA.
# note address of MH indicates its in the same domain as HA.
$ns_ node-config -wiredRouting OFF
set MH [$ns_ node 1.0.1]
set node_(0) $MH
set HAaddress [AddrParams addr2id [$HA node-addr]]
[$MH set regagent_] set home_agent_ $HAaddress
# movement of the MH
$MH set Z_ 0.000000000000
$MH set Y_ 2.000000000000
$MH set X_ 2.000000000000
# MH starts to move towards FA
$ns_ at 100.000000000000 "$MH setdest 640.000000000000 610.000000000000
20.000000000000"
# goes back to HA
$ns_ at 200.000000000000 "$MH setdest 2.000000000000 2.000000000000 20.000000000000"
# create links between wired and BaseStation nodes
$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $HA 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $FA 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $HA orient left-down
$ns_ duplex-link-op $W(1) $FA orient right-down
# setup TCP connections between a wired node and the MobileHost
CSMA/CD is the most commonly used protocol for LANs. It was developed by Digital
Equipment Corporation (DEC), Intel and Xerox. This network is called Ethernet. The IEEE
802.3 CSMA/CD standard for LAN is based on Ethernet specification.
A station with a message to send must monitor the channel to see if any other station is sending.
If another station is sending, the second station must wait until the sending station has finished.
Then it may send its message. If no station was sending at the time that it first listened, the
station may send its message immediately. The term carrier sense indicates this listening
before transmitting behavior.
All stations are attached to one path and monitor the signal on the channel through transceiver
attached to the cable. CSMA/CD has totally decentralized control and is based on contention
access.
CSMA/CD supports both baseband and broadband system. CSMA/CD offers four options in
terms of bit rate, signaling method and maximum electrical cable segment length.
These are:
10 BASE 5
10 BASE 2
1 BASE 5
10 BROAD 36
? The number at the beginning indicates the bit rate in Mbps.
? The middle term indicates the type of signaling.
? The number at the end indicates the cable length in multiples of 100 meters.
Base band (Digital signal): Manchester encoding.
Broad band (Analog signal): Differential Phase Shift Keying encoding.
The format of MAC frames in CSMA/CD:
Preamble: 56 bits of altering 1s and 0s.
SFD: Start Frame Delimiter. This signals the beginning of the frame.
Destination address: Contains the physical address of the packets destination address.
Source address: Contains the physical address of the packets senders address.
Length PDU: Indicates the number of bytes in the coming Protocol Data Unit (PDU).
The data unit in the upper layer of Data Link Layer (Logical Link Control) is the PDU.
Data and Padding: It contains the information and padding is done if necessary.
CRC: Cyclic Redundancy Check. It contains the error detection information.
DSAP: Destination Service Access Point.
SSAP: Source Service Access Point.
Control:It is used for flow management. The frames are of 3 types as in HDLC:
If the first bit in the control field is 0, it is I-frame.
If the two bits in the control field are 10, it is S-frame.
If the two bits in the control field are 11, it is U-frame.
The PDU from the LLC has no CRC, and no station address. These fields are added in the MAC
layer (lower layer in the Data Link Layer).
Result:
Thus the Ethernet LAN protocol with its frame format was studied.
Algorithm:
Step 1: Declare two frame variables and assign them the data frames to be transmitted.
Step 2: Initialize the CSMA/CD function.
Step 3: Send data from node B to node A and sense multiple access.
Step 4: Send data from node A to node C and sense multiple access.
Step 5: Check if collision occurs.
Step 6: If collision occurs wait for 1000ms and then retransmit the two data from the nodes one
after the other.
Step 7: Stop the execution.
Program:
include
void main()
{
Frame X,Y;
X="A.R.Engg";
Y="III-ECE";
CSMACD_INIT();
CSMACD_START();
CSMA_SEND(B,A,X);
CSMA_SEND(A,C,Y);
R=COLLISION_OCCUR();
if(R)
{
WAIT(1000);
RETRANSMIT(B,A);
RETRANSMIT(A,C);
}
}
Output:
1. Two Stations Transmit data X and Y simultaneously:
2. Occurrence of Collision:
3. Retransmit the data X from Station B to Station A:
4. Retransmit the data Y from Station A to Station C:
Result:
Thus the CSMA/CD protocol was simulated using ViRtSim software was studied.