Sie sind auf Seite 1von 37

PROGRAM 1: 3 nodes point-to-point network

TCL

set ns [new Simulator] # ’ns’ is an object which is created within the ‘Simulator’ class with the help of ‘new’ operator

set f [open out1.tr w] # trace file to analyse the output

set nf [open out1.nam w] # animation file to visualize the output

$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

$ns color 1 "Blue"

$ns color 2 "Red"

proc finish {} {

global ns f nf

$ns flush-trace

close $f

close $nf

exec nam out1.nam &

exit 0

set n0 [$ns node] # ‘node’ is a subclass of ‘Simulator’ class, within which nodes are created

set n1 [$ns node]

set n2 [$ns node]

# set the label for each node

$n0 label "UDP Source"

$n1 label "TCP source"

$n2 label "TCP & UDP Dest."

$ns duplex-link $n0 $n1 1.25Mb 10ms DropTail # 7 parameters

$ns duplex-link $n1 $n2 1.25Mb 20ms DropTail # change bandwidth of both the links from 0.25 to 2.75Mb

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

1
$ns duplex-link-op $n1 $n2 orient down

$ns queue-limit $n1 $n2 20

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

$ns attach-agent $n0 $udp0

$udp0 set class_ 1

# application layer protocol; ‘CBR’ class is within ‘Traffic’ class which is within ‘Application class’ (which again is in
‘Simulator’ class)

set cbr0 [new Application/Traffic/CBR] # object cbr0 is attached to node0

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

set tcp0 [new Agent/TCP]

$ns attach-agent $n1 $tcp0

$tcp0 set class_ 2

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ftp0 set maxPkts_ 1000

set null0 [new Agent/Null]

$ns attach-agent $n2 $null0

set sink [new Agent/TCPSink]

$ns attach-agent $n2 $sink

$ns connect $udp0 $null0

$ns connect $tcp0 $sink

2
$ns at 0.5 "$cbr0 start"

$ns at 1.0 "$ftp0 start"

$ns at 4.0 "$ftp0 stop"

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run

AWK

