Beruflich Dokumente
Kultur Dokumente
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.*;
class crc
{
void div(int a[],int k)
{
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
count=0;
}
}
}
26
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter the Reveived Data: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("error in data");
else
System.out.println("no error");
}
}
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]);
}
}
29
PROGRAM 9: TCP
Client
import java.net.*;
import java.io.*;
public class cc
{
public static void main( String args[ ] ) throws Exception
{
Socket sock = new Socket( "127.0.0.1", 4000);
// reading the file name from keyboard. Uses input stream
System.out.print("Enter the file name");
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
String fname = keyRead.readLine();
// sending the file name to server. Uses PrintWriter
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);
// receiving the contents from server. Uses input stream
InputStream istream = sock.getInputStream();
BufferedReader socketRead = new BufferedReader(new
InputStreamReader(istream));
String str;
while((str = socketRead.readLine()) != null) // reading line-by-line
{
System.out.println(str);
}
pwrite.close(); socketRead.close(); keyRead.close();
}
}
Server
import java.net.*;
import java.io.*;
public class cs
{
public static void main(String args[]) throws Exception
{
// establishing the connection with the server
ServerSocket sersock = new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket sock = sersock.accept();
// binding with port: 4000
System.out.println("Connection is successful and wating for chatting");
// reading the file name from client
InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new
InputStreamReader(istream));
30
String fname = fileRead.readLine( );
// reading file contents
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
// keeping output stream ready to send the contents
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
while((str = contentRead.readLine()) != null) // reading line-by-line from file
{
pwrite.println(str);
// sending each line to client
}
sock.close(); sersock.close();
// closing network sockets
pwrite.close(); fileRead.close(); contentRead.close();
}
}
31
PROGRAM 10: UDP
Client
import java.io.*;
import java.net.*;
class udpc
{
public static void main(String[] a) throws IOException
{
int i =2000;
while(true)
{
byte buf[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(i);
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String str2=new String(dp.getData(),0,dp.getLength());
System.out.println("Server:"+str2);
System.out.println("**************************");
i=i+1;
}
}
}
Server
import java.io.*;
import java.net.*;
class udps
{
public static void main(String [] a) throws IOException
{
int i =2000;
String fooString1 = new String("exit");
while(true)
{
InetAddress clientIP=InetAddress.getLocalHost();
int clientPort=i;
byte buf[]=new byte[1024];
DatagramSocket ds=new DatagramSocket();
BufferedReader dis =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Server is Running......");
String str1=new String(dis.readLine());
if(str1.equals(fooString1))
{
ds.close();
break;
}
else
{
32
buf=str1.getBytes();
DatagramPacket dp=new
DatagramPacket(buf,str1.length(),clientIP,clientPort);
ds.send(dp);
i=i+1;
}
ds.close();
}
}
}
33
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++)
34
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;
}
}
35
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,process;
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: ");
process= 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 Recieved | 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,process);
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();
}
36
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}
37