FInal Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Computer Network Laboratory

PART-A
1. Implement three nodes point – to – point network with duplex links between them. Set the
queue size, vary the bandwidth and find the number of packets dropped.

set ns [ new Simulator ]

set tf [ open lb1.tr w ]

$ns trace-all $tf

set nf [ open lb1.nam w ]

$ns namtrace-all $nf

# The below code is used to create the nodes.

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#This is used to give color to the packets.

$ns color 1 "red"

$ns color 2 "blue"

$n0 label "Source/udp0"

$n1 label "Source/udp1"

$n2 label "Router"

$n3 label "Destination/Null"

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

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

$ns duplex-link $n2 $n3 1Mb 300ms DropTail

#The below code is used to set the queue size b/w the nodes

$ns set queue-limit $n0 $n2 10

Dept of CSE,SCET Belagavi Page 1


Computer Network Laboratory

$ns set queue-limit $n1 $n2 10

$ns set queue-limit $n2 $n3 5

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

set null3 [new Agent/Null]

$ns attach-agent $n3 $null3

set udp1 [new Agent/UDP]

$ns attach-agent $n1 $udp1

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp1

#The below code sets the udp0 packets to red and udp1

#packets to blue color

$udp0 set class_ 1

$udp1 set class_ 2

$ns connect $udp0 $null3

$ns connect $udp1 $null3

#The below code is used to set the packet size to 500

$cbr1 set packetSize_ 500Mb

#The below code is used to set the interval of the packets,

#i.e., Data rate of the packets. if the data rate is high

Dept of CSE,SCET Belagavi Page 2


Computer Network Laboratory

#then packets drops are high.

$cbr1 set interval_ 0.005

