0% found this document useful (0 votes)
19 views

CN Lab Manual

cn lab manul

Uploaded by

daya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CN Lab Manual

cn lab manul

Uploaded by

daya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Sl.

Experiments
NO
1 Implement three nodes point – to – point network with duplex links between them. Set the queue size, vary the
bandwidth and find the number of packets dropped.[B1]
2
Implement the data link layer framing methods such as character, character-stuffing and bit stuffing.

3
Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP

4 Develop a simple data link layer that performs the flow control using the sliding window protocol, and loss
recovery using the Go-Back-N mechanism.
5 Implement Dijsktra’s algorithm to compute the shortest path through a network
6
Implement data encryption and data decryption

7 Simulate the network with five nodes n0, n1, n2, n3, n4, forming a star topology. The node n4 is at the centre.
Node n0 is a TCP source, which transmits packets to node n3 (a TCP sink) through the node n4. Node n1 is
another traffic source, and sends UDP packets to node n2 through n4. The duration of the simulation time is 10
seconds.[B2]
8 Simulate to study transmission of packets over Ethernet LAN and determine the number of packets drop
destination.[B3]
Demonstration Experiments ( For CIE ) if any
9 Simulate the different types of internet traffic such as FTP and TELNET over a wired network and analyze the
packet drop and packet delivery ratio in the network.[B4]
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.

Basic Architecture of NS2

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} {
set c [expr sqrt($a * $a + $b * $b)]
return $c }
puts “Diagonal of a 3, 4 right triangle is [Diag 3 4]”
Output: Diagonal of a 3, 4 right triangle is 5.0

⚪ Loops

while{$i < $n} { for {set i 0} {$i < $n} {incr i} {


... ...
} }
Wired TCL Script Components
Create the event scheduler
Open new files & turn on the tracing
Create the nodes
Setup the links
Configure the traffic type (e.g., TCP, UDP, etc)
Set the time of traffic generation (e.g., CBR, FTP)
Terminate the simulation
NS Simulator Preliminaries.
1. Initialization and termination aspects of the ns simulator.
2. Definition of network nodes, links, queues and topology.
3. Definition of agents and of applications.
4. The nam visualization tool.
5. Tracing and random variables.
Initialization and Termination of TCL Script in NS-2
An ns simulation starts with the command
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
#Open the NAM trace file
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
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,

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

Definition of a network of links and nodes


The way to define a node is

The node is created which is printed by the variable n0. When we shall refer to that node in the
script we shall thus write $n0.
Once we define several nodes, we can define the links that connect them. An example of
a definition of a link is:

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).
Dept of ISE, CIT, Gubbi

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:

Agents and Applications


We need to define routing (sources, destinations) the agents (protocols) the application
that use them.
FTP over TCP TCP is a dynamic reliable congestion control protocol. It uses
Acknowledgements created by the destination to know whether packets are well received.
There are number variants of the TCP protocol, such as Tahoe, Reno, NewReno, Vegas.
The type of agent appears in the first line:

The command $ns attach-agent $n0 $tcp defines the source node of the tcp connection.
The command

Defines the behavior of the destination node of TCP and assigns to it a pointer called sink.
#Setup a UDP connection
#setup a CBR over UDP connection
The below shows the definition of a CBR application using a UDP agent
The command $ns attach-agent $n4 $sink defines the destination node. The command
$ns connect $tcp $sink finally makes the TCP connection between the source and destination
nodes.

TCP has many parameters with initial fixed defaults values that can be changed if mentioned
explicitly. For example, the default TCP packet size has a size of 1000bytes.This can be changed
to another value, say 552bytes, using the command $tcp set packetSize_ 552.
When we have several flows, we may wish to distinguish them so that we can identify
them with different colors in the visualization part. This is done by the command $tcp set fid_ 1
that assigns to the TCP connection a flow identification of “1”.We shall later give the flow
identification of “2” to the UDP connection.

CBR over UDP


A UDP source and destination is defined in a similar way as in the case of TCP.
Instead of defining the rate in the command $cbr set rate_ 0.01Mb, one can define the
time interval between transmission of packets using the command.
The packet size can be set to some value using
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:

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

