0% found this document useful (0 votes)
119 views36 pages

CNLABFINAL (v2) PDF

The document describes four network simulation programs created using the Tcl scripting language: 1. A 3 node point-to-point network is created with UDP and TCP traffic between nodes. Performance is analyzed by measuring packets dropped at varying bandwidths. 2. Ping messages are transmitted between 5 nodes connected in a network. Packet drops are measured when bandwidth is reduced on one link. 3. An 8 node Ethernet LAN is simulated comparing the congestion window of TCP connections using Vegas and Reno algorithms. 4. A simple wireless ad hoc network is created by inputting the number of nodes. Tracing files are generated to analyze network performance.

Uploaded by

Maruti Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views36 pages

CNLABFINAL (v2) PDF

The document describes four network simulation programs created using the Tcl scripting language: 1. A 3 node point-to-point network is created with UDP and TCP traffic between nodes. Performance is analyzed by measuring packets dropped at varying bandwidths. 2. Ping messages are transmitted between 5 nodes connected in a network. Packet drops are measured when bandwidth is reduced on one link. 3. An 8 node Ethernet LAN is simulated comparing the congestion window of TCP connections using Vegas and Reno algorithms. 4. A simple wireless ad hoc network is created by inputting the number of nodes. Tracing files are generated to analyze network performance.

Uploaded by

Maruti Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

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.Scanner;
public class crc {

static int n,m,msb,i,j,k,g[],d[],z[],r[];


public static void main(String[] args)
{
Scanner s=new Scanner(System.in);

System.out.println("Enter no. of databits:");


n=s.nextInt();

System.out.println("Enter no. of generator bits:");


m=s.nextInt();

d=new int[m+n];
g=new int[n];

System.out.println("Enter databits:");
for(i=0;i<n;i++)
d[i]=s.nextInt();

System.out.println("Enter generator bits:");


for(i=0;i<m;i++)
g[i]=s.nextInt();
crcEval();

System.out.println("\nThe code data is:");


for(i=0;i<n+m-1;i++)
System.out.println(d[i]);
crcEval();
boolean t=true;
for(i=n;i<n+m-1;i++) {
if(r[i]==1)
t=false;
}
if(t)
System.out.println("\nNo error");
else
System.out.println("Error");
}

private static void crcEval()


{
r=new int[m+n];
for(i=0;i<m;i++)
r[i]=d[i];
z=new int[m];
for(i=0;i<n;i++) {
k=0;
msb=r[i];

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 no. of databits: 7

Enter no. of generator bits: 3

Enter databits: 1 0 1 0 1 0 1

Enter generator bits: 1 0 1

The code data is: 1 0 1 0 1 0 1 0 0

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

OUTPUT:

Enter the number of vertices: 4

Enter the Weighted matrix


0500
5034
0302
0420

Enter the source vertex: 2


2→1: 5
2→1: 0
2→1: 3
2→1: 4

29
PROGRAM 9: TCP
Client

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client


{
public static void main(String[] args) throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter file name:");
String fname=s.next();
Socket socket=new Socket("127.0.0.1",4000);

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;

public class Server


{
public static void main(String[] args) throws Exception
{
System.out.println("Server ready for communication");
ServerSocket serverSocket=new ServerSocket(4000);
Socket socket=serverSocket.accept();

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.*;

public class UDPClient {


public static void main(String[] args) throws Exception{
DatagramSocket ds=new DatagramSocket(4000);
byte data[]=new byte[1000];

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;

public class UDPServer {


public static void main(String[] args) throws Exception{
Scanner in = new Scanner(System.in);
DatagramSocket ds=new DatagramSocket();
String msg=in.nextLine();

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:

Enter message Enter the Message to be encrypted: hello

Enter value of p and q


5
7

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:

Enter the Bucket Size: 5

Enter the Operation Rate: 2

Enter the no. of seconds you want to stimulate: 5

Enter the size of the packet entering at 1 sec: 5

Enter the size of the packet entering at 2 sec: 4

Enter the size of the packet entering at 3 sec: 3

Enter the size of the packet entering at 4 sec: 0

Enter the size of the packet entering at 5 sec: 0

Second | Packet Received | Packet Sent | Packet Left |Packet Dropped|


1 |5 |2 |3 |0 |
2 |4 |2 |3 |2 |
3 |3 |2 |3 |1 |
4 |0 |2 |1 |0 |
5 |0 |1 |0 |0 |

36

You might also like