You are on page 1of 14

LAB PROGRAM 1

1. Simulate a three nodes point — to — point network with duplex links between them. Set
the queue size and vary the bandwidth and find the number of packets dropped.
TCL Script
set ns [new Simulator]
set nf [open prog1.nam w]
$ns namtrace-all $nf

set nd [open prog1.tr w]


$ns trace-all $nd

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]

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


$ns duplex-link $n1 $n2 512Kb 10ms DropTail
$ns queue-limit $n1 $n2 5

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0

set sink [new Agent/Null]


$ns attach-agent $n2 $sink
$ns connect $udp0 $sink

set cbr0 [new Application/Traffic/CBR]


$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

proc finish { } {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog1.nam &
#exec awk -f prog1.awk prog1.tr &
exit 0
}

$ns at 0.2 "$cbr0 start"


$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
#immediately after BEGIN should open braces „{„

BEGIN {
dcount = 0;
rcount = 0;
}
{
event = $1;
if(event == "d")
{
dcount++;
}
if(event == "r")
{
rcount++;
}
}
END {
printf("The no.of packets dropped : %d\n ",dcount);
printf("The no.of packets recieved : %d\n ",rcount);
}

Steps for execution:


*Open vi editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# vi lab1.tcl
* Run the simulation program
[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~]# awk –f lab1.awk lab1.tr
*To see the trace file contents open the file as ,
[root@localhost~]# vi lab1.tr

Trace file contains 12 columns:


Event type, Event time, From Node, To Node, Packet Type, Packet Size, Flags (indicated by --------),
Flow ID, Source address, Destination address, Sequence ID, Packet ID

LAB PROGRAM 2
A. BIT STUFFING

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

B.CHARACTER STUFFING
#include<string.h>
#include<stdio.h>
void 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;
printf(“enter characters to be stuffed:”);
scanf(“%s”,a);
printf(“\n Enter 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);
}

LAB PROGRAM 3
Implement the error correcting code Cyclic Redundancy Check (CRC) of data link layer using various polynomials like
CRC-CRC 12, CRC 16 and CRC CCIPP.

Commonly used divisor polynomials are:


CRC 12 : x12 + x11 + x3 + x2 + x + 1
CRC 16 : x16 + x15 + x2 + 1
CRC CCITT : x16 + x12 + x5 + 1
Source code

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

int crc(char *ip, char *op, char *poly, int mode)


{
strcpy(op, ip);
if (mode) {
for (int i = 1; i < strlen(poly); i++)
strcat(op, "0");
}
/* Perform XOR on the msg with the selected polynomial */
for (int i = 0; i < strlen(ip); i++) {
if (op[i] == '1') {
for (int j = 0; j < strlen(poly); j++) {
if (op[i + j] == poly[j])
op[i + j] = '0';
else
op[i + j] = '1';
}
}
}
/* check for errors. return 0 if error detected */
for (int i = 0; i < strlen(op); i++)
if (op[i] == '1')
return 0;
return 1;
}

int main()
{
char ip[50], op[50], recv[50];
char poly[] = "10001000000100001";

printf( "Enter the input message in binary\n");


scanf("%s",&ip);
crc(ip, op, poly, 1);
printf("The transmitted message is: %s,%s\n", ip, op + strlen(ip));
printf( "Enter the recevied message in binary\n");
scanf("%s",&recv);
if (crc(recv, op, poly, 0))
printf("No error in data\n");
else
printf("Error in data transmission has occurred\n" );

return 0;
}

OUTPUT 1
Enter the input message in binary

1001
The transmitted message is: 1001,1001000100101001

Enter the recevied message in binary

10011001000100101001

No error in data

OUTPUT 2.

Enter the input message in binary

1001

The transmitted message is: 1001,1001000100101001

Enter the recevied message in binary

10001001000100101001

Error in data transmission has occurred

LAB PROGRAM 4
LAB PROGRAM 5 –Dijsktra’s algorithm

#include<stdio.h>

void dijkstra(int n, int v, int cost[10][10],int dist[10])


{
int count, u, i, w, visited[10], min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min) && (visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;
for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w]) && (visited[w]!=1))
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n, v, cost[10][10], dist[10], i, j;

printf("Enter number of vertices:");


scanf("%d",&n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);
printf("\nShortest path from \n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d -> %d = %d", v, i, dist[i]);

Output:
Enter number of vertices:7
Enter cost matrix (for infinity, enter 999):

0 2 999 3 999 999 999


2 0 9 999 1 4 999
999 9 0 999 999 3 999
3 999 999 0 5 999 7
999 1 999 5 0 999 4
999 4 3 999 999 0 6
999 999 999 7 4 6 0

Enter source vertex: 0


Shortest path from
0 -> 1 = 2
0 -> 2 = 9
0 -> 3 = 3
0 -> 4 = 3
0 -> 5 = 6
0 -> 6 = 7

LAB PROGRAM 6 -ENCRYPTION AND DECRYPTION OF DATA

Source Code:
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int gcd(long m,long n)


{
while(n!=0)
{
long r=m%n;
m=n;
n=r;

return m;

}
int rsa(char message[50])

long p=0,q=0,n=0,e=0,d=0,phi=0;

long nummes[100]={0};
long encrypted[100]={0},decrypted[100]={0};
long i=0,j=0,nofelem=0;
printf("\nenter value of p and q\n");
scanf("%d%d",&p,&q);
n=p*q;
phi=(p-1)*(q-1);
for(i=2;i<phi;i++)
if(gcd(i,phi)==1) break;
e=i;

for(i=2;i<phi;i++)
if((e*i-1)%phi==0)break;
d=i;

for(i=0;i<strlen(message);i++)
nummes[i]=message[i]-96;
nofelem=strlen(message);
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
printf("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
printf(" %ld ",encrypted[i]);
printf("%c",(char)(encrypted[i])+96);
}
for(i=0;i<nofelem;i++)
{
decrypted[i]=1;
for(j=0;j<d;j++)
decrypted[i]=(decrypted[i]*encrypted[i])%n;
}
printf("\n Decrypted message\n ");
for(i=0;i<nofelem;i++)
printf("%c",(char)(decrypted[i]+96));
return 0;
}

int main()
{
char msg[50];
system("clear");
printf("Enter The Message To Be Encrypted\n");
scanf("%s",msg);
rsa(msg);
return 0;
}

Output:
Enter the text:
hello
Enter the value of P and Q :
5
7
Encrypted Text is: 8 h 10 j 17 q 17 q 15 o
Decrypted Text is: hello

LAB PROGRAM 7
Write a TCL script to simulate the network described below Consider a small network with five 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.

set val(stop) 10.0;


set ns [new Simulator]

$ns color 1 Blue


$ns color 2 Red

#open the NS trace file


set tracefile [open adc.tr w]
$ns trace-all $tracefile

#open the NAM trace file


set namfile [open adc.nam w]
$ns namtrace-all $namfile

#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

#Create labels for nodes


$n0 label "Tcp_Source"
$n3 label "Tcp_Destination"
$n1 label "Udp_Source"
$n2 label "Udp_Destination"

$n0 shape square


$n3 shape square
$n4 shape circle
$n1 shape hexagon
$n2 shape hexagon

$n0 color green


$n3 color red
$n1 color green
$n2 color red
$n4 color black

#create link between two nodes


$ns duplex-link $n0 $n4 100.0Mb 40ms DropTail
$ns queue-limit $n0 $n4 5;
$ns duplex-link $n4 $n3 100.0Mb 40ms DropTail
$ns queue-limit $n4 $n3 5;
$ns duplex-link $n1 $n4 100.0Mb 40ms DropTail
$ns queue-limit $n1 $n4 5;
$ns duplex-link $n4 $n2 100.0Mb 40ms DropTail
$ns queue-limit $n4 $n2 5;
$ns duplex-link-op $n4 $n0 orient left-down
$ns duplex-link-op $n1 $n4 orient left-up
$ns duplex-link-op $n3 $n4 orient left-down
$ns duplex-link-op $n2 $n4 orient right-down

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp0 $sink3
$tcp0 set packetSize_ 1000

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1
set null2 [new Agent/Null]
$ns attach-agent $n2 $null2
$ns connect $udp1 $null2
$udp1 set packetSize_ 1000

$tcp0 set fid_ 1


$udp1 set fid_ 2

set cbr0 [new Application/Traffic/CBR]


$cbr0 attach-agent $tcp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 3.0Mb
$cbr0 set tandom_ null
$ns at 0.01 "$cbr0 start"
$ns at 9.9 "$cbr0 stop"

set cbr1 [new Application/Traffic/CBR]


$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 1000
$cbr1 set rate_ 2.0Mb
$cbr1 set random_ null
$ns at 0.1 "$cbr1 start"
$ns at 9.0 "$cbr1 stop"

proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam adc.nam &
exit 0
}
$ns at $val(stop) "finish"
$ns run

LAB PROGRAM-8
Simulate to study transmission of packets over Ethernet LAN and determine the number of packets drop at destination.
TCL Script

set ns [new Simulator]


set nf [open prog4.nam w]
$ns namtrace-all $nf
set nd [open prog4.tr w]
$ns trace-all $nd

$ns color 1 Blue


$ns color 2 Red

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]
set n8 [$ns node]

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


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

$ns newLan "$n3 $n4 $n5 $n6 $n7 $n8" 512Kb 40ms LL
Queue/DropTail

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


$ns duplex-link-op $n2 $n0 orient right-up
$ns duplex-link-op $n0 $n3 orient right

set tcp [new Agent/TCP]


$ns attach-agent $n1 $tcp
set sink1 [new Agent/TCPSink]
$ns attach-agent $n4 $sink1
$ns connect $tcp $sink1

set sink2 [new Agent/TCPSink]


$ns attach-agent $n7 $sink2
$ns connect $tcp $sink2
$tcp set packetSize_ 500
set ftp [new Application/FTP]
$ftp attach-agent $tcp

set udp [new Agent/UDP]


$ns attach-agent $n2 $udp
set null1 [new Agent/Null]
$ns attach-agent $n5 $null1
$ns connect $udp $null1

set null2 [new Agent/Null]


$ns attach-agent $n8 $null2
$ns connect $udp $null2

set cbr [new Application/Traffic/CBR]


$cbr attach-agent $udp
$cbr set packetSize_ 500
$cbr set rate_ 0.01Mb
$cbr set random_ false

proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog4.nam &
exec awk -f prog4.awk prog4.tr &
exit 0
}
$ns at 0.5 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 5.0 "$ftp stop"
$ns at 5.0 "$cbr stop"
$ns at 5.5 "finish"
$ns run

Prog8.awk
BEGIN {
count=0;
count1=0;
}
{
event=$1;
if(event=="d")
{
count++;
}
if(event=="r")
{
count1++;
}
}
END {
printf("No of packets dropped and recieved : %d %d\n",count,count1);
}

LAB PROGRAM 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.

set ns [new Simulator]


set nf [open prog6a.nam w]
$ns namtrace-all $nf
set nd [open prog6a.tr w]
$ns trace-all $nd

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

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


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

set tcp [new Agent/TCP]


set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp

proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog6a.nam &
exit 0
}
$ns at 0.2 "$ftp start"
$ns at 4.5 "$ftp stop"
$ns at 5.0 "finish"
$ns run

Prog9b.tcl

set ns [new Simulator]


set nf [open prog6b.nam w]
$ns namtrace-all $nf
set nd [open prog6b.tr w]
$ns trace-all $nd
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

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


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

set tcp0 [new Agent/TCP]


set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n4 $tcpsink0

set telnet0 [new Application/Telnet]


$telnet0 attach-agent $tcp0
$ns connect $tcp0 $tcpsink0

proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam prog6b.nam &
exit 0
}
$ns at 0.2 "$telnet0 start"
$ns at 4.5 "$telnet0 stop"
$ns at 5.8 "finish"
$ns run

Prog9.awk
BEGIN {
sSize =0 ;
startTime=5.0;
stopTime=0.1;
Tput=0;
}
{
event=$1;
time=$2;
from=$3;
to=$4;
pkt=$5;
size=$6;
fid=$7;
src=$8;
dst=$9;
seqn=$10;
pid=$11;
if(event == "+") {
if(time < startTime) { startTime=time; }
}
if(event == "r") {
if(time >stopTime) { stopTime =time; }
sSize+=size;
}
Tput=(sSize/(stopTimestartTime))*(
8/1000);
printf("%f\t%f\n",time,Tput);
}
END {
}

You might also like