BEGIN {

cbrPkt=0;

tcpPkt=0;

if(($1== "d")&&($5== "cbr")) {cbrPkt++;}

if(($1== "d")&&($5== "tcp")) {tcpPkt++;}

END {

printf ("No. of CBR Packets Dropped %d\n", cbrPkt);

printf( "No. of TCP Packets Dropped %d\n", tcpPkt);

XG

TitleText: Performance Analysis (Bandwidth vs Packet Dropped)

XUnitText: Bandwidth (in Mbps)

YUnitText: No. of Packet Dropped

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

set ns [new Simulator]

set tr [open out2.tr w]

set nf [open out2.nam w]

$ns trace-all $tr

$ns namtrace-all $nf

$ns color 1 "Blue"

$ns color 2 "Green"

$ns color 3 "Red"

$ns color 4 "Yellow"

proc finish {} {

global ns tr nf

$ns flush-trace

close $tr

close $nf

#exec nam out2.nam &

exit 0

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

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

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

5
$ns duplex-link $n2 $n3 2.0Mb 10ms DropTail # Change bandwidth from 0.1-0.2 to 2Mb

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

$ns duplex-link $n3 $n5 2Mb 10ms DropTail

$ns queue-limit $n2 $n3 10

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

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

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

$ns duplex-link-op $n3 $n4 orient right-up

$ns duplex-link-op $n3 $n5 orient right-down

set p0 [new Agent/Ping]

$ns attach-agent $n0 $p0

$p0 set class_ 1

set p1 [new Agent/Ping]

$ns attach-agent $n1 $p1

$p1 set class_ 2

set p4 [new Agent/Ping]

$ns attach-agent $n4 $p4

$p4 set class_ 3

set p5 [new Agent/Ping]

$ns attach-agent $n5 $p5

$p5 set class_ 4

$ns connect $p0 $p5

$ns connect $p1 $p4

$ns connect $p4 $p0

6
$ns connect $p5 $p1

proc sendPingPacket {} {

global ns p0 p1 p4 p5

set pinginterval 0.01

set now [$ns now]

puts "$now and $pinginterval"

$ns at $now "$p0 send"

$ns at $now "$p1 send"

$ns at $now "$p4 send"

$ns at $now "$p5 send"

$ns at [expr $now + $pinginterval] "sendPingPacket"

Agent/Ping instproc recv {from rtt} {

$self instvar node_

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

$from with round-trip-time $rtt ms"

$ns at 0.01 "sendPingPacket"

$ns rtmodel-at 3.0 down $n2 $n3

$ns rtmodel-at 5.0 up $n2 $n3

$ns at 10.0 "finish"

$ns run

AWK

BEGIN {

pingDrop=0;

7
if($1=="d") { pingDrop++ ; }

END {

printf("Total no of ping packets dropped due to congestion is = %d\n", pingDrop);

XG

TitleText: Performance Analysis

XUnitText: Bandwidth

YUnitText: No. of packets Dropped

"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

set ns [new Simulator]

set f1 [open out3.nam w]

set f2 [open out3.tr w]

$ns trace-all $f2

$ns namtrace-all $f1

LanRouter set debug_ 0

# Define color classes

$ns color 1 "Blue"

$ns color 2 "Red"

proc finish {} {

global ns f1 f2

$ns flush-trace

close $f1

close $f2

exec nam out3.nam &

exit 0

# Create nodes using for loop

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

set n$i [$ns node]

# Create links between nodes

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

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

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

$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 queue limit

$ns queue-limit $n0 $n3 20

# Make node 1 as FTP/TCP agent that uses VEGAS algorithm

set tcp1 [new Agent/TCP/Vegas]

$ns attach-agent $n1 $tcp1

# Make node 7 as TCP Sink for node 1

set sink1 [new Agent/TCPSink]

$ns attach-agent $n7 $sink1

# Connect node 1 and node 7

$ns connect $tcp1 $sink1

$tcp1 set class_ 1

$tcp1 set packetSize_ 55

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

# Create trace file that logs congestion for VEGAS

set tfile [open cwnd.tr w]

$tcp1 attach $tfile

$tcp1 trace cwnd_

10
# Make node 2 as FTP/TCP agent that uses RENO algorithm

set tcp2 [new Agent/TCP/Reno]

$ns attach-agent $n2 $tcp2

# Make node 8 as TCP Sink for node 2

set sink2 [new Agent/TCPSink]

$ns attach-agent $n8 $sink2

# Connect node 2 and node 8

$ns connect $tcp2 $sink2

$tcp2 set class_ 2

$tcp2 set packetSize_ 55

set ftp2 [new Application/FTP]

$ftp2 attach-agent $tcp2

# Create trace file that logs congestion for RENO

set tfile2 [open cwnd2.tr w]

$tcp2 attach $tfile2

$tcp2 trace cwnd_

$ns at 0.5 "$ftp1 start"

$ns at 1.0 "$ftp2 start"

$ns at 5.0 "$ftp2 stop"

$ns at 5.0 "$ftp1 stop"

$ns at 5.5 "finish"

$ns run

11
AWK

BEGIN {
}

{
if($6 == "cwnd_") {
printf("%f\t%f\n",$1,$7);
}
}

END {
}

XG

TitleText: Performance Analysis (Bandwidth vs Packet Dropped)

XUnitText: Time

YUnitText: Congestion Window

"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} {

error "Command: ns <ScriptName.tcl><Number_of_Nodes>"

exit 0

# Define the Wireless simulation options

set val(chan) Channel/WirelessChannel

set val(prop) Propagation/TwoRayGround

set val(ant) Antenna/OmniAntenna

set val(ll) LL

set val(ifq) Queue/DropTail/PriQueue

set val(ifqlen) 50

set val(netif) Phy/WirelessPhy

set val(mac) Mac/802_11

set val(rp) AODV

set val(nn) [lindex $argv 0]

set opt(x) 750

set opt(y) 750

set val(stop) 100

# create instance for simulator

set ns [new Simulator]

# Open trace file and nam file

set trfd [open Wireless.tr w]

set namfd [open Wireless.nam w]

13
# Tracing nam and trace file

$ns trace-all $trfd

$ns namtrace-all-wireless $namfd $opt(x) $opt(y)

# Create topography object

set topo [new Topography]

$topo load_flatgrid $opt(x) $opt(y)

# Create god to store hop info

set god_ [create-god $val(nn)]

# Configure the nodes

$ns node-config -adhocRouting $val(rp) \

-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

# Create n number of nodes

for {set i 0} {$i< $val(nn)} {incr i} {

set n($i) [$ns node]

14
# Location fixing of the nodes

for {set i 0} {$i< $val(nn)} {incr i} {

set XX [expr rand()*750]

set YY [expr rand()*750]

$n($i) set X_ $XX

$n($i) set Y_ $YY

# Size of the node

for {set i 0} {$i< $val(nn)} {incr i} {

$ns initial_node_pos $n($i) 30

# Attach agents

set tcp1 [new Agent/TCP]

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

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

set sink1 [new Agent/TCPSink]

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

$ns connect $tcp1 $sink1

$ns at 0.0 "destination"

proc destination {} {

global ns val n

set now [$ns now]

set time 5.0

for {set i 0} {$i< $val(nn)} {incr i} {

15
set XX [expr rand()*750]

set YY [expr rand()*750]

$ns at [expr $now + $time] "$n($i) setdest $XX $YY 20.0"

$ns at [expr $now + $time] "destination"

# tell nodes to return to the initial components

for {set i 0} {$i< $val(nn)} {incr i} {

$ns at $val(stop) "$n($i) reset"

$ns at 5.0 "$ftp1 start"

# Stop nam and simulation

$ns at $val(stop) "$ns nam-end-wireless $val(stop)"

$ns at $val(stop) "stop"

proc stop {} {

global ns trfd namfd

close $trfd

close $namfd

exec nam Wireless.nam &

#exec awk -f 6.awk Wireless.tr &

exit 0

$ns run

16
AWK

BEGIN {

PacketRcvd=0;

Throughput=0.0;

if(($1=="r") && ($3=="_3_") && ($4=="AGT") && ($7=="tcp") && ($8>1000))

PacketRcvd++;

END {

Throughput=((PacketRcvd*1000*8)/(95.0*1000000));

printf( "the throughput is:%f\n",Throughput);

// Formula to calculate throughput

// throughput = (packet recieved * Packet size) / Transmission time (Microsec)

//Throughput is measured in bps so multiply it by 8

XG

TitleText: Performance Analysis (Nodes vs Throughput)

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

50 0.587536 100 0.504337

17
PROGRAM 5: GSM
TCL
# General Parameters

set opt(ecn) 0;

set opt(window) 30;

# Topology

set opt(type) gsm; # type of link:

# AQM parameters

set opt(minth) 5;

set opt(maxth) 10;

set opt(adaptive) 1; # 1 for Adaptive RED, 0 for plain RED

# Default downlink bandwidth in bps (change from 10 to 100Kbps)

set bwDL(gsm) 100000

# Default uplink bandwidth in bps (change from 10 to 100Kbps)

set bwUL(gsm) 100000

# Default downlink propagation delay in seconds

set propDL(gsm) .500

# Default uplink propagation delay in seconds

set propUL(gsm) .500

# Default buffer size in packets

set buf(gsm) 10

set ns [new Simulator]

set tf [open out.tr w]

set nf [open out1.nam w]

18
$ns trace-all $tf

$ns namtrace-all $nf

set nodes(s) [$ns node]

set nodes(bs1) [$ns node]

set nodes(msc) [$ns node]

set nodes(bs2) [$ns node]

set nodes(d) [$ns node]

proc cell_topo {} {

global ns nodes

$ns duplex-link $nodes(s) $nodes(bs1) 3Mbps 10ms DropTail

$ns duplex-link $nodes(bs1) $nodes(msc) 1Mbps 1ms RED

$ns duplex-link $nodes(msc) $nodes(bs2) 1Mbps 1ms RED

$ns duplex-link $nodes(bs2) $nodes(d) 3Mbps 50ms DropTail

proc set_link_params {t} {

global ns nodes bwUL bwDL propUL propDL buf

$ns bandwidth $nodes(bs1) $nodes(msc) $bwDL($t) simplex

$ns bandwidth $nodes(msc) $nodes(bs1) $bwUL($t) simplex

$ns bandwidth $nodes(bs2) $nodes(msc) $bwDL($t) simplex

$ns bandwidth $nodes(msc) $nodes(bs2) $bwUL($t) simplex

$ns delay $nodes(bs1) $nodes(msc) $propDL($t) simplex

$ns delay $nodes(msc) $nodes(bs1) $propDL($t) simplex

$ns delay $nodes(bs2) $nodes(msc) $propDL($t) simplex

$ns delay $nodes(msc) $nodes(bs2) $propDL($t) simplex

$ns queue-limit $nodes(bs1) $nodes(msc) $buf($t)

$ns queue-limit $nodes(msc) $nodes(bs1) $buf($t)

$ns queue-limit $nodes(bs2) $nodes(msc) $buf($t)

$ns queue-limit $nodes(msc) $nodes(bs2) $buf($t)

19
# RED and TCP parameters

Queue/RED set summarystats_ true

Queue/DropTail set summarystats_ true

Queue/RED set adaptive_ $opt(adaptive)

Queue/RED set q_weight_ 0.0

Queue/RED set thresh_ $opt(minth)

Queue/RED set maxthresh_ $opt(maxth)

Queue/DropTail set shrink_drops_ true

Agent/TCP set ecn_ $opt(ecn)

Agent/TCP set window_ $opt(window)

DelayLink set avoidReordering_ true

# Create topology

switch $opt(type) {

gsm - gprs - umts {cell_topo}

set_link_params $opt(type)

# Set up forward TCP connection

set tcp1 [$ns create-connection TCP/Sack1 $nodes(s) TCPSink/Sack1 $nodes(d) 0]

set ftp1 [[set tcp1] attach-app FTP]

$ns at 0.5 "$ftp1 start"

proc stop {} {

global nodes ns opt nf tf

$ns flush-trace

close $nf

close $tf

#exec nam out1.nam &

#exec awk -f pg5.awk out1.nam &

exit 0

20
}

$ns at 100 "stop"

$ns run

AWK

BEGIN {

PacketRcvd=0;

Throughput=0.0;

if(($1=="r") && ($5=="tcp") && ($10=4.0))

PacketRcvd++;

END {

Throughput=((PacketRcvd*1000*8)/(95.0*1000000));

printf("packet received:%f\n", PacketRcvd);

printf( "the throughput is:%f\n",Throughput);

XG

TitleText: Performance Analysis

XUnitText: Packet

YUnitText: Throughput

"GSM"

10 0.035874 40 0.130863 70 0.185432 100 0.308126

20 0.068211 50 0.155200 80 0.182821

30 0.100463 60 0.174316 90 0.285642

21
PROGRAM 6: CDMA
TCL
# General Parameters

set opt(ecn) 0;

set opt(window) 30;

# Topology

set opt(type) gsm; # type of link:

# AQM parameters

set opt(minth) 5;

set opt(maxth) 10;

set opt(adaptive) 1; # 1 for Adaptive RED, 0 for plain RED

# Default downlink bandwidth in bps (change from 10 to 30Kbps)

set bwDL(gsm) 100000

# Default uplink bandwidth in bps (change from 10 to 30Kbps)

set bwUL(gsm) 100000

# Default downlink propagation delay in seconds

set propDL(gsm) .500

# Default uplink propagation delay in seconds

set propUL(gsm) .500

# Default buffer size in packets

set buf(gsm) 10

set ns [new Simulator]

set tf [open out.tr w]

set nf [open out1.nam w]

22
$ns trace-all $tf

$ns namtrace-all $nf

set nodes(s) [$ns node]

set nodes(bs1) [$ns node]

set nodes(msc) [$ns node]

set nodes(bs2) [$ns node]

set nodes(d) [$ns node]

proc cell_topo {} {

global ns nodes

$ns duplex-link $nodes(s) $nodes(bs1) 3Mbps 10ms DropTail

$ns duplex-link $nodes(bs1) $nodes(msc) 1Mbps 1ms RED

$ns duplex-link $nodes(msc) $nodes(bs2) 1Mbps 1ms RED

$ns duplex-link $nodes(bs2) $nodes(d) 3Mbps 50ms DropTail

proc set_link_params {t} {

global ns nodes bwUL bwDL propUL propDL buf

$ns bandwidth $nodes(bs1) $nodes(msc) $bwDL($t) simplex

$ns bandwidth $nodes(msc) $nodes(bs1) $bwUL($t) simplex

$ns bandwidth $nodes(bs2) $nodes(msc) $bwDL($t) simplex

$ns bandwidth $nodes(msc) $nodes(bs2) $bwUL($t) simplex

$ns delay $nodes(bs1) $nodes(msc) $propDL($t) simplex

$ns delay $nodes(msc) $nodes(bs1) $propDL($t) simplex

$ns delay $nodes(bs2) $nodes(msc) $propDL($t) simplex

$ns delay $nodes(msc) $nodes(bs2) $propDL($t) simplex

$ns queue-limit $nodes(bs1) $nodes(msc) $buf($t)

$ns queue-limit $nodes(msc) $nodes(bs1) $buf($t)

$ns queue-limit $nodes(bs2) $nodes(msc) $buf($t)

$ns queue-limit $nodes(msc) $nodes(bs2) $buf($t)

23
# RED and TCP parameters

Queue/RED set summarystats_ true

Queue/DropTail set summarystats_ true

Queue/RED set adaptive_ $opt(adaptive)

Queue/RED set q_weight_ 0.0

Queue/RED set thresh_ $opt(minth)

Queue/RED set maxthresh_ $opt(maxth)

Queue/DropTail set shrink_drops_ true

Agent/TCP set ecn_ $opt(ecn)

Agent/TCP set window_ $opt(window)

DelayLink set avoidReordering_ true

# Create topology

switch $opt(type) {

gsm - gprs - umts {cell_topo}

set_link_params $opt(type)

# Set up forward TCP connection

set tcp1 [$ns create-connection TCP/Sack1 $nodes(s) TCPSink/Sack1 $nodes(d) 0]

set ftp1 [[set tcp1] attach-app FTP]

$ns at 0.5 "$ftp1 start"

proc stop {} {

global nodes ns opt nf tf

$ns flush-trace

close $nf

close $tf

#exec nam out1.nam &

#exec awk -f pg5.awk out1.nam &

24
exit 0

$ns at 100 "stop"

$ns run

AWK

BEGIN {

PacketRcvd=0;

Throughput=0.0;

if(($1=="r") && ($5=="tcp") && ($10=4.0))

PacketRcvd++;

END {

Throughput=((PacketRcvd*1000*8)/(95.0*1000000));

printf("packet received:%f\n", PacketRcvd);

printf( "the throughput is:%f\n",Throughput);

XG

TitleText: Performance Analysis


XUnitText: Bandwidth (kbps)
YUnitText: Throughput

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

public static void main(String args[])


{
int a[]=new int[100];
int b[]=new int[100];
int len,k;
crc ob=new crc();
System.out.println("Enter the length of Data Frame: ");
Scanner sc=new Scanner(System.in);
len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{
a[len++]=0;
}
k=len-16;
for(int i=0;i<len;i++)
{
b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: ");
for(int i=0;i<len;i++)
{

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

public static void main(String[ ] args)


{
int n=0,s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = sc.nextInt();
int A[][] = new int[n+1][n+1];
System.out.println("Enter the Weighted matrix");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
A[i][j]=sc.nextInt();
if(i==j)
{
A[i][j]=0;
continue;
}
if(A[i][j]==0)
{
A[i][j]=MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
s=sc.nextInt();
prog8 b = new prog8(n);
b.shortest(s,A);
sc.close();
}
}

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

Das könnte Ihnen auch gefallen