Structure of Trace Files


When tracing into an output ASCII file, the trace is organized in 12 fields as follows in
fig shown below, The meaning of the fields are:
Eve Tim Fro To PK PK Fla Fi Src Des Seq P
nt e m T T gs d t kt
No Ad Nu
Nod de Typ Siz dr Ad m id
e e e dr

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.
awk is a programmable, pattern-matching, and processing tool available in UNIX. It
works equally well with text and numbers.
awk is not just a command, but a programming language too. In other words, awk utility
is a pattern scanning and processing language. It searches one or more files to see if they contain
lines that match specified patterns and then perform associated actions, such as writing the line to
the standard output or incrementing a counter each time it finds a match.
Syntax:

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
THE –f OPTION: STORING awk PROGRAMS IN A FILE
You should holds large awk programs in separate file and provide them with the awk
extension for easier identification. Let’s first store the previous program in the file empawk.awk:
$ cat empawk.awk
Observe that this time we haven’t used quotes to enclose the awk program. You can now
use awk with the –f filename option to obtain the same output:

THE BEGIN AND END SECTIONS


Awk statements are usually applied to all lines selected by the address, and if there are no
addresses, then they are applied to every line of input. But, if you have to print something before
processing the first line, for example, a heading, then the BEGIN section can be used gainfully.
Similarly, the end section useful in printing some totals after processing is over.
The BEGIN and END sections are optional and take the form
BEGIN {action}
END {action}
These two sections, when present, are delimited by the body of the awk program. You
can use them to print a suitable heading at the beginning and the average salary at the end.
BUILT-IN VARIABLES
Awk has several built-in variables. They are all assigned automatically, though it is also
possible for a user to reassign some of them. You have already used NR, which signifies the
record number of the current line. We’ll now have a brief look at some of the other variable.
The FS Variable: as stated elsewhere, awk uses a contiguous string of spaces as the default field
delimiter. FS redefines this field separator, which in the sample database happens to be the |.
When used at all, it must occur in the BEGIN section so that the body of the program knows its
value before it starts processing:
BEGIN {FS=”|”}
This is an alternative to the –F option which does the same thing.
The OFS Variable: when you used the print statement with comma-separated arguments, each
argument was separated from the other by a space. This is awk’s default output field separator,
and can reassigned using the variable OFS in the BEGIN section:
BEGIN { OFS=”~” }
When you reassign this variable with a ~ (tilde), awk will use this character for delimiting the
print arguments. This is a useful variable for creating lines with delimited fields.
The NF variable: NF comes in quite handy for cleaning up a database of lines that don’t contain
the right number of fields. By using it on a file, say emp.lst, you can locate those lines not having
6 fields, and which have crept in due to faulty data entry:
$awk ‘BEGIN {FS = “|”}
NF! =6 { Print “Record No “, NR, “has”, “fields”}’ empx.lst
Experiment No 1

Q1. Implement three nodes point – to – point network with duplex links between them. Set the queue size, vary
the bandwidth and find the number of packets dropped.

1.tcl
set ns [new Simulator]
set nf [open /home/mca/sandhya/1.nam w]
$ns namtrace-all $nf
set tf [open /home/mca/sandhya/1.tr w]
$ns trace-all $tf
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam 1.nam &amp;
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Letter S is capital
# open a nam trace file in write mode
# nf nam filename
# tf trace filename
# clears trace file contents
# # creates 3 nodes
$ns duplex-link $n0 $n1 200Mb 10ms DropTail # establishing links
$ns duplex-link $n1 $n2 100Kb 1000ms DropTail
$ns queue-limit $n0 $n1 10
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packet_size_ 500
# attaching transport layer protocols
set null [new Agent/Null]
$ns attach-agent $n2 $null
$ns connect $udp $null
# creating sink(destination) node
# attaching application layer protocols
$ns at 0.1 “$cbr start”;
$ns at 1.0 “finish”;
$ns run

Output :

ns 1.tcl
1.awk
#awk file run in newterminal
BEGIN{
c=0;
}
{
if($1==&quot;d&quot;)
{
c++;
printf(&quot;%s\t%s\n&quot;,$5,$11);
}
} END{
printf(&quot;The number of packets dropped =%d\n&quot;,c);
}

