Introduction To Computer Networks
Introduction To Computer Networks
Introduction To Computer Networks
NS-2is an event driven simulation tool, which is 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.
Overview of NS2 Architecture
Trace file is a text file that contains events logged during simulation (e.g., Packet drop,
packet reception etc.).
Trace file Analysis
When tracing into an output ascii file, the trace is organized into 12 fields as follows
1 2 3 4 5 6 7 8 9 10 11 12
From To Pkt Pkt Pkt
Event Time Flags Fid Srcaddr Dstaddr Seqnum
node node type size id
Ex: r 40.639943289 _1_ AGT--- 1569 tcp 1032 [a2 1 2 800] ------- [0:0 1:0 32 1] [35 0] 2 0
The meanings of the fields are:
1. The first field is the event type. It is given by one of five possible symbols r,+,−, d, h, which
correspond respectively to receive (at the output of the link), enqueued, dequeued, dropped
and hop.
2. The second field gives the time at which the event occurs.
3. The third field gives the input node of the link at which the event occurs.
4. The fourth field gives the output node of the link at which the event occurs.
5. The fifth field gives the packet type (for example, CBR, or TCP). The type corresponds to
the name that we gave to those applications.
6. The sixth field gives the packet size.
7. Some flags follow
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-2 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 file-name.xg
Trace file Analysis for wireless network
1 2 3 4 5 6 7 8 9 10 11 12
IP
Global MAC Src.
Node Pkt. Pkt. TCP
Event Time MAC ---- Seq. Layer ---- &
Number Type Size Info
No. Info. Dst.
addr.
Ex:r 40.639943289 _1_ AGT--- 1569 tcp 1032 [a2 1 2 800] ------- [0:0 1:0 32 1] [35 0] 2 0
The first field is a letter that can have the values r,s,f,D for “received", “sent", “forwarded"
and “dropped", respectively. It can also be M for giving a location or a movement indication,
this is described later.
The second field is the time.
The third field is the node number.
The fourth field is MAC to indicate if the packet refers to a MAC layer;it is AGT to indicate
the transport layer (e.g., tcp) packet, or RTR if it refers to the routed packet. It can also be
IFQ to indicate events related to the interference priority queue (like drop of packets).
After the dashes, the global sequence number of the packet (this is not the tcp sequence
number).
At the next field comes more information on the packet type (e.g., tcp, ack or udp).
Then the packet size in bytes.
Mac layer information.
o The first hexadecimal number, a2 (which equals 162 in decimal) specifies the expected
time in seconds to send this data packet over the wireless channel. The second number,
1, stands for the MAC-id of the sending node, and the third, 2, is that of the receiving
node. The fourth number, 800, specifies that the MAC type is ETHERTYPE_IP.
o The next numbers in the second square brackets refer to the IP source and destination
addresses, then the ttl (Time To Live) of the packet.
o The third brackets concern the tcp information: its sequence number and the
acknowledgement number.
Where the first number is the time, the second is the node number, then comes the origin and
destination locations, and finally the speed is given.
Network Animator [NAM]is an animation tool for viewing network simulation traces.
Visualisation Using NAM
1. Orientation
3. Shape of Node
o $n1 shape box (or instead of “box" one can use “hexagon" or “circle")
Keywords
set : to assign a value to a variable
unset : to remove the value assigned for variable
proc : to define a procedure
$ : used with the variable name
# : comment
Basic Unix commands
ls List your files
cd Change directory
mv filename1 filename2 Moves a file
cp filename1 filename2 Copies a file
chmod Change the mode
geditfilename.ext Create or edit the file
grep Find the particular data
awk Performs mathematical operations
gccfilename.ext Compile C/C++ program
./a.out Run the C Program
PART-A
PART-A:
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.
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.
4. Implement Ethernet LAN using n nodes and assign multiple traffic to the
nodes and obtain congestion window for different sources/ destinations.
5. Implement ESS with transmission nodes in Wireless LAN and obtain the
performance parameters.
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.
Description:
A duplex data transmission means that data can be transmitted in both directions on a signal
carrier at the same time.
Network performance refers to measures of service quality of a network.
There are many different ways to measure the performance of a network, they are
o Bandwidth commonly measured in bits/second is the maximum rate that information can be
transferred
o Throughput is the actual rate that information is transferred
o Latency the delay between the sender and the receiver decoding it, this is mainly a function of the
signals travel time, and processing time at any nodes the information traverses
o Jitter variation in packet delay at the receiver of the information
o Error rate the number of corrupted bits expressed as a percentage or fraction of the total sent
CBR stands for constant bitrate, and is an encoding method that keeps the bitrate the same.
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
Packet size
Single packet transmission time =
Bandwidth
Write the value of network performance in observation sheet. Repeat the above step by
changing the bandwidth to [0.3Mb, 0.4Mb, 0.5Mb, 0.7Mb]
Sl.No. Bandwidth Network Performance
1. 0.3
2. 0.4
3. 0.5
4. 0.7
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.
Description:
The TCP and UDP protocols are two different protocols that handle data communications
between terminals in an IP network (the Internet).
Transmission Control Protocol(TCP) is one of the main protocols in TCP/IP networks.
Whereas the IP protocoldeals only with packets, TCP enables two hosts to establish a
connection and exchange streams of data. TCP guarantees delivery of data and also
guarantees that packets will be delivered in the same order in which they were sent.
User Datagram Protocol (UDP) is part of the Internet Protocol suite used by programs
running on different computers on a network. UDP is used to send short messages called
datagrams but overall, it is an unreliable, connectionless protocol.
File Transfer Protocol (FTP) is the commonly used protocol for exchanging files over
the Internet. FTP uses the Internet's TCP/IPprotocols to enable data transfer. FTP
promotes sharing of files via remote computers with reliable and efficient data transfer
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
Description:
Ethernet is the name of the most commonly used LAN today.
A LAN (Local Area Network) is a network of computers that covers a small area like a
room, an office, a building or a campus.
Ethernet is a network protocol that controls how data is transmitted over a LAN.
Technically it is referred to as the IEEE 802.3 protocol.
The protocol has evolved and improved over time and can now deliver at the speed of a
gigabit per second.
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
#Create a LAN
$ns make-lan "$n0 $n1 $n3 $n2" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n5 $n6 $n4" 10Mb 10ms LL Queue/DropTail Mac/802_3
Calculation of Throughput
Packet size
Single packet transmission time =
Bandwidth
Write the value of throughput in observation sheet. Repeat the above step by changing the
error rate to the following line of the program
$err set rate_ 0.7 #vary error rate 0.1, 0.4, 0.5 and 0.7
Sl. Error rate Throughput
No.
1. 0.1
2. 0.4
3. 0.5
4. 0.7
4. Implement Ethernet LAN using n nodes and assign multiple traffic to the
nodes and obtain congestion window for different sources/ destinations.
Description:
Congestion means Blockage of traffic (or) Heavy traffic (or) Crowded traffic.
During “Multiple Traffic Transmission”, the congestion of traffic is occurred. Hence the
throughput is decreased substantially.
Here Congestion window determines the number of bytes outstanding at a time. Its size
varies according to the network state.
TCP follows the following steps to avoid the Congestion:
o Initially the window size increases rapidly
o When it reaches a large value, it grows slowly.
o When congestion is detected, the window size decreases drastically.
o This mechanism allows resolving congestion rapidly and yet using efficiently the
network’s bandwidth.
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
$ns trace-all $f
#Create 6 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]
#Create a LAN
$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 " 10Mb 30ms LL Queue/DropTail Mac/802_3#vary
bandwidth 1, 10
5. Implement ESS with transmission nodes in Wireless LAN and obtain the
performance parameters.
Description:
Wireless LAN:It is a type of network where the data is not transmitted via cables butover the
air through the use of wireless transmitters and receivers. Most modern WLANs are based on
IEEE 802.11 standards.
Important Parameters related to wireless network:
o Stations [STA]: All components that can connect into a wireless medium in a network arereferred to
as stations.
o Basic service set [BSS]: The basic service set (BSS) is a set of all stations that cancommunicate with
each other at PHY layer.
o Extended service set [ESS]: An extended service set (ESS) is a set of connectedBSSs.
List of basic parameters which need to be assigned (configured) for wireless node
o Channel type- Wireless Channel
o Radio Propagation type- TwoRayGround
o Routing Protocol- DSDV
o Network Interface type- WirelessPhy
o
(Physical Layer Type)
o Link layer type- LL Mac Type- 802_11
o Antenna Type- Omni Antenna
o Interface Queue Type- Queue/DropTail/PriQueue
o Queue Length- 50
o Number of mobile nodes- 2
o agentTrace ON
o routerTrace ON
In wired nodes, connectivity is determined by wires (defined by duplex-link, simplex-
link). But, the connectivity of wireless nodes is determined by their distance from one
another.
Because wireless nodes can MOVE, their distances from one another change over time
To keep track of their positions in the topology grid, we need to create a General
Operations Director (GOD) object.
The God object stores:
o a table of shortest number of hops required to reach from one node to another.
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
#Node Configuration
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue\
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-ifqLen 50 \
-phyTypePhy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON
#Create God object
create-god 3
#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$ns run
Steps for execution:
Open gediteditor & type program. Program name should have the extension “ .tcl”
[root@localhost ~]# gedit lab5.tcl
Save the program and quit.
Run the simulation program
[root@localhost~]# ns lab5.tcl
Description:
A router is a networking device that forwards data packets between computer networks.
When a data packet comes in on one of the lines, the router reads the network address
information in the packet to determine the ultimate destination.
Routing is the process of selecting paths in a network along which to send network traffic.
Goals of routing are correctness, simplicity, robustness, stability, fairness & optimality.
Routing is performed for many kinds of network, including the telephone network,
electronic data network & transportation networks.
Routing algorithms can be classified based on the following:
o Static or Dynamic routing
o Distributed or Centralized
o Single path or Multi-path
o Flat or Hierarchical
o Intra Domain or Inter Domain
o Link State or Distance vector
The basic concept of link-state routing is that every node constructs a map of the connectivity
to the network, in the form of a graph, showing which nodes are connected to which other
nodes.
Each node then independently calculates the next best logical path from it to every possible
destination in the network. Each collection of best paths will then form each node's routing
table.
Design:
Source code:
#Create a simulator object
set ns [new Simulator]
PART-B
PART-B:
2. Write a program for distance vector algorithm to find suitable path for
transmission.
3. Implement Dijkstra’s algorithm to compute the shortest routing path.
4. For the given data, use CRC-CCITT polynomial to obtain CRC code.
Verify the program for the cases
i) Without error
ii) With error
5. Implementation of Stop and Wait Protocol and Sliding Window
Protocol
6. Write a program for congestion control using leaky bucket algorithm.
Description:
The new technique allows data frames to contain an arbitrary number if bits and allows
character codes with an arbitrary no of bits per character.
Each frame begins and ends with special bit pattern, 01111110, called a flag byte.
Whenever the sender’s data link layer encounters five consecutive one’s in the data, it
automatically stuffs a 0 bit into the outgoing bit stream.
This bit stuffing is analogous to character stuffing, in which a DLE is stuffed into the
outgoing character stream before DLE in the data
Source code:
#include<stdio.h>
#include<string.h>
main()
{
if(src[si] == '1')
one_count++;
else
one_count = 0;
dest[di++] = src[si++];
if(one_count ==5)
{
dest[di++] = '0';
one_count = 0;
//================================
// Destuffing
//=================================
j=0;
int destLength = strlen(dest);//-16;
one_count=0
if(dest[i] == '0')
{
one_count=0;
temp[j++] = dest[i];
else
{
if(dest[i] == '1' && one_count!=5)
{
one_count++;
temp[j++] = dest[i];
}
if(one_count==5)
{
i++;
one_count=0;
printf("\n 3 Value of i and one_count are %d and %d \n", i, one_count);
}
}
} // for ends
// Stuff null
temp[j] = '\0';
temp_len = strlen(temp);
printf("\n Destuffed frame is: \n %s \n\n", temp);
printf("\n Size of Destuffed frame is %d \n", temp_len);
Execution:
Description:
The framing method gets around the problem of resynchronization after an error by having
eachframe start with the ASCII character sequence DLE STX and the sequence DLE ETX.
If the destinationever losses the track of the frame boundaries all it has to do is look for DLE
STX or DLE ETXcharacters to figure out.
The data link layer on the receiving end removes the DLE before the data aregiven to the
network layer.
This technique is called character stuffing
Source code:
#include<stdio.h>
#include<string.h>
main()
{
int si=0, di=0, i, ptr=0;
strcpy(dest,begin);
di = strlen(begin);
while(src[si] != '\0')
{
}
else
dest[di++] = src[si++];
dest[di] = '\0';
strcat(dest,end);
printf("\n Character stuffed frame is: %s \n\n", dest);
strcpy(de, dest);
printf("\n de's contents are %s \n", de);
for(i=7;i< strlen(dest)-7;i++)
{
if(dest[i+0] == 'D' && dest[i+1] == 'L' && dest[i+2] == 'E')
{
destuffed_frame[ptr++] = de[i];
//printf("\n Destuffed frame (original frame) is: %c \n", de[i]);
printf("\n\n");
EXECUTION :
2. Write a program for distance vector algorithm to find suitable path for
transmission.
Description:
Distance vector routing algorithms operate by having each router maintain a table (i.e.,
vector) giving the best known distance to each destination and which line to get there.
These tables are updated by exchanging information with the neighbors.
Source code:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n, i, j, k, count=0;
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<n;i++)
{
Execution:
[root@localhost code]# gcc 2.c
[root@localhost code]# ./a.out
Enter the number of nodes : 4
0 2 999 1
2052
999 5 0 6
1260
Description:
Dijkstra algorithm is also called single source shortest path algorithm.
The algorithm maintains a list visited [ ] of vertices, whose shortest distance from the source
is calculated.
Source code:
#include<stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
Execution:
0 10 0 30 100
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
100 0 10 60 0
Distance of node1=10
Path=1<-0
Distance of node2=50
Path=2<-3<-0
Distance of node3=30
Path=3<-0
Distance of node4=60
Path=4<-2<-3<-0
4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the
program for the cases
a. Without error
b. With error
Description:
CyclicRedundancy Check is error detection method.
It is used to find that the transmitted data contains error (or) not.
This is done by polynomial generation method.
Source code:
#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[28],cs[28],g[]="1101";
int data_length,modified_length,i,e_d;
void xor()
{
for(i=1;i<N;i++)
cs[i]=((cs[i]==g[i])?'0':'1');
}
void crc()
{
for(modified_length=0;modified_length<N;modified_length++);
cs[modified_length]=t[modified_length];
do
{
if(cs[0]=='1')
xor();
for(i=0;i<N-1;i++)
cs[i]=cs[i+1];
cs[i]=t[modified_length++];
}
while(modified_length<=data_length+N-1);
}
int main()
{
printf("\n Enter data: ");
scanf("%s",t);
printf("\n-----------------------------------------");
printf("\n generating polynomial:%s",g);
data_length=strlen(t);
for(modified_length=data_length;modified_length<data_length+N-1;modified_length++)
t[modified_length]='0';
printf("\n-----------------------------------------");
t[modified_length]=cs[modified_length-data_length];
printf("\n-----------------------------------------");
printf("final codeword is:%s",t);
printf("\n------------------------------------------");
printf("\n Test error detection 0(yes) 1(no)?: ");
scanf("%d",&e_d);
if(e_d==0)
{
do
{
printf("\n enter the position where error is to be inserted: ");
scanf("%d",&e_d);
}
while
(e_d==0 || e_d>data_length+N-1);
t[e_d-1]=(t[e_d-1]=='0')?'1':'0';
printf("\n-----------------------------------------");
printf("\n Erroneous data:%s \n",t);
}
crc();
for(e_d=0;(e_d<N-1)&&(cs[e_d]!='1');e_d++);
if(e_d<N-1)
printf("\n error detected \n\n");
else
printf("/ No error is deteced \n\n");
printf("\n-------------------------------\n");
return 0;
}
Execution:
Checksum is : 1000101101011000
Checksum is : 1000101101011000
Source code:
# include <stdio.h>
# include <stdlib.h>
void main()
{
int i,j,noframes,x,x1=10,x2;
//for(i=0;i< 5 ;i++)
// rand();
i=1;
j=1;
noframes= 8;
x=rand()%10;
printf("\n value of x with srand %d ",x);
if(x%2==0)
{
for(x2=1;x2<2;x2++)
{
printf("\n waiting for %d seconds",x2);
printf("\n Either frame or ACK is lost ");
sleep(x2);
}
printf("\n So, resending frame %d",i);
srand(x1++);
x=rand()%10;
}
j++;
printf("\n ---------------------------\n");
}// While ends
printf("\n end of stop and wait protocol\n\n");
Execution:
no of frames is 8
---------------------------
sending frame 1
value of x with srand 5
Received ACK for frame 1
---------------------------
sending frame 2
value of x with srand 3
Received ACK for frame 2
---------------------------
sending frame 3
value of x with srand 0
waiting for 1 seconds
Either frame or ACK is lost
So, resending frame 3
Received ACK for frame 3
---------------------------
sending frame 4
value of x with srand 3
Received ACK for frame 4
---------------------------
sending frame 5
value of x with srand 3
Received ACK for frame 5
---------------------------
sending frame 6
value of x with srand 4
waiting for 1 seconds
---------------------------
sending frame 7
value of x with srand 8
waiting for 1 seconds
Either frame or ACK is lost
So, resending frame 7
Received ACK for frame 7
---------------------------
sending frame 8
value of x with srand 1
Received ACK for frame 8
---------------------------
Source code:
#include<stdio.h>
int main()
{
int w, i, f, frames[50];
printf("Enter window size: ");
scanf("%d", &w);
printf("\n With 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 acknowledgementsent 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;
Execution:
Enter 5 frames: 12 20 87 65 4
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 20 87
Acknowledgement of above frames sent is received by sender
65 4
Acknowledgement of above frames sent is received by sender
Description:
In Leaky bucket, each host is connected to the network by an interface containing a leaky
bucket that is a finite internal queue.
Ifa packet arrives at the queue when it is full, the packet is discarded.
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
intbucket_size;
if(pkt_sz>bucket_size )
else
sleep(1);
while ( pkt_sz>op_rt )
pkt_sz-= op_rt;
sleep(1);
if ( pkt_sz> 0 )
int main()
sleep(1);
packet_size = random()%1000;
return 0;
Execution:
Bucket overflow
Bucket overflow
Bucket overflow
Bucket overflow