proc finish { } {

global ns nf tf

$ns flush-trace

exec nam lb1.nam &

close $tf

close $nf

exit 0

$ns at 0.1 "$cbr0 start"

$ns at 0.1 "$cbr1 start"

$ns at 10.0 "finish"

$ns run

For awk program

BEGIN{

count=0;

if($1=="d")

count++

END{

printf("The Total no of Packets Dropped due to Congestion :%d\n\n", count)

OUTPUT:

Dept of CSE,SCET Belagavi Page 3


Computer Network Laboratory

[root@localhost~]# ns lb1.tcl [root@localhost~]#gedit lb1.tr

Topology Output

Trace File

[root@localhost~]# awk -f lb1.awk lb1.tr

Dept of CSE,SCET Belagavi Page 4


Computer Network Laboratory

2. Implement transmission of ping messages/trace route over a network topology consisting


of 6 nodes and find the number of packets dropped due to congestion.

set ns [new Simulator]

set tf [open lb2.tr w]

$ns trace-all $tf

set nf [open lb2.nam w]

$ns namtrace-all $nf

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]

$n0 label "Ping0"

$n4 label "Ping4"

$n5 label "Ping5"

$n6 label "Ping6"

$n2 label "Router"

$ns color 1 "red"

$ns color 2 "green"

$ns duplex-link $n0 $n2 100Mb 300ms DropTail

$ns duplex-link $n2 $n6 1Mb 300ms DropTail

$ns duplex-link $n5 $n2 100Mb 300ms DropTail

$ns duplex-link $n2 $n4 1Mb 300ms DropTail

$ns duplex-link $n3 $n2 1Mb 300ms DropTail

$ns duplex-link $n1 $n2 1Mb 300ms DropTail

$ns queue-limit $n0 $n2 5

$ns queue-limit $n2 $n6 2

Dept of CSE,SCET Belagavi Page 5


Computer Network Laboratory

$ns queue-limit $n2 $n4 3

$ns queue-limit $n5 $n2 5

#The below code is used to connect between the ping agents

#to the node n0, n4 , n5 and n6.

set ping0 [new Agent/Ping]

$ns attach-agent $n0 $ping0

set ping4 [new Agent/Ping]

$ns attach-agent $n4 $ping4

set ping5 [new Agent/Ping]

$ns attach-agent $n5 $ping5

set ping6 [new Agent/Ping]

$ns attach-agent $n6 $ping6

$ping0 set packetSize_ 50000

$ping0 set interval_ 0.0001

$ping5 set packetSize_ 60000

$ping5 set interval_ 0.00001

$ping0 set class_ 1

$ping5 set class_ 2

$ns connect $ping0 $ping4

$ns connect $ping5 $ping6

#The below function is executed when the ping agent receives

#a reply from the destination

Agent/Ping instproc recv {from rtt} {

$self instvar node_

puts " The node [$node_ id] received an reply from $from

with round trip time of $rtt"

Dept of CSE,SCET Belagavi Page 6


Computer Network Laboratory

proc finish {} {

global ns nf tf

exec nam lb2.nam &

$ns flush-trace

close $tf

close $nf

exit 0

#The below code makes the link down(failure) at 0.9 from n2

#to n6 and when the time becomes 1.5 the link between n2 to

#n6 is enabled.

$ns rtmodel-at 0.9 down $n2 $n6

$ns rtmodel-at 1.5 up $n2 $n6

$ns at 0.1 "$ping0 send"

$ns at 0.2 "$ping0 send"

$ns at 0.3 "$ping0 send"

$ns at 0.4 "$ping0 send"

$ns at 0.5 "$ping0 send"

$ns at 0.6 "$ping0 send"

$ns at 0.7 "$ping0 send"

$ns at 0.8 "$ping0 send"

$ns at 0.9 "$ping0 send"

$ns at 1.0 "$ping0 send"

$ns at 1.1 "$ping0 send"

$ns at 1.2 "$ping0 send"

$ns at 1.3 "$ping0 send"

$ns at 1.4 "$ping0 send"

$ns at 1.5 "$ping0 send"

Dept of CSE,SCET Belagavi Page 7


Computer Network Laboratory

$ns at 1.6 "$ping0 send"

$ns at 1.7 "$ping0 send"

$ns at 1.8 "$ping0 send"

$ns at 0.1 "$ping5 send"

$ns at 0.2 "$ping5 send"

$ns at 0.3 "$ping5 send"

$ns at 0.4 "$ping5 send"

$ns at 0.5 "$ping5 send"

$ns at 0.6 "$ping5 send"

$ns at 0.7 "$ping5 send"

$ns at 0.8 "$ping5 send"

$ns at 0.9 "$ping5 send"

$ns at 1.0 "$ping5 send"

$ns at 1.1 "$ping5 send"

$ns at 1.2 "$ping5 send"

$ns at 1.3 "$ping5 send"

$ns at 1.4 "$ping5 send"

$ns at 1.5 "$ping5 send"

$ns at 1.6 "$ping5 send"

$ns at 1.7 "$ping5 send"

$ns at 1.8 "$ping5 send"

$ns at 5.0 "finish"

$ns run

For awk Program

BEGIN{

Dept of CSE,SCET Belagavi Page 8


Computer Network Laboratory

count=0;

if($1=="d")

count++

END{

printf("The Total no of Packets Dropped due to Congestion:%d\n", count)

OUTPUT:

[root@localhost~]# ns lb2.tcl [root@localhost~]# gedit lb2.tr

Topology Output

Trace File

Dept of CSE,SCET Belagavi Page 9


Computer Network Laboratory

[root@localhost~]# awk –f lb2.awk lb2.tr

3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.

Dept of CSE,SCET Belagavi Page 10


Computer Network Laboratory

set ns [new Simulator]

set tf [open lb3.tr w]

$ns trace-all $tf

set nf [open lb3.nam w]

$ns namtrace-all $nf

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

$ns make-lan "$n0 $n1 $n2 $n3" 10mb 10ms LL Queue/DropTail Mac/802_3

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

set sink3 [new Agent/TCPSink]

$ns attach-agent $n3 $sink3

$ns connect $tcp0 $sink3

set tcp2 [new Agent/TCP]

$ns attach-agent $n2 $tcp2

set ftp2 [new Application/FTP]

$ftp2 attach-agent $tcp2

set sink2 [new Agent/TCPSink]

$ns attach-agent $n1 $sink2

$ns connect $tcp2 $sink2

######To trace the congestion window##########

set file1 [open file1.tr w]

Dept of CSE,SCET Belagavi Page 11


Computer Network Laboratory

$tcp0 attach $file1

$tcp0 trace cwnd_

$tcp0 set maxcwnd_ 10

set file2 [open file2.tr w]

$tcp2 attach $file2

$tcp2 trace cwnd_

proc finish { } {

global nf tf ns

$ns flush-trace

exec nam lb3.nam &

close $nf

close $tf

exit 0

$ns at 0.1 "$ftp0 start"

$ns at 1.5 "$ftp0 stop"

$ns at 2 "$ftp0 start"

$ns at 3 "$ftp0 stop"

$ns at 0.2 "$ftp2 start"

$ns at 2 "$ftp2 stop"

$ns at 2.5 "$ftp2 start"

$ns at 4 "$ftp2 stop"

$ns at 5.0 "finish"

$ns run

Dept of CSE,SCET Belagavi Page 12


Computer Network Laboratory

For AWK Program

BEGIN{

if($6=="cwnd_")

printf("%f \t %f \n", $1,$7);

END{

puts "DONE"

OUTPUT:

[root@localhost~]# ns lb3.tcl

[root@localhost~]# awk –f lb3.awk file1.tr > a1


[root@localhost~]# awk –f lb3.awk file2.tr > a2
[root@localhost~]# xgraph a1 a2\

Dept of CSE,SCET Belagavi Page 13


Computer Network Laboratory

4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.

Dept of CSE,SCET Belagavi Page 14


Computer Network Laboratory

set ns [new Simulator]

set tf [open lb4.tr w]

$ns trace-all $tf

set topo [new Topography]

$topo load_flatgrid 1000 1000

set nf [open lb4.nam w]

$ns namtrace-all-wireless $nf 1000 1000

$ns node-config -adhocRouting DSDV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail \

-ifqLen 50 \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-propType Propagation/TwoRayGround \

-antType Antenna/OmniAntenna \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON

create-god 3

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

$n0 label "tcp0"

$n1 label "sink1/tcp1"

$n2 label "sink2"

Dept of CSE,SCET Belagavi Page 15


Computer Network Laboratory

#The below code is used to give the initial node positions.

$n0 set X_ 50

$n0 set Y_ 50

$n0 set Z_ 0

$n1 set X_ 100

$n1 set Y_ 100

$n1 set Z_ 0

$n2 set X_ 600

$n2 set Y_ 600

$n2 set Z_ 0

$ns at 0.1 "$n0 setdest 50 50 15"

$ns at 0.1 "$n1 setdest 100 100 25"

$ns at 0.1 "$n2 setdest 600 600 25"

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

set sink1 [new Agent/TCPSink]

$ns attach-agent $n1 $sink1

$ns connect $tcp0 $sink1

set tcp1 [new Agent/TCP]

$ns attach-agent $n1 $tcp1

set ftp1 [new Application/FTP]

Dept of CSE,SCET Belagavi Page 16


Computer Network Laboratory

$ftp1 attach-agent $tcp1

set sink2 [new Agent/TCPSink]

$ns attach-agent $n2 $sink2

$ns connect $tcp1 $sink2

$ns at 5 "$ftp0 start"

$ns at 5 "$ftp1 start"

#The below code is used to provide the node movements.

$ns at 100 "$n1 setdest 550 550 15"

$ns at 190 "$n1 setdest 70 70 15"

proc finish {} {

global ns nf tf

$ns flush-trace

exec nam lb4.nam &

close $tf

exit 0

$ns at 250 "finish"

$ns run

For AWK Program

BEGIN{

count1=0

count2=0

Dept of CSE,SCET Belagavi Page 17


Computer Network Laboratory
pack1=0

pack2=0

time1=0

time2=0

if($1=="r"&&$3=="_1_"&&$4=="AGT")

count1++

pack1=pack1+$8

time1=$2

if($1=="r"&&$3=="_2_"&&$4=="AGT")

count2++

pack2=pack2+$8

time2=$2

END{

printf("The Throughput from n0 to n1: %fMbps\n",

((count1*pack1*8)/(time1*1000000)))

printf("The Throughput from n1 to n2: %f Mbps",

((count2* pack2 * 8) /(time2*1000000)))

OUTPUT:

[root@localhost~]# ns lb4.tcl

Dept of CSE,SCET Belagavi Page 18


Computer Network Laboratory

Node 1 and 2 are communicating Node 2 is moving towards node 3

Node 2 is coming back from node 3 towards node1

Dept of CSE,SCET Belagavi Page 19


Computer Network Laboratory

[root@localhost~]# awk –f lb4.awk lb4.tr [root@localhost~]# gedit lab4.tr

Trace File

Part B
1. Write a program for error detecting code using CRC-CCITT (16- bits).

import java.io.*;

class Crc

Dept of CSE,SCET Belagavi Page 20


Computer Network Laboratory

public static void main(String args[]) throws IOException

BufferedReader br=new BufferedReader(newInputStreamReader(System.in));

int[ ] data;

int[ ]div;

int[ ]divisor;

int[ ]rem;

int[ ] crc;

int data_bits, divisor_bits, tot_length;

System.out.println("Enter number of data bits : ");

data_bits=Integer.parseInt(br.readLine());

data=new int[data_bits];

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

for(int i=0; i<data_bits; i++)

data[i]=Integer.parseInt(br.readLine());

System.out.println("Enter number of bits in divisor : ");

divisor_bits=Integer.parseInt(br.readLine());

divisor=new int[divisor_bits];

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

for(int i=0; i<divisor_bits; i++)

divisor[i]=Integer.parseInt(br.readLine());

tot_length=data_bits+divisor_bits;

div=new int[tot_length];

rem=new int[tot_length];

crc=new int[tot_length];

Dept of CSE,SCET Belagavi Page 21


Computer Network Laboratory

/*------------------ CRC GENERATION-----------------------*/

for(int i=0;i<data.length;i++)

div[i]=data[i];

System.out.print("Dividend (after appending 0's) are : ");

for(int i=0; i< div.length; i++)

System.out.print(div[i]);

System.out.println();

for(int j=0; j<div.length; j++)

rem[j] = div[j];

rem=divide(div, divisor, rem);

for(int i=0;i<div.length;i++)

//append dividend and ramainder

crc[i]=(div[i]^rem[i]);

System.out.println();

System.out.println("CRC code : ");

for(int i=0;i<crc.length;i++)

System.out.print(crc[i]);

/*-------------------ERROR DETECTION---------------------*/

System.out.println();

System.out.println("Enter CRC code of "+tot_length+" bits : ");

for(int i=0; i<crc.length; i++)

crc[i]=Integer.parseInt(br.readLine());

Dept of CSE,SCET Belagavi Page 22


Computer Network Laboratory

for(int j=0; j<crc.length; j++)

rem[j] = crc[j];

rem=divide(crc, divisor, rem);

for(int i=0; i< rem.length; i++)

if(rem[i]!=0)

System.out.println("Error");

break;

if(i==rem.length-1)

System.out.println("No Error");

System.out.println("THANK YOU.... :)");

static int[] divide(int div[],int divisor[], int rem[])

int cur=0;

while(true)

for(int i=0;i<divisor.length;i++)

rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)

cur++;

if((rem.length-cur)<divisor.length)

Dept of CSE,SCET Belagavi Page 23


Computer Network Laboratory

break;

return rem;

OUTPUT:

2. Write a program to find the shortest path between vertices using bellman-ford algorithm.

import java.util.Scanner;

public class BellmanFord

Dept of CSE,SCET Belagavi Page 24


Computer Network Laboratory

private int D[];

private int num_ver;

public static final int MAX_VALUE = 999;

public BellmanFord(int num_ver)

this.num_ver = num_ver;

D = new int[num_ver + 1];

public void BellmanFordEvaluation(int source, int A[][])

for (int node = 1; node <= num_ver; node++)

D[node] = MAX_VALUE;

D[source] = 0;

for (int node = 1; node <= num_ver - 1; node++)

for (int sn = 1; sn <= num_ver; sn++)

for (int dn = 1; dn <= num_ver; dn++)

if (A[sn][dn] != MAX_VALUE)

if (D[dn] > D[sn]+ A[sn][dn])

D[dn] = D[sn] + A[sn][dn];

Dept of CSE,SCET Belagavi Page 25


Computer Network Laboratory

for (int sn = 1; sn <= num_ver; sn++)

for (int dn = 1; dn <= num_ver; dn++)

if (A[sn][dn] != MAX_VALUE)

if (D[dn] > D[sn]+ A[sn][dn])

System.out.println("The Graph contains negative egde cycle");

for (int vertex = 1; vertex <= num_ver; vertex++)

System.out.println("distance of source " + source + " to "+vertex + " is "+D[vertex]);

public static void main(String[ ] args)

int num_ver = 0;

int source;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of vertices");

num_ver = scanner.nextInt();

int A[][] = new int[num_ver + 1][num_ver + 1];

System.out.println("Enter the adjacency matrix");

for (int sn = 1; sn <= num_ver; sn++)

Dept of CSE,SCET Belagavi Page 26


Computer Network Laboratory

for (int dn = 1; dn <= num_ver; dn++)

A[sn][dn] = scanner.nextInt();

if (sn == dn)

A[sn][dn] = 0;

continue;

if (A[sn][dn] == 0)

A[sn][dn] = MAX_VALUE;

System.out.println("Enter the source vertex");

source = scanner.nextInt();

BellmanFord b = new BellmanFord (num_ver);

b.BellmanFordEvaluation(source, A);

scanner.close();

OUTPUT 1:

Dept of CSE,SCET Belagavi Page 27


Computer Network Laboratory

OUTPUT 2:

3.a) Using TCP/IP sockets, write a client – server program to make the client send the file
name and to make the server send back the contents of the requested file if present import

Dept of CSE,SCET Belagavi Page 28


Computer Network Laboratory

java.net.*;

import java.io.*;

public class client

public static void main(String[] args) throws Exception

Socket sock=new Socket("127.0.0.1",4000);

System.out.print("Enter the file name : ");

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

String fname=keyRead.readLine();

OutputStream Ostream=sock.getOutputStream();

PrintWriter pwrite=new PrintWriter(Ostream,true);

pwrite.println(fname);

InputStream istream=sock.getInputStream();

BufferedReader socketRead=new BufferedReader(new InputStreamReader(istream));

String str;

while((str=socketRead.readLine())!=null)

System.out.println(str);

pwrite.close();

socketRead.close();

keyRead.close();

3.b) Using TCP/IP sockets, write a client – server program to make the client send the file
name and to make the server send back the contents of the requested file if present.

import java.net.*;

import java.io.*;

Dept of CSE,SCET Belagavi Page 29


Computer Network Laboratory

public class server

public static void main(String[] args) throws Exception

ServerSocket sersock=new ServerSocket(4000);

System.out.println("server ready for connection");

Socket sock=sersock.accept();

System.out.println("Connection is successfull!!! and waiting for the file name ");

InputStream istream=sock.getInputStream();

BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));

String fname=fileRead.readLine();

BufferedReader contentRead =new BufferedReader(new FileReader(fname));

OutputStream ostream= sock.getOutputStream();

PrintWriter pwrite =new PrintWriter(ostream,true);

String str;

while((str=contentRead.readLine())!=null)

pwrite.println(str);

sock.close();

sersock.close();

pwrite.close();

fileRead.close();

contentRead.close();

Dept of CSE,SCET Belagavi Page 30


Computer Network Laboratory

OUTPUT:

At Server Side At Client Side

4.A) Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.

import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception

Dept of CSE,SCET Belagavi Page 31


Computer Network Laboratory

{
BufferedReader inFromUser = new BufferedReader(newInputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress= InetAddress.getByName("localhost");

byte[] sendData = new byte[1024];


byte[] receiveData = new byte[1024];

String sentence = inFromUser.readLine();


sendData =sentence.getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData,sendData.length,IPAddress,9876);

clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());


System.out.println("From server:"+modifiedSentence);
clientSocket.close();
}
}

4. B)Write a program on datagram socket for client/server to display the messages on


client side, typed at the server side.

import java.io.*;

import java.net.*;

public class UDPServer

Dept of CSE,SCET Belagavi Page 32


Computer Network Laboratory

public static void main(String args[])throws Exception

DatagramSocket serverSocket = new DatagramSocket(9876);

BufferedReader inFormUser = new BufferedReader(new InputStreamReader(System.in));

byte[] receiveData= new byte[1024];

byte[] sendData =new byte[1024];

while(true)

DatagramPacket receivePacket = new


DatagramPacket(receiveData,receiveData.length);

serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData());

System.out.println(" Received: "+sentence);

InetAddress IPAddress=receivePacket.getAddress();

int port =receivePacket.getPort();

String sentence1 = inFormUser.readLine();

sendData=sentence1.getBytes();

DatagramPacket sendPacket = new


DatagramPacket(sendData,sendData.length,IPAddress,port);

serverSocket.send(sendPacket);

OUTPUT:

Dept of CSE,SCET Belagavi Page 33


Computer Network Laboratory

At ServerSide At Client Side

5. Write a program for simple RSA algorithm to encrypt and decrypt the data.

import java.util.*;

import java.io.*;

public class rsa

Dept of CSE,SCET Belagavi Page 34


Computer Network Laboratory

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

Dept of CSE,SCET Belagavi Page 35


Computer Network Laboratory

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

encrypted[i] =(encrypted[i]*nummes[i])%n;

System.out.print(" Encrypted message: ");

for(i=0;i<nofelem;i++)

System.out.print(encrypted[i]);

System.out.print((char)(encrypted[i]+96));

for(i=0;i<nofelem;i++)

Dept of CSE,SCET Belagavi Page 36


Computer Network Laboratory

decrypted[i]=1;

for(j=0;j<d;j++)

decrypted[i]=(decrypted[i]*encrypted[i])%n;

System.out.print("\nDecrypted message: ");

for(i=0;i<nofelem;i++)

System.out.print((char)(decrypted[i]+96));

System.out.println();

return;

OUTPUT:

Dept of CSE,SCET Belagavi Page 37


Computer Network Laboratory

6. Write a program for congestion control using leaky bucket algorithm.

import java.util.*;

public class leaky

Dept of CSE,SCET Belagavi Page 38


Computer Network Laboratory

public static void main(String args[]) throws Exception

Scanner s=new Scanner(System.in);

int bucket_size;

int out_rate,no_packets,tot_size=0;

System.out.print("\n Enter the bucket size in bytes:");

bucket_size=s.nextInt();

System.out.print("Enter the no of packets :");

no_packets=s.nextInt();

int in_size[]=new int[1000];

for(int i=0;i<no_packets;i++)

System.out.print("\n Enter the size of packets"+(i+1)+" in bytes :");

in_size[i]=s.nextInt();

if((tot_size+in_size[i]<=bucket_size))

tot_size=tot_size+in_size[i];

else

System.out.println("\n Bucket overflow");

continue;

Dept of CSE,SCET Belagavi Page 39


Computer Network Laboratory

System.out.println("\nEnter the output rate in bytes:");

out_rate=s.nextInt();

int temp =tot_size;

int sent_pkts=1;

while((out_rate<=temp))

System.out.println("\n Packets "+sent_pkts+" sent");

sent_pkts=sent_pkts+1;

temp=temp-out_rate;

OUTPUT:

Dept of CSE,SCET Belagavi Page 40


Computer Network Laboratory

Dept of CSE,SCET Belagavi Page 41

You might also like