output:-open new terminal


awk -f 1.awk 1.tr
The number of packets dropped =28
Experiment No 2

Implement the data link layer framing methods such as character, character-stuffing and
bit stuffing.

Bit Stuffing Program in C

#include<stdio.h>
#include<string.h>
int 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]);
return 0;
}

CHARACTER / BYTE STUFFING:


#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
char sdel[]="DELSTX",data[100]="",sdata[100]="";
int i=0,j=0;
printf("enter the message:");
scanf("%s",data);
// converting user input to uppercase

for(int k=0;data[k]!='\0';k++) {
if(data[k] >= 'a' && data[k] <= 'z'){
data[k] = data[k] - 32;
}
}
printf("original message: %s \n", data);

if(strlen(data) < 3){


strcat(sdel,data);
strcat(sdel,"DLEETX");
printf("Message after character stuffing is : %s", sdel);
}
else{
while(data[i] != '\0'){
if(data[i] == 'D' && data[i+1] == 'L' && data[i+2]=='E'){
strcat(sdata,"DLEDLE");
i = i+3;
j = j+6;
continue;
}
sdata[j] = data[i];
j++;
i++;
}
strcat(sdel,sdata);
strcat(sdel,"DLEETX");
printf("Message after character stuffing is : %s", sdel);
}
return 0;
}

Experiment No 3

Q. Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC
CCIP.
Implement On A Data Set Of Characters The Three CRC Polynomials – CRC 12, CRC 16 And CRC CCIP.

Introduction to Cyclic Redundancy Check:


CRC method can detect a single burst of length n, since only one bit per column will be changed, a burst of
length n+1 will pass undetected, if the first bit is inverted, the last bit is inverted and all other bits are correct.
If the block is badly garbled by a long burst or by multiple shorter burst, the probability that any of the n
columns will have the correct parity that is 0.5. so the probability of a bad block being expected when it
should not be 2 power(-n). This scheme sometimes is known as Cyclic Redundancy Code.

