0% found this document useful (0 votes)
5 views22 pages

CN Lab Programs

The document outlines the implementation of network simulations using NS2, including point-to-point networks, wireless LANs, and error detection using CRC. It details the setup of nodes, links, traffic applications, and procedures to measure packet drops and throughput. Additionally, it includes a Java program for the Bellman-Ford algorithm to find the shortest path in a graph and a program for simulating Ethernet LAN traffic.

Uploaded by

Subhashri C
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)
5 views22 pages

CN Lab Programs

The document outlines the implementation of network simulations using NS2, including point-to-point networks, wireless LANs, and error detection using CRC. It details the setup of nodes, links, traffic applications, and procedures to measure packet drops and throughput. Additionally, it includes a Java program for the Bellman-Ford algorithm to find the shortest path in a graph and a program for simulating Ethernet LAN traffic.

Uploaded by

Subhashri C
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

Program – 1:- Implement three nodes point–to–point networks with

duplex links between them. Set the queue size, vary the bandwidth,
and find the number of packets dropped.

#Create Simulator

set ns [new Simulator]

#Open Trace file and NAM file set ntrace [open [Link] w]

$ns trace-all $ntrace

set namfile [open [Link] w]

$ns namtrace-all $namfile

#Finish Procedure proc Finish {} {

global ns ntrace namfile

#Dump all the trace data and close the files

$ns flush-trace close $ntrace close $namfile

#Execute the nam animation file exec nam [Link] &

#Show the number of packets dropped

exec echo "The number of packet drops is " & exec grep -c "^d" [Link]
&

exit 0

#Create 3 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node]

#Label the nodes

$n0 label "TCP Source"

$n2 label "Sink"


#Set the color

$ns color 1 blue

#Create Links between nodes

#You need to modify the bandwidth to observe the variation in packet


drop

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

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

#Make the Link Orientation

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

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

#Set Queue Size

#You can modify the queue length as well to observe the variation in
packet drop

$ns queue-limit $n0 $n1 10

$ns queue-limit $n1 $n2 10

#Set up a Transport layer connection. set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

set sink0 [new Agent/TCPSink]

$ns attach-agent $n2 $sink0

$ns connect $tcp0 $sink0

#Set up an Application layer Traffic

set cbr0 [new Application/Traffic/CBR]

$cbr0 set type_ CBR


$cbr0 set packetSize_ 100

$cbr0 set rate_ 1Mb

$cbr0 set random_ false

$cbr0 attach-agent $tcp0

$tcp0 set class_ 1

#Schedule Events

$ns at 0.0 "$cbr0 start"

$ns at 5.0 "Finish"

#Run the Simulation

$ns run

Program 2 a).Implement simple ESS and with transmitting nodes in


wire-less LAN by simulation and determine the performance with
respect to the transmission of packets.
#Create Simulator

set ns [new Simulator]

#Use colors to differentiate the traffic

$ns color 1 Blue

$ns color 2 Red

#Open trace and NAM trace file set ntrace [open [Link] w]

$ns trace-all $ntrace

set namfile [open [Link] w]

$ns namtrace-all $namfile

#Finish Procedure proc Finish {} {

global ns ntrace namfile


#Dump all trace data and close the file

$ns flush-trace close $ntrace close $namfile

#Execute the nam animation file exec nam [Link] &

#Find the number of ping packets dropped

puts "The number of ping packets dropped are "

exec grep "^d" [Link] | cut -d " " -f 5 | grep -c "ping" & exit 0

#Create six nodes

for {set i 0} {$i < 6} {incr i} {

set n($i) [$ns node]

#Connect the nodes

for {set j 0} {$j < 5} {incr j} {

$ns duplex-link $n($j) $n([expr ($j+1)]) 0.1Mb 10ms DropTail

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

Agent/Ping instproc recv {from rtt} {

$self instvar node_

puts "node [$node_ id] received ping answer from $from with round trip
time $rtt

ms"

#Create two ping agents and attach them to n(0) and n(5)

set p0 [new Agent/Ping]

$p0 set class_ 1

$ns attach-agent $n(0) $p0


set p1 [new Agent/Ping]

$p1 set class_ 1

#Create a ns simulator

set ns [new Simulator]

#Setup topography object

set topo [new Topography]

$topo load_flatgrid 1500 1500

#Open the NS trace file

set tracefile [open [Link] w]

$ns trace-all $tracefile

#Open the NAM trace file

set namfile [open [Link] w]

$ns namtrace-all $namfile

$ns namtrace-all-wireless $namfile 1500 1500

#===================================

#Mobile node parameter setup

#===================================

$ns node-config -adhocRouting DSDV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail \

-ifqLen 20 \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON

#===================================

#Nodes Definition

#===================================

create-god 6

#Create 6 nodes

set n0 [$ns node]

$n0 set X_ 630

$n0 set Y_ 501

$n0 set Z_ 0.0

$ns initial_node_pos $n0 20

set n1 [$ns node]

$n1 set X_ 454

$n1 set Y_ 340

$n1 set Z_ 0.0

$ns initial_node_pos $n1 20

set n2 [$ns node]

$n2 set X_ 785

$n2 set Y_ 326

$n2 set Z_ 0.0

$ns initial_node_pos $n2 20

set n3 [$ns node]

$n3 set X_ 270

$n3 set Y_ 190

$n3 set Z_ 0.0

$ns initial_node_pos $n3 20


set n4 [$ns node]

$n4 set X_ 539

$n4 set Y_ 131

$n4 set Z_ 0.0

$ns initial_node_pos $n4 20

set n5 [$ns node]

$n5 set X_ 964

$n5 set Y_ 177

$n5 set Z_ 0.0

$ns initial_node_pos $n5 20

#===================================

#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

#Setup a TCP connection

set tcp0 [new Agent/TCP]

$ns attach-agent $n3 $tcp0

set sink1 [new Agent/TCPSink]

$ns attach-agent $n5 $sink1

$ns connect $tcp0 $sink1

#===================================
#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

#Setup a FTP Application over TCP connection

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

#===================================

#Termination

#===================================

#Define a 'finish' procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam [Link] &

exec echo "Number of packets dropped is:" &

exec grep -c "^D" [Link] &

exit 0

}
$ns at 1.0 "$cbr0 start"

$ns at 2.0 "$ftp0 start"

$ns at 180.0 "$ftp0 stop"

$ns at 200.0 "$cbr0 stop"

$ns at 200.0 "finish"

$ns at 70 "$n4 set dest 100 60 20"

$ns at 100 "$n4 set dest 700 300 20"

$ns at 150 "$n4 set dest 900 200 20"

$ns run

#===================================

COMPUTER NETWORK [.awk] CODE

Save the below code file name with .awk extension


#CODE TO BE SAVED AS .awk FILE

#===================================

BEGIN{

count1=0

count2=0

pack1=0

pack2=0

time1=0

time2=0

if($1=="r"&&$3=="_1_"&&$4=="RTR")

count1++

pack1=pack1+$8

time1=$2
}

if($1=="r"&&$3=="_2_"&&$4=="RTR")

count2++

pack2=pack2+$8

time2=$2

END{

printf("The Throughput from n0 to n1:


%fMbps\n",((count1*pack1*8)/(time1*1000000)));

printf("The Throughput from n1 to n2:


%fMbps\n",((count2*pack2*8)/(time2*1000000)));

Program 2 b) Write a program for error detecting code using CRC-CCITT (16- bits).

import [Link];

import [Link].*;

public class CRC1 {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

//Input Data Stream

[Link]("Enter message bits: ");

String message = [Link]();

[Link]("Enter generator: ");

String generator = [Link]();

int data[] = new int[[Link]() + [Link]() - 1];

int divisor[] = new int[[Link]()];


for(int i=0;i<[Link]();i++)

data[i] = [Link]([Link](i)+"");

for(int i=0;i<[Link]();i++)

divisor[i] = [Link]([Link](i)+"");

//Calculation of CRC

for(int i=0;i<[Link]();i++)

if(data[i]==1)

for(int j=0;j<[Link];j++)

data[i+j] ^= divisor[j];

//Display CRC

[Link]("The checksum code is: ");

for(int i=0;i<[Link]();i++)

data[i] = [Link]([Link](i)+"");

for(int i=0;i<[Link];i++)

[Link](data[i]);

[Link]();

//Check for input CRC code

[Link]("Enter checksum code: ");

message = [Link]();

[Link]("Enter generator: ");

generator = [Link]();

data = new int[[Link]() + [Link]() - 1];

divisor = new int[[Link]()];

for(int i=0;i<[Link]();i++)

data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link]();i++)

divisor[i] = [Link]([Link](i)+"");

//Calculation of remainder

for(int i=0;i<[Link]();i++) {

if(data[i]==1)

for(int j=0;j<[Link];j++)

data[i+j] ^= divisor[j];

//Display validity of data

boolean valid = true;

for(int i=0;i<[Link];i++)

if(data[i]==1){

valid = false;

break;

if(valid==true)

[Link]("Data stream is valid");

else

[Link]("Data stream is invalid. CRC error occurred.");

3.a)Implement transmission of ping messages/trace route over a network topology consisting of 6


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

$ns attach-agent $n(5) $p1

$ns connect $p0 $p1


#Set queue size and monitor the queue

#Queue size is set to 2 to observe the drop in ping packets

$ns queue-limit $n(2) $n(3) 2

$ns duplex-link-op $n(2) $n(3) queuePos 0.5

#Create Congestion

#Generate a Huge CBR traffic between n(2) and n(4)

set tcp0 [new Agent/TCP]

$tcp0 set class_ 2

$ns attach-agent $n(2) $tcp0 set sink0 [new Agent/TCPSink]

$ns attach-agent $n(4) $sink0

$ns connect $tcp0 $sink0

#Apply CBR traffic over TCP

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set rate_ 1Mb

$cbr0 attach-agent $tcp0

#Schedule events

$ns at 0.2 "$p0 send"

$ns at 0.4 "$p1 send"

$ns at 0.4 "$cbr0 start"

$ns at 0.8 "$p0 send"

$ns at 1.0 "$p1 send"

$ns at 1.2 "$cbr0 stop"

$ns at 1.4 "$p0 send"

$ns at 1.6 "$p1 send"

$ns at 1.8 "Finish"


#Run the Simulation

$ns run

Write a program to find the shortest path between vertices


Program 3B)
using the bellman-ford algorithm
import [Link];

public class ford

private int D[];

private int num_ver;

public static final int MAX_VALUE = 999;

public ford(int num_ver)

this.num_ver = num_ver;

D = new int[num_ver + 1];

public void BellmanFordEvaluation(int source, int A[][])

for (int node = 1; node <= num_ver; node++)

D[node] = MAX_VALUE;

D[source] = 0;

for (int node = 1; node <= num_ver - 1; node++)

for (int sn = 1; sn <= num_ver; sn++)

for (int dn = 1; dn <= num_ver; dn++)


{

if (A[sn][dn] != MAX_VALUE)

if (D[dn] > D[sn]+ A[sn][dn])

D[dn] = D[sn] + A[sn][dn];

for (int sn = 1; sn <= num_ver; sn++)

for (int dn = 1; dn <= num_ver; dn++)

if (A[sn][dn] != MAX_VALUE)

if (D[dn] > D[sn]+ A[sn][dn])

[Link]("The Graph contains negative egde cycle");


}

for (int vertex = 1; vertex <= num_ver; vertex++)

[Link]("distance of source"+source+"to"+vertex+"is" +
D[vertex]);

public static void main(String[ ] args)

{
int num_ver = 0;

int source;

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of vertices");

num_ver = [Link]();

int A[][] = new int[num_ver + 1][num_ver + 1];

[Link]("Enter the adjacency matrix");

for (int sn = 1; sn <= num_ver; sn++)

for (int dn = 1; dn <= num_ver; dn++)

A[sn][dn] = [Link]();

if (sn == dn)

A[sn][dn] = 0;

continue;

if (A[sn][dn] == 0)

A[sn][dn] = MAX_VALUE;

[Link]("Enter the source vertex");

source = [Link]();

ford b = new ford (num_ver);

[Link](source, A);

[Link]();
}

Program 4 a) Implement an Ethernet LAN using n nodes and set


multiple traffic nodes and plot conges the window for different
source/destination.

#Create Simulator

set ns [new Simulator]

#Use colors to differentiate the traffics

$ns color 1 Blue

$ns color 2 Red

#Open trace and NAM trace file set ntrace [open [Link] w]

$ns trace-all $ntrace

set namfile [open [Link] w]

$ns namtrace-all $namfile

#Use some flat file to create congestion graph windows set winFile0
[open WinFile0 w]

set winFile1 [open WinFile1 w]

#Finish Procedure proc Finish {} {

#Dump all trace data and Close the files global ns ntrace namfile

$ns flush-trace close $ntrace close $namfile

#Execute the NAM animation file exec nam [Link] &

#Plot the Congestion Window graph using xgraph exec xgraph WinFile0
WinFile1 &

exit 0

}
#Plot Window Procedure

proc PlotWindow {tcpSource file} { global ns

set time 0.1

set now [$ns now]

set cwnd [$tcpSource set cwnd_] puts $file "$now $cwnd"

$ns at [expr $now+$time] "PlotWindow $tcpSource $file"

#Create 6 nodes

for {set i 0} {$i<6} {incr i} { set n($i) [$ns node]

#Create duplex links between the nodes

$ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail

$ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail

$ns duplex-link $n(2) $n(3) 0.6Mb 100ms DropTail

#Nodes n(3) , n(4) and n(5) are considered in a LAN

set lan [$ns newLan "$n(3) $n(4) $n(5)" 0.5Mb 40ms LL Queue/DropTail
MAC/802_3 Channel]

#Orientation to the nodes

$ns duplex-link-op $n(0) $n(2) orient right-down

$ns duplex-link-op $n(1) $n(2) orient right-up

$ns duplex-link-op $n(2) $n(3) orient right

#Setup queue between n(2) and n(3) and monitor the queue

$ns queue-limit $n(2) $n(3) 20

$ns duplex-link-op $n(2) $n(3) queuePos 0.5


#Set error model on link n(2) to n(3) set loss_module [new ErrorModel]

$loss_module ranvar [new RandomVariable/Uniform]

$loss_module drop-target [new Agent/Null]

$ns lossmodel $loss_module $n(2) $n(3)

#Set up the TCP connection between n(0) and n(4) set tcp0 [new
Agent/TCP/Newreno]

$tcp0 set fid_ 1

$tcp0 set window_ 8000

$tcp0 set packetSize_ 552

$ns attach-agent $n(0) $tcp0

set sink0 [new Agent/TCPSink/DelAck]

$ns attach-agent $n(4) $sink0

$ns connect $tcp0 $sink0

#Apply FTP Application over TCP set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ftp0 set type_ FTP

#Set up another TCP connection between n(5) and n(1) set tcp1 [new
Agent/TCP/Newreno]

$tcp1 set fid_ 2

$tcp1 set window_ 8000

$tcp1 set packetSize_ 552

$ns attach-agent $n(5) $tcp1

set sink1 [new Agent/TCPSink/DelAck]

$ns attach-agent $n(1) $sink1


$ns connect $tcp1 $sink1

#Apply FTP application over TCP set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$ftp1 set type_ FTP

#Schedule Events

$ns at 0.1 "$ftp0 start"

$ns at 0.1 "PlotWindow $tcp0 $winFile0"

$ns at 0.5 "$ftp1 start"

$ns at 0.5 "PlotWindow $tcp1 $winFile1"

$ns at 25.0 "$ftp0 stop"

$ns at 25.1 "$ftp1 stop"

$ns at 25.2 "Finish"

#Run the simulation

$ns run

Program 4 b)Write a program for congestion control using a leaky


bucket algorithm

import [Link];

import [Link].*;

public class lab7 {

public static void main(String[] args)

int i;

int a[]=new int[20];

int buck_rem=0,buck_cap=4,rate=3,sent,recv;

Scanner in = new Scanner([Link]);

[Link]("Enter the number of packets");

int n = [Link]();
[Link]("Enter the packets");

for(i=1;i<=n;i++)

a[i]= [Link]();

[Link]("Clock \t packet size \t accept \t sent \t


remaining");

for(i=1;i<=n;i++)

if(a[i]!=0)

if(buck_rem+a[i]>buck_cap)

recv=-1;

else

recv=a[i];

buck_rem+=a[i];

else

recv=0;

if(buck_rem!=0)

if(buck_rem<rate)

{sent=buck_rem;

buck_rem=0;

else

sent=rate;

buck_rem=buck_rem-rate;

}
else

sent=0;

if(recv==-1)

[Link](+i+ "\t\t" +a[i]+ "\t dropped \t" + sent +"\t"


+buck_rem);

else

[Link](+i+ "\t\t" +a[i] +"\t\t" +recv +"\t" +sent + "\t"


+buck_rem);

You might also like