Professional Documents
Culture Documents
Network simulators implemented in software are valuable tools for researchers to develop,
test, and diagnose network protocols. Simulation is economical because it can carry out
experiments without the actual hardware. It is flexible because it can, for example, simulate a
link with any bandwidth and propagation delay. Simulation results are easier to analyze than
experimental results because important information at critical points can be easily logged to
help researchers diagnose network protocols.
Network simulators, however, have their limitations. A complete network simulator needs to
simulate networking devices (e.g., hosts and routers) and application programs that generate
network traffic. It also needs to provide network utility programs to configure, monitor, and
gather statistics about a simulated network. Therefore, developing a complete network
simulator is a large effort. Due to limited development resources, traditional network
simulators usually have the following drawbacks:
• Simulation results are not as convincing as those produced by real hardware and software
equipment. In order to constrain their complexity and development cost, most network
simulators usually can only simulate real-life network protocol implementations with limited
details, and this may lead to incorrect results.
• These simulators are not extensible in the sense that they lack the standard UNIX POSIX
application programming interface (API). As such, existing or to-be-developed real-life
application programs cannot run normally to generate traffic for a simulated network.
Instead, they must be rewritten to use the internal API provided by the simulator (if there is
any) and be compiled with the simulator to form a single, big, and complex program.
To overcome these problems, Wang invented a kernel re-entering simulation methodology and
used it to implement the Harvard network simulator. Later on, Wang further improved the
methodology and used it to design and implement the NCTUns network simulator and
emulator.
Different types of simulators
Some of the different types of simulators are as follows:-
MIT's NETSIM
NIST
CPSIM
INSANE
NEST
REAL
NS
OPNET
NCTUns
P
GNS3
WireShark
Syllabus
NETWORKS LABORATORY
IA Marks: 25
Exam Hours: 03
Sub Code: 10CSL77 Exam Marks: 50
Hrs/ Week: 03
Total Hrs. 42
PART A
SIMULATION EXERCISES
1. Simulate a three nodes point-to-point network with duplex links between them. Set
the queue size and vary the bandwidth and find the number of packers dropped.
2. Simulate a four node point-to-point network and connect the links as follows: n0 –n2,
n1 – n2 and n2 – n3. Apply TCP agent between n0 – n3 and UDP n1 – n3. Apply relevant
applications over TCP and UDP agents changing the parameter and determine the
number of packets sent by TCP / UDP.
4. Simulate an Ethernet LAN using N nodes (6-10). Change error rate and data rate and
compare throughput.
5. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
6. Simulate simple ESS and with transmitting nodes in wireless LAN by simulation and
determine the performance with respect to transmission of packets.
PART-A
Introduction to NS-2:
● Widely known as NS2, is simply an event driven simulation tool.
● 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.
● In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviors.
Tcl scripting
● Tcl is a general purpose scripting language. [Interpreter]
● Tcl runs on most of the platforms such as Unix, Windows, and Mac.
● The strength of Tcl is its simplicity.
● It is not necessary to declare a data type for variable prior to the usage.
Basics of TCL
Syntax: command arg1 arg2 arg3
●
Hello World!
puts stdout{Hello, World!}
Hello, World!
⚪
Variables Command Substitution
set a 5 set len [string length foobar]
set b $a set len [expr [string length foobar] + 9]
●
Simple Arithmetic
expr 7.2 / 4
●
Procedures
proc Diag {a b} {
Which is thus the first line in the tcl script? This line declares a new variable as using the set
command, you can call this variable as you wish, In general people declares it as ns because it is
an instance of the Simulator class, so an object the code[new Simulator] is indeed the installation
of the class Simulator using the reserved word new.
In order to have output files with data on the simulation (trace files) or files used for visualization
(nam files), we need to create the files using ―open‖ command:
#Open the Trace file
set tracefile1 [open out.tr w]
The above creates a dta trace file called ―out.tr‖ and a nam visualization trace file called
―out.nam‖.Within the tcl script,these files are not called explicitly by their names,but instead by
pointers that are declared above and called ―tracefile1‖ and ―namfile‖ respectively.Remark that
they begins with a # symbol.The second line open the file ―out.tr‖ to be used for writing,declared
with the letter ―w‖.The third line uses a simulator method called trace-all that have as parameter
the name of the file where the traces will go.
The last line tells the simulator to record all simulation traces in NAM input format.It also
gives the file name that the trace will be written to later by the command $ns flush-trace.In our
case,this will be the file pointed at by the pointer ―$namfile‖,i.e the file ―out.tr‖.
The termination of the program is done using a ―finish‖ procedure.
#Define a „finish‟ procedure
Proc finish { } {
$ns flush-trace
Close $tracefile1
Close $namfile
Exit 0
The word proc declares a procedure in this case called finish and without arguments. The
word global is used to tell that we are using variables declared outside the procedure. The simulator
method ―flush-trace” will dump the traces on the respective files. The tcl command ―close”
closes the trace files defined before and exec executes the nam program for visualization. The
command exit will ends the application and return the number 0 as status to the system. Zero is
the default for a clean exit. Other values can be used to say that is a exit because something fails.
At the end of ns program we should call the procedure ―finish‖ and specify at what time
the termination should occur. For example,
$ns at 125.0 “finish”
will be used to call ―finish‖ at time 125sec.Indeed,the at method of the simulator allows us to
schedule events explicitly.
The simulation can then begin using the command
$ns run
Which means that $n0 and $n2 are connected using a bi-directional link that has 10ms of
propagation delay and a capacity of 10Mb per sec for each direction.
To define a directional link instead of a bi-directional one, we should replace ―duplex-
link‖ by ―simplex-link‖.
In NS, an output queue of a node is implemented as a part of each link whose input is that
node. The definition of the link then includes the way to handle overflow at that queue. In our case,
if the buffer capacity of the output queue is exceeded then the last packet to arrive is dropped.
Many alternative options exist, such as the RED (Random Early Discard) mechanism, the FQ (Fair
Queuing), the DRR (Deficit Round Robin), the stochastic Fair Queuing (SFQ) and the CBQ (which
including a priority and a round-robin scheduler).
In ns, an output queue of a node is implemented as a part of each link whose input is that
node. We should also define the buffer capacity of the queue related to each link. An example
would be:
#set Queue Size of link (n0-n2) to 20
Scheduling Events
NS is a discrete event based simulation. The tcp script defines when event should occur.
The initializing command set ns [new Simulator] creates an event scheduler, and events are then
scheduled using the format:
$ns at <time> <event>
The scheduler is started when running ns that is through the command $ns run.
The beginning and end of the FTP and CBR application can be done through the following
command
$ns at 0.1 “$cbr start”
1. The first field is the event type. It is given by one of four possible symbols r, +, -, d which
correspond respectively to receive (at the output of the link), enqueued, dequeued and dropped.
2. The second field gives the time at which the event occurs.
3. Gives the input node of the link at which the event occurs.
4. Gives the output node of the link at which the event occurs.
5. Gives the packet type (eg CBR or TCP)
6. Gives the packet size
7. Some flags
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 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 [options] file-name
Awk- An Advanced
Here, selection_criteria filters input and select lines for the action component to act upon.
The selection_criteria is enclosed within single quotes and the action within the curly braces.
Both the selection_criteria and action forms an awk program.
Example: $ awk „/manager/ {print}‟ emp.lst
Variables
Awk allows the user to use variables of there choice. You can now print a serial number,
using the variable kount, and apply it those directors drawing a salary exceeding 6700:
$ awk –F”|” „$3 == “director” && $6 > 6700
{ kount =kount+1
printf “ %3f %20s %-12s %d\n”, kount,$2,$3,$6 }‟ empn.lst
PROGRAM NO 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 band width
set ns [new Simulator] #create a new simulator object
$ns duplex-link $n0 $n2 1Mb 10ms DropTail # create a duplex link between the nodes
$ns duplex-link $n1 $n2 1Mb 5ms DropTail
$ns duplex-link $n2 $n3 1.4Mb 10ms DropTail
$ns run
BEGIN {
c=0;
}
{
if ($1=="d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
END{
printf("The number of packet dropped= %d\n",c);
}
Band width v/s packet drops
Band width Packet drop
1 111
1.1 91
1.2 71
1.3 52
1.4 32
1.5 12
1.6 0
Program 2
Implement a 4 node point to point network with links n0-n2, n1-n2 and n2-n3 apply TCP
agent between n0-n3 and UDP agent between n1-n3 apply relevant application over TCP and
UDP agent changing the parameter and determine the no of pocket sent by tcp/udp
BEGIN{udp=0;
tcp=0;
}
{
if($1=="r" && $5=="cbr")
{
udp++;
}
else if($1=="r" && $5=="tcp")
{
tcp++;
}
}
END
{
printf("Number of packets sent by TCP=%d\n",tcp);
printf("Number of packets sent by UDP=%d\n",udp);
}
Out put
Number of packets sent by TCP=676
Number of packets sent by UDP=210
Program 3
Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by changing the error
rate and data rate
set ns [new Simulator]
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab3.nam &
exit
}
$ns make-lan "$n0 $n1 $n2 $n3" 100Mb 300ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6 $n7" 100Mb 300ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n3 $n4 100Mb 300ms DropTail
$ns duplex-link-op $n3 $n4 color "green"
$ns duplex-link-op $n3 $n4 orient right
set err [new ErrorModel]
$ns lossmodel $err $n3 $n4
$err set rate_ 0.5
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set fid_ 0
$cbr set packetSize_ 1000
$cbr set interval_ 0.0001
set null [new Agent/Null]
$ns attach-agent $n7 $null
$ns connect $udp $null
BEGIN{
pkt=0;
time=0;
}
{
if($1=="r" && $3=="9" && $4=="7"){
pkt = pkt + $6;
time =$2;
}
}
END {
printf("throughput:%fMbps",(( pkt / time) * (8 / 1000000)));
}
Ouput
For Error rate of 0.1
Throughput 33.59256 Mbps
For Error rate of 0.5
Throughput 1.7549Mbps
Program 4
Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and obtain
congestion window for different sources/destinations.
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab4.nam &
exit
}
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
Output
Time =0.0 congestion Window=1.0
Time 2.24 congestion window=20.44
NAM WINDOW
Program 5
Implement ESS with transmission nodes in wireless LAN and obtain the performance
parameters
set ns [new Simulator]
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 set X_ 0
$n0 set Y_ 0
$n0 set Z_ 0
$n1 set X_ 0
$n1 set Y_ 0
$n1 set Z_ 0
$n2 set X_ 0
$n2 set Y_ 0
$n2 set Z_ 0
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab5.nam &
close $tf
exit 0
}
$ns at 150 "finish"
$ns run
Awk file
BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_1_"&&$4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1:%f Mbps \n",((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps",((count2*pack2*8)/(time2*1000000)));
}
Output
The throughput from no to n1:5863.4422Mbps
The throughput from n1 to n2:1307.611Mbps
Data transfer between node-1 and node-2
Program 6
Implement of link state routing algorithm
set ns [new Simulator]
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
$ns rtproto LS
$ns at 45 "finish"
$ns run
PART B
Bit stuffing
Byte stuffing
Byte Stuffing
Byte Stuffing
Program no 1
Bit stuffing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int i=0,j=0,k,d,ext=8,m;
char a[50]="",flag[]="01111110",temp[50]="";
//printf("enter the data=\n");
//scanf("%s",a);
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&a[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",a[i]);
strcpy(temp,flag);
for(i=0,j=0;i<strlen(a);i++)
{
if(a[i]=='1')
j++;
else
j=0;
temp[i + ext]=a[i];
if(j==5)
{
++ext;
temp[i+ ext]='0';
j=0;
}
}
strcat(temp,flag);
printf("\nStuffed bit sequence = \n%s",temp);
}
Program2
Character stuffing
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char c[50],d[50],t[50];
int i,m,j;
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';j++;
d[j]='l'; j++;
d[j]='e'; j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';m++;
d[m]='l';m++;
d[m]='e';m++;
d[m]='e';m++;
d[m]='t';m++;
d[m]='x';m++;
printf("\n\n transmitted data: \n");
for (i=0;i<m;i++)
{
printf("%c",d[i]);
}
}
Program 3
CRC code using CRC-CCITT polynomial
#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[128], cs[128], g[]="1010",dmy[128];
int a, e, c, b;
void xor()
{
for(c=1; c<N; c++)
cs[c] = ((cs[c] == g[c])? '0' : '1');
}
void crc()
{
for(e=0; e<N; e++)
cs[e] = t[e];
do
{
if(cs[0] == '1')
xor();
for(c=0; c<N-1; c++)
{
cs[c] = cs[c+1];
}
cs[c] = t[e++];
}
while(e<=a+N-1);
}
void main()
{
printf("\nEnter data t: ");
scanf("%s", t);
printf("\nPredefined Generator Polynomial is : %s", g);
a = strlen(t);
for(e=a; e<a+N-1; e++)
t[e] = '0';
printf("\nModified t[u] is : %s", t);
crc();
printf("\nChecksum is : %s", cs);
for(e=a; e<a+N-1; e++)
t[e] = cs[e-a];
printf("\nFinal Codeword is : %s", t);
printf("\n\nTest Error detection 1(yes) 0(no) ? : ");
scanf("%d", &b);
if(b==1)
{
printf("\nEnter position where error is to inserted : ");
scanf("%d", &e);
t[e] = (t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n", t);
crc();
printf("\nChecksum is : %s", cs);
if(strcmp(cs,dmy)==0)
Calculation at transmitter
G=1 1 0 0
Data entered is t=1 1 1 0
Modified sequence=1 1 1 0 0 0 0
___________
1 1 0 0|1 1 1 0 0 0 0 | 1011
|1 1 0 0
0100
0000
01000
1100
01000
1100
0100
The transmitted signal is t+ remainder=1 1 1 0 1 0 0=M
Calculation at receiver
With no error in transmitted signal, we should get remainder as zero after polynomial division
___________
1 1 0 0|1 1 1 0 0 0 0 | 1011
|1 1 0 0
0100
0000
01000
1100
01000
1100
0100
Program no 4 Dijkstra’s algorithm
prevNode[i] = v;
dist[i] = cost[v][i];
}
}
visited[v] = 1;
dist[v] = 1;
prevNode[v] = v;
count = 2;
while (count <= n)
{
min = 999;
for (w = 0; w < n; w++)
if ((dist[w] <= min) && (visited[w] != 1))
{
min = dist[w];
u = w;
}
visited[u] = 1;
count++;
for (w = 0; w < n; w++)
if ((dist[u] + cost[u][w] < dist[w]) &&
(visited[w] != 1))
{
prevNode[w] = u;
}
}
}
void main()
{
int n, v, cost[10][10], dist[10], prevNode[10], i, j;
printf("Enter number of vertices:");
scanf("%d", &n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d", &v);
dijkstra(n, v, cost, dist, prevNode);
printf("\nShortest path from \n");
for (i = 0; i < n; i++)
if (i != v) printf("\n%d -> %d = %d; prev node = %d", v, i, dist[i], prevNode[i]);
printf("\n");
}
Program 5 Distance vector algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int d[10][10], t[10][10], a[10][10], i, j, n, k, count;
printf("Enter the number of nodes\n");
scanf("%d", &n);
printf("Enter the initial cost matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", &d[i][j]);
d[i][i] = 0;
t[i][j] = d[i][j];
a[i][j] = j;
}
}
do
{
count = 0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
if(t[i][j]>(d[i][k]+t[k][j]))
{
t[i][j] = (t[i][k] + t[k][j]);
a[i][j] = k;
count++;
}
}
}
}
}
while(count!=0);
for(i=1; i<=n; i++)
{
printf("\n\n For router %d\n", i);
for(j=1; j<=n; j++)
{
printf("\t\nto node %d, via %d, Distance -> %d ",j, a[i][j], t[i][j]);
}
}
printf("\n\n");
return 0;
}
Program no 6
Implementation of stop and wait protocol and sliding window protocol
#include<stdio.h>
void sender(void);
void receiver(void);
int frame=0,ack=0,frame_no=1;
int lost_frame=1,lost_ack=2;
int seq_no=0,ack_no=0,timer=0,ready=1,max_frames=3;
void main()
{
printf("Enter no of frames\n");
scanf("%d",&max_frames);
while(frame_no<=max_frames)
sender();
sleep(2);
if(frame_no==lost_frame)
frame=0;
lost_frame=0;
receiver();
sleep(2);
ack=0;
lost_ack=0;
if(ack==0)
sleep(4);
timer=1;
}
}
void sender(void)
if(ready)
frame=1;
ready=0;
timer=0;
else
if(timer==1)
frame=1;
timer=0;
if(ack==1)
seq_no=!seq_no;
ack=0;
frame_no=frame_no+1;
if(frame_no<=max_frames)
{
printf("\nFrame %d is sent with seq no %d\n",frame_no,seq_no);
frame=1;
ready=0;
timer=0;
void receiver(void)
if(frame==1)
frame=0;
if(ack_no==seq_no)
ack_no=!ack_no;
ack=1;
else
printf("\nFrame %d is duplicate and discarded ack with ack no %d has been sent\n",frame_no,ack_no);
ack=1;
}
Program 7 write a program for congestion control using leaky bucket algorithm
Output:
Compile and run
$ cc –o Congestion Congestion.c
$ ./Congestion
Enter The Bucket Size
5
Enter The Operation Rate
2
5
Enter The Size Of The Packet Entering At 1 sec
4
Enter The Size Of The Packet Entering At 1 sec
3
1 5 2 3 0
2 4 2 3 2
3 3 2 3 1
4 0 2 1 0
5 0 1 0 0
Prepared by: D S MANJUNATH
VIVA QUESTIONS
1. What are functions of different layers?
2. Differentiate between TCP/IP Layers and OSI Layers
3. Why header is required?
4. What is the use of adding header and trailer to frames?
5. What is encapsulation?
6. Why fragmentation requires?
7. What is MTU?
8. Which layer imposes MTU?
9. Differentiate between flow control and congestion control.
10. Differentiate between Point-to-Point Connection and End-to-End connections.
11. What are protocols running in different layers?
12. What is Protocol Stack?
13. Differentiate between TCP and UDP.
14. Differentiate between Connectionless and connection oriented connection.
15. Why frame sorting is required?
16. What is meant by subnet?
17. What is meant by Gateway?
18. What is an IP address?
19. What is MAC address?
20. Why IP address is required when we have MAC address?
21. What is meant by port?
22. What are ephemerical port number and well known port numbers?
23. What is a socket?
24. What are the parameters of socket()?
25. Describe bind(), listen(), accept(),connect(), send() and recv().
26. What are system calls? Mention few of them.
27. What is IPC? Name three techniques.
28. Explain mkfifo(), open(), close() with parameters.
Prepared by: D S MANJUNATH