Program Algorithm/Flowchart:
Begin
Step 1:Declare I, j , fr[8], dupfr[11], recfr[11], tlen, flag, gen[4], genl, frl, rem[4] as integer
Step 2: initialize frl=8 and genl=4
Step 3: initialize i=0
Step 4: Repeat step(5to7) until i<frl
Step 5: read fr[i]
Step 6: dupfr[i]=fr[i]
Step 7: increment i
Step 8: initialize i=0
Step 9: repeat step(10to11) until i<genl
Step 10: read gen[i]
Step 11: increment i
Step 12: tlen=frl+genl-1
Step 13: initialize i=frl
Step 14: Repeat step(15to16) until i<tlen
Step 15: dupfr[i]=0
Step 16: increment i
Step 17: call the function remainder(dupfr)
Step 18: initialize i=0
Step 19: repeat step(20 to 21) until j<genl
Step 20: recfr[i]=rem[j]
Step 21: increment I and j
Step 22: call the function remainder(dupfr)
Step 23: initialize flag=0 and i=0
Step 24: Repeat step(25to28) until i<4
Step 25: if rem[i]!=0 then
Step 26: increment flag
Step 27: end if
Step 28: increment i
Step 29: if flag=0 then
Step 25: print frame received correctly
Step 25: else
Step 25: print the received frame is wrong
End
Function: Remainder(int fr[])
Begin
Step 1: Declare k,k1,I,j as integer
Step 2: initialize k=0;
Step 3: repeat step(4 to 14) until k< frl
Step 4: if ((fr[k] == 1) then
Step 5: k1=k
Step 6: initialize i=0, j=k
Step 7: repeat step(8 to 9) until i< genl
Step 8: rem[i] =fr[j] exponential gen[i]
Step 9: increment I and j
Step 10: initialize I = 0
Step 11: repeat step(12to13) until I <genl
Step 12: fr[k1] = rem[i]
Step 13: increment k1 and i
Step 14: end if
End

Program Code: // Program for Cyclic Redundency Check


#include<stdio.h>
int gen[4],genl,frl,rem[4];
void main()
{
int i,j,fr[8],dupfr[11],recfr[11],tlen,flag;
frl=8; genl=4;
printf("Enter frame:");
for(i=0;i<frl;i++)
{
scanf("%d",&fr[i]);
dupfr[i]=fr[i];
}
printf("Enter generator:");
for(i=0;i<genl;i++)
scanf("%d",&gen[i]);
tlen=frl+genl-1;
for(i=frl;i<tlen;i++)
{
dupfr[i]=0;
}
remainder(dupfr);
for(i=0;i<frl;i++)
{
recfr[i]=fr[i];
}
for(i=frl,j=1;j<genl;i++,j++)
{
recfr[i]=rem[j];
}
remainder(recfr);
flag=0;
for(i=0;i<4;i++)
{
if(rem[i]!=0)
flag++;
}
if(flag==0)
{
printf("frame received correctly");
}
else
{
printf("the received frame is wrong");
}
}
remainder(int fr[])
{
int k,k1,i,j;
for(k=0;k<frl;k++)
{
if(fr[k]==1)
{
k1=k;
for(i=0,j=k;i<genl;i++,j++)
{
rem[i]=fr[j]^gen[i];
}
for(i=0;i<genl;i++)
{
fr[k1]=rem[i];
k1++;
}
}
}
}

Program Output:
Enter frame: MLRITM
Enter generator: frame received correctly
------------------
(program exited with code: 24)
Experiment No 4

Q. Develop a simple data link layer that performs the flow control using the sliding window protocol, and loss
recovery using the Go-Back-N mechanism.
#include<stdio.h>

int main()
{
int w,i,f,frames[50];

printf("Enter window size: ");


scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");


scanf("%d",&f);

printf("\nEnter %d frames: ",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]);
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 window size: 3

Enter number of frames to transmit: 5

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

46

Acknowledgement of above frames sent is received by sender

Experiment No 5

Q . Implement Dijsktra’s algorithm to compute the shortest path through a network

Dijkstra’s Algorithm
1. Create cost matrix C[ ][ ] from adjacency matrix adj[ ][ ]. C[i][j] is the cost of going from
vertex i to vertex j. If there is no edge between vertices i and j then C[i][j] is infinity.

2. Array visited[ ] is initialized to zero.


for(i=0;i<n;i++)
visited[i]=0;

3. If the vertex 0 is the source vertex then visited[0] is marked as 1.

4. Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-1 from the
source vertex 0.
for(i=1;i<n;i++)
distance[i]=cost[0][i];
Initially, distance of source vertex is taken as 0. i.e. distance[0]=0;

5. for(i=1;i<n;i++)
– Choose a vertex w, such that distance[w] is minimum and visited[w] is 0. Mark visited[w] as
1.
– Recalculate the shortest distance of remaining vertices from the source.
– Only, the vertices not marked as 1 in array visited[ ] should be considered for recalculation of
distance. i.e. for each vertex v
if(visited[v]==0)
distance[v]=min(distance[v],
distance[w]+cost[w][v])

Time Complexity
The program contains two nested loops each of which has a complexity of O(n). n is number of
vertices. So the complexity of algorithm is O(n2).

Program for Dijkstra’s Algorithm in C


#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;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
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];
//initialize pred[],distance[] and visited[]
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;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through nextnode
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++;
}

//print the path and distance of each node


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);
}
}

OUTPUT:
mca@mca-OptiPlex-3090:~$ ./a.out
Enter no. of vertices:5

Enter the adjacency matrix:


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

Enter the starting node: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

Experiment No 6

Q . Implement data encryption and data decryption

#include <stdio.h>
int main()
{
int i, x;
char str[100];
printf("\nPlease enter a string:\t");
gets(str);
printf("\nPlease choose following options:\n");
printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);
//using switch case statements
switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value

printf("\nEncrypted string: %s\n", str);


break;

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

printf("\nDecrypted string: %s\n", str);


break;

default:
printf("\nError\n");
}
return 0;
}

Output
#Encryption
#Decryption
Experiment No 7

Q .simulate the network with nodes n0,n1,n2,n3,n4, forming a star topology. The node n4 is at the
center. Node n0 is a TCP source, which transmits packets to node n3(a tcp sink) through the node n4,
node n1 is another traffic source, and sends UDP packets to node n2 through n4. The duration of the
simulation time is 10 seconds.

#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red

#Open the NAM trace file

set nf [open 2b.nam w]

$ns namtrace-all $nf

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n4 1Mb 10ms DropTail

$ns duplex-link $n1 $n4 1Mb 10ms DropTail

$ns duplex-link $n4 $n3 1Mb 10ms DropTail

$ns duplex-link $n4 $n2 1Mb 10ms DropTail


#Setup a TCP connection

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

#Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

#Setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n2 $null

$ns connect $udp $null

#Setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packet_size_ 500

#Schedule events for the CBR and FTP agents

$ns at 0.0 "$cbr start"

$ns at 0.0 "$ftp start"

$ns at 20.0 "$ftp stop"

$ns at 20.0 "$cbr stop"

#Define a 'finish' procedure


proc finish {} {

global ns nf

$ns flush-trace

#Close the NAM trace file

close $nf

#Execute NAM on the trace file

exec nam 2b.nam &

exit 0

$ns at 10.0 "finish"

#Run the simulation

$ns run

Output:

run ns 2b.tcl
2b.awk:-
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);

output:-

mca@mca-HP-dx2480-MT-KL969AV:$ awk -f 2b.awk 2b.tr

Number of packets sent by TCP = 676

Number of packets sent by UDP=210


Experiment No 8

Q. Simulate to study transmission of packets over Ethernet LAN and determine the
number of packets drop destination.

set ns [new Simulator]

set nf [open 3b.nam w]

$ns namtrace-all $nf

set tf [open 3b.tr w]

$ns trace-all $tf

set n0 [$ns node]

$n0 color "red"

$n0 label "src1"

set n1 [$ns node]

set n2 [$ns node]

$n2 color "red"

$n2 label "src2"

set n3 [$ns node]

$n3 color "blue"

$n3 label "dest2"

set n4 [$ns node]

set n5 [$ns node]

$n5 color "blue"

$n5 label "dest1"

$ns make-lan "$n0 $n1 $n2 $n3 $n4" 1Mb 100ms LL Queue/DropTail Mac/802_3

$ns duplex-link $n4 $n5 1Kb 1ms DropTail

$ns queue-limit $n4 $n5 1


set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ftp0 set packetSize_ 500

$ftp0 set interval_ 0.0001

set sink5 [new Agent/TCPSink]

$ns attach-agent $n5 $sink5

$ns connect $tcp0 $sink5

set tcp2 [new Agent/TCP]

$ns attach-agent $n2 $tcp2

set ftp2 [new Application/FTP]

$ftp2 attach-agent $tcp2

$ftp2 set packetSize_ 600

$ftp2 set interval_ 0.001

set sink3 [new Agent/TCPSink]

$ns attach-agent $n3 $sink3

$ns connect $tcp2 $sink3

proc finish { } {

global ns nftf

$ns flush-trace

close $tf

close $nf

exec nam 3b.nam &

exit 0
}

$ns at 0.1 "$ftp0 start"

$ns at 5 "$ftp0 stop"

$ns at 7 "$ftp0 start"

$ns at 0.2 "$ftp2 start"

$ns at 8 "$ftp2 stop"

$ns at 14 "$ftp0 stop"

$ns at 10 "$ftp2 start"

$ns at 15 "$ftp2 stop"

$ns at 16 "finish"

$ns run

OUTPUT :
3b.awk

#awk file run in nnew terminal

BEGIN{

c=0;

if($1=="d")

{ c+

+;

}}

END{

printf("The number of %s packets dropped =%d\n",$5,c);

output:- the number of ack packets dropped=3


Experiment No 9

Q .Simulate the different types of internet traffic such as FTP and TELNET over a wired
network and analyze the packet drop and packet delivery ratio in the network.

5.tcl

set ns [new Simulator]

set nf [open 5b.nam w]

$ns namtrace-all $nf

set nd [open 5b.tr w]

$ns trace-all $nd

proc finish {}

{ global ns nfnd

$ns flush-trace

close $nf

close $nd

exec nam 5b.nam &

exit 0

$ns color 1 Blue

$ns color 2 Red

set n0 [$ns node]

$n0 label "ftp/src1"

set n1 [$ns node]

$n1 label "telnet/src2"

set n2 [$ns node]

set n3 [$ns node]


$n3 label "tcpsink/dest1"

set n4 [$ns node]

$n4 label "tcpsink/dest2"

$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n3 $n2 1Mb 10ms DropTail

$ns duplex-link $n4 $n2 1Mb 10ms DropTail

#$ns queue-limit $n1 $n2 1

$ns queue-limit $n0 $n2 3

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n4 $sink

$ns connect $tcp $sink

$tcp set fid_ 1

set ftp [new Application/FTP]

$ftp attach-agent $tcp

set tcp0 [new Agent/TCP]

$ns attach-agent $n1 $tcp0

set tcpsink0 [new Agent/TCPSink]

$ns attach-agent $n3 $tcpsink0

$ns connect $tcp0 $tcpsink0

$tcp0 set fid_ 2

set telnet0 [new Application/Telnet]

$telnet0 attach-agent $tcp0


$ns at 0.5 "$ftp start"

$ns at 4.5 "$ftp stop"

$ns at 0.5 "$telnet0 start"

$ns at 4.5 "$telnet0 stop"

$ns at 5.0 "finish"

$ns run

OUTPUT:

5b.awk

BEGIN{

ftpsend=0;

telnetsend=0;

ftpDrop=0;

telnetDrop=0;

telnetrecv=0;

ftprecv=0;
}

event=$1; src=$9;

dest=$10; pktype=$5;

if((event=="+") && (src=="0.0") && (dest=="4.0") &&pktype="tcp")

ftpsend++;

if((event=="r") && (src=="0.0") && (dest=="4.0") &&pktype="tcp")

ftprecv++;

if((event=="+") && (src=="1.0") && (dest=="3.0") &&pktype="tcp")

telnetsend++;

if((event=="r") && (src=="1.0") && (dest=="3.0") &&pktype="tcp")

telnetrecv++;

if((event=="d") (src=="0.0") && (dest=="4.0") &&pktype="tcp")

ftpDrop++;

}
if((event=="d") && (src=="1.0") && (dest=="3.0") &&pktype="tcp")

telnetDrop++;

END{

print "\n Number of Packets Sent by ftp : " ftpsend

print "\n Number of Packets Sent by tenet : " telnetsend

print "\n Number of Packets Received by ftp: " ftprecv

print "\n Number of Packets Received by telnet: " telnetrecv

print "\n Packet Delivery Ratio by FTP : " (ftprecv/ftpsend)*100

print "\n Packet Delivery Ratio by telnet : " (telnetrecv/telnetsend)*100

print "\n Packet drop by ftp:" ftpDrop

print "\n Packet drop by telnet:" telnetDrop

output:-

mca@mca-HP-dx2480-MT-KL969AV: $ awk -f 5b.awk 5b.tr

Number of Packets Sent by ftp : 565

Number of Packets Sent by tenet : 12

Number of Packets Received by ftp: 554

Number of Packets Received by telnet: 12

Packet Delivery Ratio by FTP : 98.0531

Packet Delivery Ratio by telnet : 100

Packet drop by ftp:11

Packet drop by telnet:0


PART B
Experiment No 6

Q. Simulate the transmission of ping messages over a network topology consisting of


6 nodes and find the number of packets dropped due to congestion.

#Create a simulator object

set ns [new Simulator]

#Open a trace file

set nf [open 6b.nam w]

$ns namtrace-all $nf

set nd [ open 6b.tr w]

$ns trace-all $nd

$ns color 1 Red

$ns color 2 Green

$ns color 3 Yellow

$ns color 4 Pink

#Define a 'finish' procedure

proc finish {} {

global ns nfnd

$ns flush-trace

close $nf

close $nd

exec nam 6b.nam&

exit 0

}
#Create six 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]

#Connect the nodes with two links

$ns duplex-link $n0 $n1 0.1Mb 10ms DropTail

$ns duplex-link $n1 $n2 0.1Mb 10ms DropTail

$ns duplex-link $n2 $n3 0.1Mb 10ms DropTail

$ns duplex-link $n3 $n4 0.1Mb 10ms DropTail

$ns duplex-link $n4 $n5 0.1Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right

$ns duplex-link-op $n1 $n2 orient right

$ns duplex-link-op $n2 $n3 orient down

$ns duplex-link-op $n3 $n4 orient left

$ns duplex-link-op $n4 $n5 orient left

$ns queue-limit $n0 $n1 3

$ns queue-limit $n1 $n2 3

#Define a 'recv' function for the class 'Agent/Ping'

Agent/Ping instprocrecv {from rtt} {

$self instvar node_

puts "node [$node_ id] received ping answer from \

#$from with round-trip-time $rttms."

#Create two ping agents and attach them to the nodes n0 and n2

set p0 [new Agent/Ping]

$ns attach-agent $n0 $p0


$p0 set fid_ 1

set p1 [new Agent/Ping]

$ns attach-agent $n5 $p1

$p1 set fid_ 2

set p2 [new Agent/Ping]

$ns attach-agent $n1 $p2

$p2 set fid_ 3

set p4 [new Agent/Ping]

$ns attach-agent $n4 $p4

$p4 set fid_ 4

#Connect the two agents

$ns connect $p0 $p1

$ns connect $p2 $p4

#Schedule events

$ns at 0.2 "$p0 send"

$ns at 0.4 "$p1 send"

$ns at 0.6 "$p0 send"

$ns at 0.8 "$p1 send"

$ns at 1.2 "$p2 send"

$ns at 1.4 "$p4 send"

$ns at 2.0 "finish"

#Run the simulation

$ns run
OUTPUT

6b.awk

#awk file run in nnew terminal

BEGIN{

drop=0;

if($1=="d")

drop++;

END{

printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);

}
output:-

mca@mca-HP-dx2480-MT-KL969AV:$ ns 6b.tcl

node 0received answer from 3 with round trip time 497.9 msec

node 3received answer from 0 with round trip time 736.7 msec

node 0received answer from 3 with round trip time 875.5 msec

node 3received answer from 0 with round trip time 895.7 msec

node 0received answer from 3 with round trip time 1253.1 msec

node 3received answer from 0 with round trip time 1273.3 msec

node 0received answer from 3 with round trip time 1691.9 msec

mca@mca-HP-dx2480-MT-KL969AV:$ awk -f 6b.awk 6b.tr

Total number of ping packets dropped due to congestion =4


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.
29. What is meant by file descriptor?
30. What is meant by traffic shaping?
31. How do you classify congestion control algorithms?
32. Differentiate between Leaky bucket and Token bucket.
33. How do you implement Leaky bucket?
34. How do you generate busty traffic?
35. What is the polynomial used in CRC-CCITT?
36. What are the other error detection algorithms?
37. What is difference between CRC and Hamming code?
38. Why Hamming code is called 7,4 code?
39. What is odd parity and even parity?
40. What is meant by syndrome?
41. What is generator matrix?
42. What is spanning tree?
43. Where Pirm’s algorithm does finds its use in Networks?
44. Differentiate between Prim’s and Kruskal’s algorithm.
45. What are Routing algorithms?
46. How do you classify routing algorithms? Give examples for each.
47. What are drawbacks in distance vector algorithm?
48. How routers update distances to each of its neighbor?
49. How do you overcome count to infinity problem?
50. What is cryptography?
51. How do you classify cryptographic algorithms?
52. What is public key?
53. What is private key?
54. What are key, ciphertext and plaintext?
55. What is simulation?
56. What are advantages of simulation?
57. Differentiate between Simulation and Emulation.
58. What is meant by router?
59. What is meant by bridge?
60. What is meant by switch?
61. What is meant by hub?
62. Differentiate between route, bridge, switch and hub.
63. What is ping and telnet?
64. What is FTP?
65. What is BER?
66. What is meant by congestion window?
67. What is BSS?
68. What is incoming throughput and outgoing throughput?

You might also like