Professional Documents
Culture Documents
Introduction to NS-2
NS (Version 2) is an open source network simulation tool. It is an object oriented, discrete
event driven simulator written in C++ and Otcl. The primary use of NS is in network researches
to simulate various types of wired/wireless local and wide area networks; to implement network
protocols such as TCP and UPD, traffic source behavior such as FTP, Telnet, Web, CBR and
VBR, router queue management mechanism such as Drop Tail, RED and CBQ, routing
algorithms such as Dijkstra, and many more.
Ns2 is written in C++ and Otcl to separate the control and data path implementations. The
simulator supports a class hierarchy in C++ (the compiled hierarchy) and a corresponding
hierarchy within the Otcl interpreter (interpreted hierarchy).
The reason why ns2 uses two languages is that different tasks have different requirements:
For example simulation of protocols requires efficient manipulation of bytes and packet headers
making the run-time speed very important. On the other hand, in network studies where the aim
is to vary some parameters and to quickly examine a number of scenarios the time to change the
model and run it again is more important.
In ns2, C++ is used for detailed protocol implementation and in general for such cases where
every packet of a flow has to be processed. For instance, if you want to implement a new
queuing discipline, then C++ is the language of choice. Otcl, on the other hand, is suitable for
configuration and setup. Otcl runs quite slowly, but it can be changed very quickly making the
construction of simulations easier. In ns2, the compiled C++ objects can be made available to the
Otcl interpreter. In this way, the ready-made C++ objects can be controlled from the OTcl level.
Otcl basics
This chapter introduces the syntax and some basic commands of the Otcl language that are
used by ns2. It is important to understand how Otcl works before moving to the part that deals
with the creation of the actual simulation scenario.
set a 5
In the first line, the variable a is assigned the value “5”. In the second line, the result of the
command [expr $a/5], which equals 1, is then used as an argument to another command, which
in turn assigns a value to the variable b. The “$” sign is used to obtain a value contained in a
variable and square brackets are an indication of a command substitution.
Procedures
One can define new procedures with the proc command. The first argument to proc is the
name of the procedure and the second argument contains the list of the argument names to that
procedure. For instance a procedure that calculates the sum of two numbers can be defined as
follows:
proc sum {a b} {
expr $a + $b
proc factorial a {
if {$a <= 1} {
return 1
It is also possible to give an empty string as an argument list. However, in this case the
variables that are used by the procedure have to be defined as global. For instance:
proc sum {} {
global a b
expr $a + $b
The first line of the file can be stored to a list with a command:
Now it is possible to obtain the elements of the list with commands (numbering of elements
starts from 0) :
Calling subprocesses
The command exec creates a subprocess and waits for it to complete. The use of exec is
similar to giving a command line to a shell program. For instance, to remove a file:
exec rm $testfile
The exec command is particularly useful when one wants to call a tcl-script from within
another tclscript. For instance, in order to run the tcl-script example.tcl multiple times with the
value of the parameter “test” ranging from 1 to 10, one can type the following lines to another
tcl-script:
The simulator object has member functions that enable creating the nodes and the links,
connecting agents etc. All these basic functions can be found from the class Simulator. When
using functions belonging to this class, the command begins with “$ns”, since ns was defined to
be a handle to the Simulator object.
Nodes
New node objects can be created with the command:
The member function of the Simulator class, called “node” creates four nodes and assigns
them to the handles n0, n1, n2 and n3. These handles can later be used when referring to the
nodes. If the node is not a router but an end system, traffic agents (TCP, UDP etc.) and traffic
sources (FTP,CBR etc.) must be set up, i.e, sources need to be attached to the agents and the
agents to the nodes, respectively.
The most common agents used in ns2 are UDP and TCP agents. In case of a TCP agent,
several types are available. The most common agent types are:
The most common applications and traffic sources provided by ns2 are:
send(int nbytes)
can be used at the tcl-level provided that the udp-agent is first configured and attached to
some node.
Below is a complete example of how to create a CBR traffic source using UDP as transport
protocol and attach it to node n0:
An FTP application using TCP as a transport protocol can be created and attached to node n1
in much the same way:
The UDP and TCP classes are both child-classes of the class Agent. With the expressions
[new Agent/TCP] and [new Agent/UDP] the properties of these classes can be combined to the
new objects udp0 and tcp1. These objects are then attached to nodes n0 and n1. Next, the
application is defined and attached to the transport protocol. Finally, the configuration
parameters of the traffic source are set. In case of CBR, the traffic can be defined by parameters
rate_ (or equivalently interval_, determining the interarrival time of the packets), packetSize_
and random_ . With the random_ parameter it is possible to add some randomness in the
interarrival times of the packets. The default value is 0, meaning that no randomness is added.
Traffic Sinks
If the information flows are to be terminated without processing, the udp and tcp sources
have to be connected with traffic sinks. A TCP sink is defined in the class Agent/TCPSink and
an UDP sink is defined in the class Agent/Null.
A UDP sink can be attached to n2 and connected with udp0 in the following way:
A standard TCP sink that creates one acknowledgement per a received packet can be attached
to n3 and connected with tcp1 with the commands:
There is also a shorter way to define connections between a source and the destination with
the command:
For example, to create a standard TCP connection between n1 and n3 with a class ID of 1:
One can very easily create several tcp-connections by using this command inside a for-loop.
Links
Links are required to complete the topology. In ns2, the output queue of a node is
implemented as part of the link, so when creating links the user also has to define the queue-type.
Figure 2 shows the construction of a simplex link in ns2. If a duplex-link is created, two
simplex links will be created, one for each direction. In the link, packet is first enqueued at the
queue. After this, it is either dropped, passed to the Null Agent and freed there, or dequeued and
passed to the Delay object which simulates the link delay. Finally, the TTL (time to live) value is
calculated and updated.
Links can be created with the following command:
For example, to create a duplex-link with DropTail queue management between n0 and n2:
The values for bandwidth can be given as a pure number or by using qualifiers k (kilo), M
(mega), b (bit) and B (byte). The delay can also be expressed in the same manner, by using m
(milli) and u (mikro) as qualifiers. There are several queue management algorithms implemented
in ns2, but in this exercise only DropTail and RED will be needed.
Traces
All events from the simulation can be recorded to a file with the following commands:
$ns flush-trace
close $trace_all
First, the output file is opened and a handle is attached to it. Then the events are recorded to
the file specified by the handle. Finally, at the end of the simulation the trace buffer has to be
flushed and the file has to be closed. This is usually done with a separate finish procedure. If
links are created after these commands, additional objects for tracing (EnqT, DeqT, DrpT and
RecvT) will be inserted into them.
These new objects will then write to a trace file whenever they receive a packet. The format
of the trace file is following:
+ : enqueue
- : dequeue
d : drop
r : receive
The fields in the trace file are: type of the event, simulation time when the event occurred,
source and destination nodes, packet type (protocol, action or traffic source), packet size, flags,
flow id, source and destination addresses, sequence number and packet id. In addition to tracing
all events of the simulation, it is also possible to create a trace object between a particular source
and a destination with the command:
Tracing all events from a simulation to a specific file and then calculating the desired
quantities from this file for instance by using perl or awk and Matlab is an easy way and suitable
when the topology is relatively simple and the number of sources is limited. However, with
complex topologies and many sources this way of collecting data can become too slow. The trace
files will also consume a significant amount of disk space.
Monitors
With a queue monitor it is possible to track the statistics of arrivals, departures and drops in
either bytes or packets. Optionally the queue monitor can also keep an integral of the queue size
over time.
For instance, if there is a link between nodes n0 and n1, the queue monitor can be set up as
follows:
The packet arrivals and byte drops can be tracked with the commands:
Besides assigning a value to a variable the set command can also be used to get the value of a
variable. For example here the set command is used to get the value of the variable “parrivals”
defined in the queue monitor class.
A flow monitor is similar to the queue monitor but it keeps track of the statistics for a flow
rather than for aggregated traffic. A classifier first determines which flow the packet belongs to
and then passes the packet to the flow monitor.
The flowmonitor can be created and attached to a particular link with the commands:
Notice that since these commands are related to the creation of the flow-monitor, the
commands are defined in the Simulator class, not in the Flowmonitor class. The variables and
commands in the Flowmonitor class can be used after the monitor is created and attached to a
link. For instance, to dump the contents of the flowmonitor (all flows):
$fmon dump
If you want to track the statistics for a particular flow, a classifier must be defined so that it
selects the flow based on its flow id, which could be for instance 1:
$ns run
The first command schedules the procedure finish at the end of the simulation, and the
second command actually starts the simulation. The finish procedure has to be defined to flush
the trace buffer, close the trace files and terminate the program with the exit routine. It can
optionally start NAM (a graphical network animator), post process information and plot this
information.
The finish procedure has to contain at least the following elements:
proc finish {} {
global ns trace_all
$ns flush-trace
close $trace_all
exit 0
Other events, such as the starting or stopping times of the clients can be scheduled in the
following way:
If you have defined your own procedures, you can also schedule the procedure to start for
example every 5 seconds in the following way:
proc example {} {
global ns
set interval 5
….
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.
Objective: Simulate a three node point to point network with duplex links between them. Set
queue size and vary the bandwidth and find number of packets dropped.
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#==========================================
# These below line helps to define color, label and shape
#==========================================
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 100.0Mb 20ms DropTail
$ns queue-limit $n0 $n2 20 #Q1
$ns duplex-link $n1 $n2 100.0Mb 20ms DropTail
$ns queue-limit $n1 $n2 20 #Q2
$ns duplex-link $n2 $n3 100.0Mb 20ms DropTail
$ns queue-limit $n2 $n3 20 #Q3
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n3 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 8.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam lab1.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
Awk Script:
BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="3"&&$5=="tcp"&&$6=="1540")
{
tcppack++;
}
if($1=="d"&&$3=="2"&&$5=="tcp"&&$6=="1540")
{
tcppack1++;
}
}
END{
printf("\n total number of data packets received at Node 3: %d\n", tcppack++);
printf("\n total number of packets dropped at Node 2: %d\n", tcppack1++);
}
Outcome:
Execution Steps:
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.
Objective: Simulate a four node point to point network with the links connected as follows: n0
– n2, n1 – n2 and n2 – n3. Apply TCP agent between n0 – n3 and UDP agent between n1 – n3.
Apply relevant applications over TCP and UDP agents changing the parameter and determine the
number of packets sent by TCP / UDP
#===================================
# Simulation parameters setup
#===================================
set val(stop) 50.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#========================================================
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 200.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n2 $n3 200.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 50
$ns duplex-link $n1 $n2 200.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp0 $sink3
$tcp0 set packetSize_ 1500 # Change the Packet Size
$tcp0 set interval_ 0.1
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 9.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam lab2.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
Awk Script:
BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="2"&&$5=="tcp"&&$6=="1540") #1540, 1040, 1340
{
tcppack++;
}
if($1=="r"&&$4=="2"&&$5=="cbr"&&$6=="1500") #1500, 1500, 1500
{
tcppack1++;
}
}
END{
printf("\n total number of TCP data packets sent between Node 0 and Node 2:
%d\n", tcppack++);
printf("\n total number of UDP data packets sent between Node 1 and Node 2:
%d\n", tcppack1++);
}
Outcome:
Execution Steps:
Objective: Simulate an Ethernet LAN using N (6-10) nodes, change error rate and data rate
and compare throughput
$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
# set error rate. Here ErrorModel is a class and it is single word and space should
not be given between Error and Model
# lossmodel is a command and it is single word. Space should not be given
between loss and model
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab3.nam &
exit 0
}
$ns run
Awk Script:
BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="r"&&$4=="7"&&$5=="cbr"&&$6=="1000")
{
tcppack++;
}
}
END{
printf("\n total number of data packets at Node 7: %d\n", tcppack++);
}
Outcome:
Execution Steps:
4. Implement Ethernet LAN using N nodes and assign multiple traffic to the
nodes and obtain congestion window for different sources/ destinations.
Objective: Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and plot
congestion window for different source / destination
$ns run
40
30
CGWD
20 TCP
TCPReno
10
0
0.5 1 1.5 2 2.5 3
Simulation Time (Sec.)
.
Outcome:
Time TCP TCPReno
0.5 23.02 21.91
1 29.73 29.53
1.5 34.59 34.71
2 2 41.11
2.5 26.23 2
3 32.84 24.62
Table 1: Above values are taken from file1.tr and file2.tr
5. Implement ESS with transmission nodes in Wireless LAN and obtain then
Performance Parameters.
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
BEGIN{
tcppack=0
tcppack1=0
}
{
if($1=="s"&&$3=="_0_"&&$4=="AGT"&&$8=="1040")
{
tcppack++;
}
if($1=="r"&&$3=="_2_"&&$4=="AGT"&&$8=="1040")
{
tcppack1++;
}
}
END{
printf("\n total number of data packets sent from Node 0: %d\n",
tcppack++);
printf("\n total number of data packets received at Node 2: %d\n",
tcppack++);
Execution Steps:
Objective: Simulate simple ESS and with transmitting nodes in wireless LAN by simulation and
determine the performance with respect to transmission of packets.
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n1 100.0Mb 10ms DropTail
#===================================
# Agents Definition
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
$ns connect $udp0 $null1
$udp0 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 1.0Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 5.0 "$cbr0 stop"
$ns rtproto LS
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam lab6.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
Outcome:
Total Number of Routing Paths: 4
1. N0-N1-N2-N3-N4: Total Cost is of 7
2. N0-N2- N1 -N3-N4 : Total Cost is of 8
3. N0-N2-N3-N4 : Total Cost is of 4
4. N0-N3-N4 : Total Cost is of 5
Objective: To write a program for distance vector algorithm to find suitable path for transmission
#include<stdio.h>
#include<stdlib.h>
#define nul 1000
#define nodes 10
int no;
struct node
{
int a[nodes][3];
}
router[nodes];
void init(int r)
{
int i;
for(i=1;i<=no;i++)
{
router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
}
router[r].a[r][2]=0;
router[r].a[r][3]=r;
}
void inp(int r)
{
int i;
printf("\n enter dist to the node %d to other nodes",r);
printf("\n please enter 999 if there is no direct route\n");
for(i=1;i<=no;i++)
{
if(i!=r)
{
printf("\n enter dist to the node %d:",i);
scanf("%d",&router[r].a[i][2]);
router[r].a[i][3]=i;
}
}
}
void display(int r)
{
int i;
printf("\n\n the routimg table for node %d is as follows",r);
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]>=999)
printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
else
printf("\n\t\t\t %d \t %d \t\t %d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
}
}
void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
{
for(j=1;j<=no;j++)
{
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}
void find(int x,int y)
{
if(router[x].a[y][3]!=y)
{
find(x,router[x].a[y][3]);
printf("%d-->",router[x].a[y][3]);
find(router[x].a[y][3],y);
return;
}
}
int main()
{
int i,j,x,y;
int choice;
printf("enter the no of nodes required(less than 10 pls):");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("\n the configuration of the nodes after initilization is as follows:");
for(i=i;i<=no;i++)
display(i);
for(j=1;j<=no;j++)
for(i=1;i<=no;i++)
dv_algo(i);
printf("\n the configuration of the nodes after computation of path is as follows:");
for(i=1;i<=no;i++)
display(i);
while(1)
{
printf("\n\n enter 1 to continue 0 to quit:");
scanf("\%d",&choice);
if(choice!=1)
break;
printf("\n enter the nodes btn which shortest path is to be found:\n");
scanf("%d%d",&x,&y);
printf("\n the shortest path is:");
printf("%d-->",x);
find(x,y);
printf("%d",y);
printf("\n the length of the shortest path is%d",router[x].a[y][2]);
}
return 0;
}
/* output
enter the no of nodes required(less than 10 pls):6
2 1 2
3 0 3
4 3 2
5 2 2
6 3 6
Outcome:
enter the nodes btn which shortest path is to be found:
1
6
Objective: To Write a C program for error detecting code using CRC-CCITT (16-bits)
#include<stdio.h>
int a[100],b[100],i,j,len,k,count=0;
//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};
int main()
{
void div();
printf("\nEnter the length of Data Frame :");
scanf("%d",&len);
printf("\nEnter the Message :");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
div();
for(i=0;i<len;i++)
if(a[i]!=0)
{
printf("\nERROR in Recived Data");
return 0;
}
printf("\nData Recived is ERROR FREE");
}
void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
}
count=0;
}
}
Outcome:
*****************************************************************
Objective: Using TCP/IP sockets, write a client-server program to make client sending the file
name and the server to send back the contents of the requested file if present.
#include<string.h>
#include<stdio.h>
main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
getch();
Outcome:
#include<string.h>
#include<stdio.h>
main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
Outcome
Objective: Write a program for congestion control using Leaky bucket algorithm.
#include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do
{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}
while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
for(i=0;count || i<nsec;i++)
{
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
count=cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
}
else
{
drop=0;
count=0;
}
printf(" \t %d \t %d \n",drop,count);
}
return 0;
}
Outcome:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int i,j,noframes,x,x1=10,x2;
// clrscr();
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
i=1;
j=1;
noframes=noframes/4;
// scanf("%d",&noframes);
getch();
while(noframes>0)
srand(x1++);
x=rand()%15;
if(x%5==0)
for(x2=1;x2<2;x2++)
sleep(x2);
srand(x1++);
x=rand()%10;
noframes=noframes-1;
i++;
j++;
Outcome:
#include<stdio.h>
int main()
int w,i,f,frames[50];
scanf("%d",&w);
scanf("%d",&f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith 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 acknowledgement sent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
if(i%w==0)
printf("%d\n",frames[i]);
else
printf("%d ",frames[i]);
if(f%w!=0)
return 0;
Outcome