CN&WP Lab Manual
CN&WP Lab Manual
Bit stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5
consecutive 1's.
Source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();
Byte stuffing –
A byte (usually escape character(ESC)), which has a predefined bit pattern is added to the
data section of the frame when there is a character with the same pattern as the flag.
Whenever the receiver encounters the ESC character, it removes from the data section and
treats the next character as data, not a flag.
But the problem arises when the text contains one or more escape characters followed by a
flag. To solve this problem, the escape characters that are part of the text are marked by
another escape character i.e., if the escape character is part of the text, an extra one is added
to show that the second one is part of the text.
Example:
Source code:
#include<stdio.h>
#include<string.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;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
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("\n After stuffing:%s", fs);
getch();
}
Output:-
Enter characters to be stuffed: goodday
Enter a character that represents starting delimiter: d
Enter a character that represents ending delimiter: g
After stuffing: dggooddddayg.
a. The communicating parties agrees upon the size of message,M(x) and the
generator polynomial, G(x).
b. If r is the order of G(x),r, bits are appended to the low order end of M(x). This
makes the block size bits, the value of which is xrM(x).
c. The block xrM(x) is divided by G(x) using modulo 2 division.
d. The remainder after division is added to xrM(x) using modulo 2 addition. The
result is the frame to be transmitted, T(x). The encoding procedure makes
exactly divisible by G(x).
e. The receiver divides the incoming data frame T(x) unit by G(x) using modulo
2 division. Mathematically, if E(x) is the error, then modulo 2 division of
[M(x) + E(x)] by G(x) is done.
f. If there is no remainder, then it implies that E(x). The data frame is accepted.
g. A remainder indicates a non-zero value of E(x), or in other words presence of
an error. So the data frame is rejected. The receiver may then send an
erroneous acknowledgment back to the sender for retransmission.
Source code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for(i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for(i=0;i<keylen;i++)
temp[i]=input[i];
for(i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for(j=0;j<keylen;j++)
key[j]='0';
else
for(j=0;j<keylen;j++)
key[j]=key1[j];
for(j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for(i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for(i=0;i<msglen;i++)
printf("%c",input[i]);
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
In computer networks sliding window protocol is a method to transmit data on a network. Sliding
window protocol is applied on the Data Link Layer of OSI model. At data link layer data is in the
form of frames. In Networking, Window simply means a buffer which has data frames that needs
to be transmitted.
Both sender and receiver agrees on some window size. If window size=w then after sending w
frames sender waits for the acknowledgement (ack) of the first frame.
As soon as sender receives the acknowledgement of a frame it is replaced by the next frames to
be transmitted by the sender. If receiver sends a collective or cumulative acknowledgement to
sender then it understands that more than one frames are properly received, for eg:- if ack of
frame 3 is received it understands that frame 1 and frame 2 are received properly.
Image Source
In sliding window protocol the receiver has to have some memory to compensate any loss in
transmission or if the frames are received unordered.
η = (W*tx)/(tx+2tp)
W = Window Size
tx = Transmission time
tp = Propagation delay
It is of two types:-
1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.
2. Go back n: Sender transmits all frames present in the window that occurs after the error bit
including error bit also.
Source code:
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
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]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Output
Enter 5 frames: 12 5 89 4 6
With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 5 89
Acknowledgement of above frames sent is received by sender
4 6
Acknowledgement of above frames sent is received by sender
Source code:
#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);
if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
output:-
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Given a graph and a source vertex in the graph, find shortest paths from source to all
vertices in the given graph.
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree.
Like Prim’s MST, we generate a SPT (shortest path tree) with given source as root.
We maintain two sets, one set contains vertices included in shortest path tree, other set
includes vertices not yet included in shortest path tree. At every step of the algorithm,
we find a vertex which is in the other set (set of not yet included) and has a minimum
distance from the source.
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from
a single source vertex to all other vertices in the given graph.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in
shortest path tree, i.e., whose minimum distance from source is calculated and
finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance
values as INFINITE. Assign distance value as 0 for the source vertex so that it is
picked first.
3)WhilesptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance
values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of
distance value of u (from source) and weight of edge u-v, is less than the distance
value of v, then update the distance value of v.
Source code:
#include"stdio.h"
#include"conio.h"
#define infinity 999
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("n Enter the number of nodes:");
scanf("%d",&n);
printf("n Enter the cost matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("n Shortest path:n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%dn",v,i,dist[i]);
getch();
}
Source code:
#include<stdio.h>
int a[10][10],n;
main()
int i,j,root;
clrscr();
printf(“Enterno.of nodes:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf(“%d”,&a[i][j]);
scanf(“%d”,&root);
adj(root);
adj(int k)
int i,j;
printf(“%d\n\n”,k);
for(j=1;j<=n;j++)
if(a[k][j]==1 || a[j][k]==1)
printf(“%d\t”,j);
printf(“\n”);
for(i=1;i<=n;i++)
printf(“%d”,i);
OUTPUT
134
8. AIM: C Program / source code for the Distance Vector Routing Algorithm using
Bellman Ford's Algorithm
Distance Vector Routing in this program is implemented using Bellman Ford Algorithm:-
Source code:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct distance from
the node i to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}
9. AIM: C program to encrypt and decrypt the string using Caesar Cypher
Algorithm.
While encrypting the given string, 3 is added to the ASCII value of the characters. Similarly,
for decrypting the string, 3 is subtracted from the ASCII value of the characters to print an
original string.
Source code:
#include <stdio.h>
int main()
{
int i, x;
char str[100];
case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value
default:
printf("\nError\n");
}
return 0;
}
10. AIM: Write a program for congestion control using Leaky bucket algorithm.
In the network layer, before the network can make Quality of service guarantees, it must
know what traffic is being guaranteed. One of the main causes of congestion is that traffic
is often bursty.
To understand this concept first we have to know little about traffic shaping. Traffic
Shaping is a mechanism to control the amount and the rate of the traffic sent to the
network. Approach of congestion management is called Traffic shaping. Traffic shaping
helps to regulate rate of data transmission and reduces congestion.
1. Leaky Bucket
2. Token Bucket
Suppose we have a bucket in which we are pouring water in a random order but we have to
get water in a fixed rate, for this we will make a hole at the bottom of the bucket. It will
ensure that water coming out is in a some fixed rate, and also if bucket will full we will stop
pouring in it.
The input rate can vary, but the output rate remains constant. Similarly, in networking, a
technique called leaky bucket can smooth out bursty traffic. Bursty chunks are stored in the
bucket and sent out at an average rate.
In the figure, we assume that the network has committed a bandwidth of 3 Mbps for a host.
The use of the leaky bucket shapes the input traffic to make it conform to this commitment.
In Figure the host sends a burst of data at a rate of 12 Mbps for 2 s, for a total of 24 Mbits of
data. The host is silent for 5 s and then sends data at a rate of 2 Mbps for 3 s, for a total of 6
Mbits of data. In all, the host has sent 30 Mbits of data in 10 s. The leaky bucket smooths the
traffic by sending out data at a rate of 3 Mbps during the same 10 s.
Without the leaky bucket, the beginning burst may have hurt the network by consuming more
bandwidth than is set aside for this host. We can also see that the leaky bucket may prevent
congestion.
A simple leaky bucket algorithm can be implemented using FIFO queue. A FIFO queue holds
the packets. If the traffic consists of fixed-size packets (e.g., cells in ATM networks), the
process removes a fixed number of packets from the queue at each tick of the clock. If the
traffic consists of variable-length packets, the fixed output rate must be based on the number
of bytes or bits.
Packet=
Since n> front of Queue i.e. n>200
Therefore, n=1000-200=800
Packet size of 200 is sent to the network.
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
intrand(int a)
{
int rn = (random() % 10) % a;
return rn == 0 ? 1 : rn;
}
intmain()
{
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; ++i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
scanf("%d", &b_size);
for(i = 0; i<NOF_PACKETS; ++i)
{
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-
PACKET REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else
{
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rand(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0;
else
op = o_rate, p_sz_rm -= o_rate;
printf("\nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
}
}
}
}
}
11. AIM: How to write a C Program to Frame sorting technique used in buffers in C
Programming Language?
Source code:
#include<stdio.h>
#include<string.h>
#define FRAM_TXT_SIZ 3
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];
{ char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
{ fr[k].seq_no = k;
fr[k].text[j] = str[i++];
printf("%d:%s ",i,fr[i].text);
void generate(int *random_ary, const int limit) //generate array of random nos
{ int r, i=0, j;
{ r = random() % limit;
if( random_ary[j] == r )
break;
}}
generate(random_ary, no_frames);
shuf_ary[i] = fr[random_ary[i]];
printf("\n\nAFTER SHUFFLING:\n");
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
int i,j,flag=1;
for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence
flag=0;
hold = shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = hold;
flag=1;
int main()
int no_frames,i;
gets(str);
no_frames = assign_seq_no();
shuffle(no_frames);
sort(no_frames);
printf("\n\nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
printf("%s",shuf_ary[i].text);
printf("\n\n");
0:Wel 1:com 2:e T 3:o A 4:cha 5:rya 6: In 7:sti 8:tut 9:e o 10:f T 11:ech 12:nol 13:ogy
AFTER SHUFFLING:
1:com 4:cha 9:e o 5:rya 3:o A 10:f T 2:e T 6: In 11:ech 13:ogy 0:Wel 8:tut 12:nol 7:sti
AFTER SORTING
12.. Wireshark
Wireshark, a network analysis tool formerly known as Ethereal, captures packets in real time
and display them in human-readable format. Wireshark includes filters, color coding, and
other features that let you dig deep into network traffic and inspect individual packets.
i) Startingwireshark:
Two different methods for starting Wireshark are available. These include the Start menu and
the Run command box.
The following methods can be used to start capturing packets with Wireshark:
After some time interval we need to stop the capturing .(click stop from capture
menu).
Once you have captured some packets or you have opened a previously saved capture file,
you can view the packets that are displayed in the packet list pane (first pane) by simply
clicking on a packet in the packet list pane, which will bring up the selected packet in the
packet list details view and byte view panes.
You can then expand any part of the tree to view detailed information about each protocol in
each packet. Clicking on an item in the tree will highlight the corresponding bytes in the byte
view pane.
In addition you can view individual packets in a separate window. We can do this by double-
clicking on an item in the packet list or by selecting the packet in which you are interested in
the packet list pane and selecting View → Show Packet in New Window. This allows you to
easily compare two or more packets, even across multiple files.
iv) Statisticks :
When using Wireshark, we have various types of Statistic tools, starting from the simple tools
for listing end-nodes and conversations, to the more sophisticated tools such as flow and I/O
graphs. Here we are representing two of them.
To start statistics tools, start Wireshark, and choose Statistics from the main menu.
V) Filters:
Wireshark has two filtering languages: capture filters and display filters. Capture filters are
used for filtering when capturing packets.
You enter the capture filter into the “Filter” field of the Wireshark “Capture Options” dialog
box, from capture menu.
Display filters are used for filtering which packets are displayed .Display filters allow you to
concentrate on the packets you are interested in while hiding the currently uninteresting ones.
They allow you to only display packets based on:
Protocol
The presence of a field
The values of fields
A comparison between fields
… and a lot more!
To only display packets containing a particular protocol, type the protocol name in the
display filter toolbar of the Wireshark window and press enter to apply the filter.
For example if we select tcp protocol in Display filter, the following screen will be displayed.
To remove the filter, click on the Clear button to the right of the display filter field.
All packets will become visible again.
Nmap will scan a target System and report which ports are open and which are closed.
3)To run nmap scan,type nmap in command line window.(eg: nmap 172.16.1.34)
The above screen shot command is used for discovering all live hosts on our network.
a) Features of NS2
b) Basic Architecture
NS2 consists of two key languages: C++ and Object-oriented Tool Command Language
(OTcl). While the C++ defines the internal mechanism (i.e., a backend) of the simulation
objects, the OTcl sets up simulation by assembling and configuring the objects as well as
scheduling discrete events. The C++ and the OTcl are linked together using TclCL.
Network
animator file Ns-2 simulator Output (Dsdv.o)
(.nam)
C/C++
Network compiler
Animator Trace file (.tr)
Analyzer module
Topology (.java file)
graphs Routing protocol
source code (.cc, .h)
Graph drawing
software (e.g Excel)
LEGEND
Tools/utilities
Output Performance
Graphs
User Input
1)create tcpudp.tcl
#===================================
# 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 $n2 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n2 $n1 50
$ns duplex-link $n1 $n0 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n0 50
$ns duplex-link $n4 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n4 $n1 50
$ns duplex-link $n3 $n1 100.0Mb 10ms DropTail
$ns queue-limit $n3 $n1 50
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
#===================================
# 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 2.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam tcpudp.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
…………………………………………………………………………………………………
…………..
$ns tcpudp.tcl
proc finish { } {
global ns nf tf $ns flush-trace # clears trace file contents
close $nf close $tf exec nam PA1.nam & exit 0
}
set n0 [$ns node] # creates 3 nodes
set n2 [$ns node]
AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
Open vi editor and type program. Program name should have the extension “ .tcl ”
[root@localhost~]# ns lab1.tcl
Here “ns” indicates network simulator. We get the topology shown in the snapshot.
Now press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# vi lab1.tr
Output Topology
proc finish { }
{
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam & exit 0
}
AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
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);
}
[root@localhost~]# ns lab2.tcl
oHere “ns” indicates network simulator. We get the topology shown in the snapshot.
oNow press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# vi lab2.tr
Topology Output
v) Simulate to find number of packets dropped due to congestion
puts "node [$node_ id]received answer from $from with round trip time $rtt msec"
}
# please provide space between $node_ and id. No space between $ and from. No space
between and $ and rtt */
$ns connect $p1 $p5
$ns connect $p3 $p4
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab4.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}
3) Open vi editor and type awk program. Program name should have the extension “.awk ”
4) Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
[root@localhost~]# ns lab4.tcl
i) Here “ns” indicates network simulator. We get the topology shown in the snapshot.
ii) Now press the play button in the simulation window and the simulation will begins.
6) After simulation is completed run awk file to see the output ,
[root@localhost~]# vi lab4.tr
Topology
Output Screens
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/ DropTail Mac/802_3 #
should come in single line
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
cwnd:- means congestion window
BEGIN {
}
{
if($6= ="cwnd_") # don‟t leave space after writing cwnd_
printf("%f\t%f\t\n",$1,$7); # you must put \n in printf
}
END {
}
Open vi editor and type program. Program name should have the extension “ .tcl ”
[root@localhost~]# ns lab7.tcl
After simulation is completed run awk file to see the output ,
[root@localhost~]# vi lab7.tr
Topology
XGraph Output
-prrootype Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "tcp0"
$n1 label "sink1/tcp1"
$n2 label "sink2"
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab8.nam &
close $tf
exit 0
}
$ ns at 250 "finish"
$ns run
AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
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)));
}
Open vi editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# vi lab8.tcl
Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Open vi editor and type awk program. Program name should have the extension “.awk ”
[root@localhost ~]# vi lab8.awk
Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
Run the simulation program
[root@localhost~]# ns lab8.tcl
Here “ns” indicates network simulator. We get the topology shown in the snapshot.
oNow press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab8.awk lab8.tr
Output: