CNLABFINAL (v2) PDF
CNLABFINAL (v2) PDF
TCL
set ns [new Simulator] # ’ns’ is an object which is created within the ‘Simulator’ class with the help of ‘new’ operator
$ns trace-all $f # ‘$ns’ is a reference to the ‘Simulator’ class (the main class); ‘$’ indicates the content of ns
$ns namtrace-all $nf # all data received from the trace and animation files will be pushed to the ‘Simulator’ class
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exit 0
set n0 [$ns node] # ‘node’ is a subclass of ‘Simulator’ class, within which nodes are created
$ns duplex-link $n1 $n2 1.25Mb 20ms DropTail # change bandwidth of both the links from 0.25 to 2.75Mb
1
$ns duplex-link-op $n1 $n2 orient down
# transport layer protocol; ‘UDP’ class is within ‘Agent’ class (which again is in ‘Simulator’ class)
set udp0 [new Agent/UDP] # object udp0 is attached to node n0 (n0 gets all the features of UDP protocol)
# application layer protocol; ‘CBR’ class is within ‘Traffic’ class which is within ‘Application class’ (which again is in
‘Simulator’ class)
2
$ns at 0.5 "$cbr0 start"
$ns run
AWK
BEGIN {
cbrPkt=0;
tcpPkt=0;
END {
XG
3
"CBR"
0.25 532
0.50 282
0.75 59
1.0 16
1.25 22
1.50 14
1.75 24
2.00 6
2.25 8
2.50 2
2.75 1
"TCP"
0.25 1
0.50 1
0.75 13
1.0 24
1.25 18
1.50 15
1.75 6
2.00 8
2.25 6
2.50 5
2.75 1
4
PROGRAM 2: Transmission of Ping messages
TCL
proc finish {} {
global ns tr nf
$ns flush-trace
close $tr
close $nf
exit 0
5
$ns duplex-link $n2 $n3 2.0Mb 10ms DropTail # Change bandwidth from 0.1-0.2 to 2Mb
6
$ns connect $p5 $p1
proc sendPingPacket {} {
global ns p0 p1 p4 p5
$ns run
AWK
BEGIN {
pingDrop=0;
7
if($1=="d") { pingDrop++ ; }
END {
XG
XUnitText: Bandwidth
"PING"
0.1 2076
0.15 997
0.17 530
0.19 229
0.2 69
0.4 8
0.6 8
0.8 8
8
Program 3: Ethernet LAN
TCL
proc finish {} {
global ns f1 f2
$ns flush-trace
close $f1
close $f2
exit 0
9
$ns duplex-link $n0 $n3 1Mb 20ms DropTail
$ns make-lan "$n3 $n4 $n5 $n6 $n7 $n8" 512Kb 40ms LL Queue/DropTail Mac/802_3
# Set up orientation
10
# Make node 2 as FTP/TCP agent that uses RENO algorithm
$ns run
11
AWK
BEGIN {
}
{
if($6 == "cwnd_") {
printf("%f\t%f\n",$1,$7);
}
}
END {
}
XG
XUnitText: Time
"Vegas"
0.000000 1.000000
0.946450 2.000000
0.946450 3.000000
1.392900 4.000000
1.394170 5.000000
"Reno"
0.000000 1.000000
1.222970 2.000000
1.447300 3.000000
1.449190 4.000000
1.671630 5.000000
12
PROGRAM 4: Simple ESS
TCL
# Getting user input
if {$argc != 1} {
exit 0
set val(ll) LL
set val(ifqlen) 50
13
# Tracing nam and trace file
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-channelType $val(chan) \
-propType $val(prop) \
-antType $val(ant) \
-ifqLen $val(ifqlen) \
-phyType $val(netif) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
14
# Location fixing of the nodes
# Attach agents
proc destination {} {
global ns val n
15
set XX [expr rand()*750]
proc stop {} {
close $trfd
close $namfd
exit 0
$ns run
16
AWK
BEGIN {
PacketRcvd=0;
Throughput=0.0;
PacketRcvd++;
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
XG
XUnitText: Nodes
YUnitText: Throughput
"Wireless"
10 0.311747 60 0.425937
20 0.307284 70 0.569095
30 0.564126 80 0.666021
40 0.483032 90 0.525035
17
PROGRAM 5: GSM
TCL
# General Parameters
set opt(ecn) 0;
# Topology
# AQM parameters
set opt(minth) 5;
set buf(gsm) 10
18
$ns trace-all $tf
proc cell_topo {} {
global ns nodes
19
# RED and TCP parameters
# Create topology
switch $opt(type) {
set_link_params $opt(type)
proc stop {} {
$ns flush-trace
close $nf
close $tf
exit 0
20
}
$ns run
AWK
BEGIN {
PacketRcvd=0;
Throughput=0.0;
PacketRcvd++;
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
XG
XUnitText: Packet
YUnitText: Throughput
"GSM"
21
PROGRAM 6: CDMA
TCL
# General Parameters
set opt(ecn) 0;
# Topology
# AQM parameters
set opt(minth) 5;
set buf(gsm) 10
22
$ns trace-all $tf
proc cell_topo {} {
global ns nodes
23
# RED and TCP parameters
# Create topology
switch $opt(type) {
set_link_params $opt(type)
proc stop {} {
$ns flush-trace
close $nf
close $tf
24
exit 0
$ns run
AWK
BEGIN {
PacketRcvd=0;
Throughput=0.0;
PacketRcvd++;
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
XG
"CDMA"
100 0.333067 225 0.369244
125 0.355822 250 0.373422
150 0.360711 275 0.377600
175 0.367111 300 0.378311
200 0.367644
25
PROGRAM 7: CRC
import java.util.Scanner;
public class crc {
d=new int[m+n];
g=new int[n];
System.out.println("Enter databits:");
for(i=0;i<n;i++)
d[i]=s.nextInt();
26
for(j=i;j<m+i;j++) {
if(msb==0)
r[j]=r[j]^z[k];
else
r[j]=r[j]^g[k];
k++;
}
r[m+i]=d[m+i];
}
//System.out.println("The code bits added are:");
for(i=n;i<n+m-1;i++) {
d[i]=r[i];
//System.out.print(d[i]+" ");
}
}
}
OUTPUT:
Enter databits: 1 0 1 0 1 0 1
27
PROGRAM 8: Bellman Ford
import java.util.Scanner;
public class prog8
{
private int D[];
private int num_ver;
int n;
public static final int MAX_VALUE = 999;
public prog8(int n)
{
this.n=n;
D = new int[n+1];
}
public void shortest(int s,int A[][])
{
for (int i=1;i<=n;i++)
{
D[i]=MAX_VALUE;
}
D[s] = 0;
for(int k=1;k<=n-1;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(A[i][j]!=MAX_VALUE)
{
if(D[j]>D[i]+A[i][j])
D[j]=D[i]+A[i][j];
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(A[i][j]!=MAX_VALUE)
{
if(D[j]>D[i]+A[i][j])
{
System.out.println("The Graph contains negative
egde cycle");
return;
}
}
}
}
for(int i=1;i<=n;i++)
{
28
System.out.println("Distance of source " + s + " to "+ i + " is " + D[i]);
}
}
OUTPUT:
29
PROGRAM 9: TCP
Client
import java.io.*;
import java.net.*;
import java.util.Scanner;
OutputStream osStream=socket.getOutputStream();
PrintWriter pwWriter=new PrintWriter(osStream,true);
pwWriter.println(fname);
InputStream inStream=socket.getInputStream();
Scanner sin=new Scanner(inStream);
while(sin.hasNext())
System.out.println(sin.next());
}
}
Server
import java.io.*;
import java.net.*;
import java.util.Scanner;
InputStream iStream=socket.getInputStream();
Scanner sin=new Scanner(iStream);
String fname=sin.next();
OutputStream oStream=socket.getOutputStream();
PrintWriter pWriter=new PrintWriter(oStream,true);
File file=new File(fname);
Scanner fin=new Scanner(file);
while(fin.hasNext())
pWriter.println(fin.next());
30
System.out.println("Connection is successful and file contents are displayed in
the client window");
}
}
31
PROGRAM 10: UDP
Client
import java.net.*;
while(true) {
DatagramPacket dp=new DatagramPacket(data, data.length);
ds.receive(dp);
String str=new String(dp.getData());
System.out.println(str);
}
}
}
Server
import java.net.*;
import java.util.Scanner;
byte code[]=msg.getBytes();
InetAddress iAddress=InetAddress.getByName("127.0.0.1");
DatagramPacket dp=new DatagramPacket(code,code.length,iAddress,4000);
ds.send(dp);
}
}
32
PROGRAM 11: RSA
import java.util.*;
import java.io.*;
public class pg11
{
static int gcd(int m,int n)
{
while(n!=0)
{
int r=m%n;
m=n;
n=r;
}
return m;
}
public static void main(String args[])
{
int p=0,q=0,n=0,e=0,d=0,phi=0;
int nummes[]=new int[100];
int encrypted[]=new int[100];
int decrypted[]=new int[100];
int i=0,j=0,nofelem=0;
Scanner sc=new Scanner(System.in);
String message ;
System.out.println("Enter the Message to be encrypted: ");
message= sc.nextLine();
System.out.println("Enter value of p and q\n");
p=sc.nextInt();
q=sc.nextInt();
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<message.length();i++)
{
char c = message.charAt(i);
int a =(int)c;
nummes[i]=c-96;
}
nofelem=message.length();
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
33
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
System.out.println("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
System.out.print(encrypted[i]);
System.out.print((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;
}
System.out.println("\n Decrypted message\n ");
for(i=0;i<nofelem;i++)
System.out.print((char)(decrypted[i]+96));
return;
}
}
OUTPUT:
Encrypted message
8h10j17q17q150
Decrypted message
hello
34
PROGRAM 12: Leaky Bucket
import java.util.*;
class pg12
{
static int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
public static void main(String[] args)
{
int drop=0,mini,nsec,cap,count=0,i,oprate;
int inp[]=new int[25];
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Bucket Size: ");
cap= sc.nextInt();
System.out.print("Enter the Operation Rate: ");
oprate= sc.nextInt();
System.out.print("Enter the no. of seconds you want to stimulate: ");
nsec=sc.nextInt();
for(i=1;i<=nsec;i++)
{
System.out.print("Enter the size of the packet entering at "+ i + " sec: ");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Received | Packet Sent | Packet Left |Packet
Dropped|\n");
//System.out.println("------------------------------------------------------------------------\n");
for(i=1;i<=nsec;i++)
{
count+=inp[i];
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i);
System.out.print("\t\t"+inp[i]);
mini=min(count,oprate);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
drop=0;
System.out.println();
}
for(;count!=0;i++)
{
35
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,oprate);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}
OUTPUT:
36