0% found this document useful (0 votes)
40 views33 pages

5 Sem Cse Computer Networks Lab Manual BCS502

Cse data

Uploaded by

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

5 Sem Cse Computer Networks Lab Manual BCS502

Cse data

Uploaded by

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

Computer Network Laboratory- BCS502

Department of Computer Science and Engineering

LAB MANUAL-2024-25
COMPUTER NETWORK LABORATORY-BCS502
SEMESTER-05
CSE

Prepared By
Mr. Jayanth C
Assistant Professor,

CSE Page 1
Computer Network Laboratory- BCS502

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.

Source code
#Create Simulator
set ns [new Simulator]
#Open Trace file and NAM file
set ntrace [open prog1.tr w]
$ns trace-all $ntrace
set namfile [open prog1.nam w]
$ns namtrace-all $namfile
#Finish Procedure
proc Finish {} {
global ns ntrace namfile
#Dump all the trace data and close the files
$ns flush-trace
close $ntrace
close $namfile
#Execute the nam animation file
exec nam prog1.nam &
#Show the number of packets dropped
exec echo "The number of packet drops is " &
exec grep -c "^d" prog1.tr &
exit 0
}
#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#Label the nodes
$n0 label "TCP Source"
$n2 label "Sink"
#Set the color
$ns color 1 blue
#Create Links between nodes
#You need to modify the bandwidth to observe the variation in packet drop
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#Make the Link Orientation
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
#Set Queue Size
#You can modify the queue length as well to observe the variation in packet drop
$ns queue-limit $n0 $n1 10

CSE Page 2
Computer Network Laboratory- BCS502
$ns queue-limit $n1 $n2 10
#Set up a Transport layer connection.
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n2 $sink0
$ns connect $tcp0 $sink0
#Set up an Application layer Traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 set type_ CBR
$cbr0 set packetSize_ 100
$cbr0 set rate_ 1Mb
$cbr0 set random_ false
$cbr0 attach-agent $tcp0
$tcp0 set class_ 1
#Schedule Events
$ns at 0.0 "$cbr0 start"
$ns at 5.0 "Finish"
#Run the Simulation
$ns run

Result

Fig. 1a. Three nodes point to point network on execution

CSE Page 3
Computer Network Laboratory- BCS502

Fig. 1b. The number of packets dropped

Note:
1. Set the queue size fixed from n0 to n2 as 10, n1-n2 to 10 and from n2-n3 as 5. Syntax: To set the
queue size

$ns set queue-limit <from> <to> <size> Eg:


$ns set queue-limit $n0 $n2 10
2. Go on varying the bandwidth from 10, 20 30 . . and find the number of packets dropped at the node 2

CSE Page 4
Computer Network Laboratory- BCS502

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.
Design

Source code
#Create Simulator
set ns [new Simulator]
#Use colors to differentiate the traffic
$ns color 1 Blue
$ns color 2 Red
#Open trace and NAM trace file
set ntrace [open prog3.tr w]
$ns trace-all $ntrace
set namfile [open prog3.nam w]
$ns namtrace-all $namfile
#Finish Procedure
proc Finish {} {
global ns ntrace namfile
#Dump all trace data and close the file
$ns flush-trace
close $ntrace
close $namfile
#Execute the nam animation file
exec nam prog3.nam &
#Find the number of ping packets dropped
puts "The number of ping packets dropped are "
exec grep "^d" prog3.tr | cut -d " " -f 5 | grep -c "ping" &
exit 0
}
#Create six nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
#Connect the nodes
for {set j 0} {$j < 5} {incr j} {
$ns duplex-link $n($j) $n([expr ($j+1)]) 0.1Mb 10ms DropTail
}
#Define the recv function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from $from with round trip time $rtt
ms"
}
#Create two ping agents and attach them to n(0) and n(5)
set p0 [new Agent/Ping]
$p0 set class_ 1
$ns attach-agent $n(0) $p0
CSE Page 5
Computer Network Laboratory- BCS502
set p1 [new Agent/Ping]
$p1 set class_ 1
$ns attach-agent $n(5) $p1
$ns connect $p0 $p1
#Set queue size and monitor the queue
#Queue size is set to 2 to observe the drop in ping packets
$ns queue-limit $n(2) $n(3) 2
$ns duplex-link-op $n(2) $n(3) queuePos 0.5
#Create Congestion
#Generate a Huge CBR traffic between n(2) and n(4)
set tcp0 [new Agent/TCP]
$tcp0 set class_ 2
$ns attach-agent $n(2) $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
#Apply CBR traffic over TCP
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set rate_ 1Mb
$cbr0 attach-agent $tcp0
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.4 "$cbr0 start"
$ns at 0.8 "$p0 send"
$ns at 1.0 "$p1 send"
$ns at 1.2 "$cbr0 stop"
$ns at 1.4 "$p0 send"
$ns at 1.6 "$p1 send"
$ns at 1.8 "Finish"
#Run the Simulation
$ns run

Result

Fig. 2a. Ping messages over a 6-nodes network

CSE Page 6
Computer Network Laboratory- BCS502

Fig. 2b. Packet drops over a 6-nodes network during ping


Note:
Vary the bandwidth and queue size between the nodes n0-n2 , n2-n4. n6-n2 and n2- n5 and see the number
of packets dropped at the nodes.

CSE Page 7
Computer Network Laboratory- BCS502

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

Source code

set ns [new Simulator]


$ns color 1 Red
$ns color 2 Blue
set na [open Lab3.nam w]
$ns namtrace-all $na
set nt [open Lab3.tr w]
$ns trace-all $nt
set ng1 [open tcp1.xg w]
set ng2 [open tcp2.xg w]
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 make-lan "$n0 $n1 $n2" 1Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n3 $n4 $n5" 2Mb 10ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
set tcp1 [new Agent/TCP]
set tcp2 [new Agent/TCP]
set cbr1 [new Application/Traffic/CBR]
set cbr2 [new Application/Traffic/CBR]
$ns attach-agent $n4 $tcp1
$cbr1 attach-agent $tcp1
$ns attach-agent $n1 $tcp2
$cbr2 attach-agent $tcp2
set sink1 [new Agent/TCPSink]
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns attach-agent $n5 $sink2
$ns connect $tcp1 $sink1
$ns connect $tcp2 $sink2
proc End {} {
global ns na nt
$ns flush-trace
close $na
close $nt
exec nam Lab3.nam &
exec xgraph tcp1.xg tcp2.xg &
exit 0
}
proc Draw {Agent File} {
global ns
set Cong [$Agent set cwnd_]
set Time [$ns now]

CSE Page 8
Computer Network Laboratory- BCS502
puts $File "$Time $Cong"
$ns at [expr $Time+0.01] "Draw $Agent $File"
}
$ns at 0.0 "$cbr1 start"
$ns at 0.7 "$cbr2 start"
$ns at 0.0 "Draw $tcp1 $ng1"
$ns at 0.0 "Draw $tcp2 $ng2"
$ns at 10.0 "End"
$ns run

CSE Page 9
Computer Network Laboratory- BCS502

Result

Fig. 3b. Ethernet lan packet drops

CSE Page 10
Computer Network Laboratory- BCS502

4. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
import java.util.*;
public class CRC {

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

Scanner input = new Scanner(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=input.nextInt();
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=input.nextInt();
System.out.println("Enter number of bits in divisor : ");
divisor_bits=input.nextInt();
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=input.nextInt();
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ 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]);
CSE Page 11
Computer Network Laboratory- BCS502
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]=input.nextInt();
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[])
{

CSE Page 12
Computer Network Laboratory- BCS502
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)
break;
}
return rem;
}
}

OUTPUT
CASE-1

Enter number of data bits :


5
Enter data bits :
1
0
1
1
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are : 1011100
CRC code :
1011111
Enter CRC code of 7 bits :
1

CSE Page 13
Computer Network Laboratory- BCS502
0
1
1
1
0
0
Error
THANK YOU.... :)

CASE-2

Enter number of data bits :


5
Enter data bits :
1
1
0
1
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
1
1
Dividend (after appending 0's) are : 1101100
CRC code :
1101100
Enter CRC code of 7 bits :
1
1
0
1
1
0

CSE Page 14
Computer Network Laboratory- BCS502
0
No Error
THANK YOU.... :)

5. Develop a program to implement a sliding window protocol in the data link layer.
import java.util.Random;

class Frame {
int seqNum;
String data;

Frame(int seqNum, String data) {


this.seqNum = seqNum;
this.data = data;
}
}

public class SlidingWindowProtocol {


private int windowSize;
private int totalFrames;
private int sendBase;
private int nextSeqNum;
private boolean[] acknowledged;
private Frame[] frames;
private Random random = new Random();

public SlidingWindowProtocol(int windowSize, int totalFrames) {


this.windowSize = windowSize;
this.totalFrames = totalFrames;
this.sendBase = 0;
this.nextSeqNum = 0;
this.acknowledged = new boolean[totalFrames];
this.frames = new Frame[totalFrames];

for (int i = 0; i < totalFrames; i++) {


frames[i] = new Frame(i, "Frame-" + i);

CSE Page 15
Computer Network Laboratory- BCS502
}
}

// Simulate packet loss with a given probability


private boolean simulatePacketLoss(double lossProbability) {
return random.nextDouble() < lossProbability;
}

// Simulate frame transmission


private void sendFrame(Frame frame, double lossProbability) {
System.out.println("Sending: " + frame.data + " (SeqNum: " + frame.seqNum + ")");
if (!simulatePacketLoss(lossProbability)) {
System.out.println("Frame " + frame.seqNum + " successfully sent.");
receiveAck(frame.seqNum);
} else {
System.out.println("Frame " + frame.seqNum + " lost during transmission.");
}
}

// Simulate acknowledgment reception


private void receiveAck(int seqNum) {
System.out.println("Acknowledgment received for SeqNum: " + seqNum);
acknowledged[seqNum] = true;
if (seqNum == sendBase) {
while (sendBase < totalFrames && acknowledged[sendBase]) {
sendBase++;
}
}
}

// Sender's window
public void sendFrames(double lossProbability) {
while (sendBase < totalFrames) {
// Send all frames within the current window
while (nextSeqNum < sendBase + windowSize && nextSeqNum < totalFrames) {
sendFrame(frames[nextSeqNum], lossProbability);

CSE Page 16
Computer Network Laboratory- BCS502
nextSeqNum++;
}

// Simulate delay
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Retransmit unacknowledged frames


for (int i = sendBase; i < nextSeqNum; i++) {
if (!acknowledged[i]) {
System.out.println("Retransmitting unacknowledged frame: SeqNum " + i);
sendFrame(frames[i], lossProbability);
}
}
}

System.out.println("All frames sent and acknowledged.");


}

public static void main(String[] args) {


int windowSize = 4;
int totalFrames = 10;
double lossProbability = 0.2; // 20% chance of packet loss

SlidingWindowProtocol protocol = new SlidingWindowProtocol(windowSize,


totalFrames);
protocol.sendFrames(lossProbability);
}
}

CSE Page 17
Computer Network Laboratory- BCS502
6. Develop a program to find the shortest path between vertices using the Bellman-Ford
and path vector routing algorithm.

import java.util.Scanner;
public class Bellman {
private int d[];
private int n;
public static final int MAX= 9999;
public static void main(String[] args) {
int n=0,s;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = scanner.nextInt();
int a[][] = new int[n][n];
System.out.println("Enter the adjacency matrix");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = scanner.nextInt();
if (a[i][j] == 0)
{
a[i][j] = MAX;
}
}
}
System.out.println("Enter the source vertex");
s = scanner.nextInt();
Bellman b= new Bellman(n);
b.Bellmanford(s,a);
scanner.close();
}
public Bellman(int n)
{
this.n = n;
d = new int[n + 1];
}
CSE Page 18
Computer Network Laboratory- BCS502
public void Bellmanford(int s, int a[][])
{
for (int i = 0; i < n; i++)
{
d[i] = MAX;
}
d[s] = 0;
for (int k = 0; k < n - 1; k++)
{ for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{ if (a[i][j] != MAX)
{ if (d[j] > d[i]+ a[i][j])
{ d[j] = d[i]+ a[i][j];
}
}
}
}
}
for (int i = 0; i <n; i++)
{
System.out.println("distance of source " + s + " to "+ i + " is " + d[i]);
}
}
}

OUTPUT
CASE-1

Enter the number of vertices


5
Enter the adjacency matrix
0 4 6 9999 9999
9999 0 8 -3 -5
9999 9999 0 -2 11
9999 9999 9999 0 9999

CSE Page 19
Computer Network Laboratory- BCS502
3 9999 9999 9999 9999

Enter the source vertex


0
distance of source 0 to 0 is 0
distance of source 0 to 1 is 4
distance of source 0 to 2 is 6
distance of source 0 to 3 is 1
distance of source 0 to 4 is -1

CASE-2
Enter the number of vertices
5
Enter the adjacency matrix
0 3 9999 6 3
9999 9999 9 4 9999
9999 9999 9999 9999 6
2 9999 9999 3 1
9999 9999 0 2 9999

Enter the source vertex


3
distance of source 3 to 0 is 2
distance of source 3 to 1 is 5
distance of source 3 to 2 is 14
distance of source 3 to 3 is 0
distance of source 3 to 4 is 1
7. 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.
// SERVER SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //

import java.net.*;
import java.io.*;
public class Server1 {

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


{
CSE Page 20
Computer Network Laboratory- BCS502

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

// reading the file name from client


InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
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();
}
}

// CLIENT SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //

import java.net.*;
import java.io.*;
public class Client {

CSE Page 21
Computer Network Laboratory- BCS502
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();
}
}

OUTPUT:
Note: As we are using eclipse save and run the server and client program in different
workspace

Server side: (First run the sever)


Server ready for connection
Connection is successful and waiting for chatting
CSE Page 22
Computer Network Laboratory- BCS502

Client side: (After the sever is ready, run the client program...... note file need to saved at the
sever side)

Enter the file name RIT.txt

RIT.
Dept. of CSE
Computer Network Laboratory
15CSL57
( contents of the file name RIT.txt will be displayed as shown above)

8. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.
// SERVER SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //

import java.io.*;
import java.net.*;
public class Server {

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


BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket dsock = new DatagramSocket();
InetAddress address = InetAddress.getLocalHost( );

System.out.println("Server is ready");

while(true)
{
Thread.sleep(1000);
System.out.println("Enter message to be send to client from server ");
String s1 =buff.readLine();
byte arr[] = s1.getBytes( );
DatagramPacket dpack = new DatagramPacket(arr, arr.length, address, 2000);
CSE Page 23
Computer Network Laboratory- BCS502
dsock.send(dpack);
}

}
}

// CLIENT SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //

import java.io.*;
import java.net.*;

public class Client {

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


DatagramSocket dsock = new DatagramSocket(2000);
DatagramPacket dpack;
while(true)
{
byte arr1[] = new byte[100];
dpack = new DatagramPacket(arr1, arr1.length);

dsock.receive(dpack);

String str = new String(arr1);

System.out.println("Message recieved from server");


System.out.println(str);

}
}
}

CSE Page 24
Computer Network Laboratory- BCS502
OUTPUT:
Note: As we are using eclipse save and run the server and client program in different
workspace
Server side: (First run the sever)
Server is ready
Enter message to be send to client from server
Client side: (After the sever is ready, run the client program)
(Then come to Server program)
Server is ready
Enter message to be send to client from server
RIT, HASSAN.
(Above text is the entered message at server side)
(Then come to client Side)

Client Side: (it displays the below message)


Message received from server

RIT, HASSAN.

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

import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
public class Rsa {

public static void main(String[] args) {


BigInteger p,q,n,z,e,d;
byte [] encrypted=new byte[1000];
byte [] decrypted=new byte[1000];
int range=128;
Random random=new Random();
p=BigInteger.probablePrime(range,random);
q=BigInteger.probablePrime(range,random);
e=BigInteger.probablePrime(range, random);
n=p.multiply(q);
z=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
CSE Page 25
Computer Network Laboratory- BCS502
while(z.gcd(e).compareTo(BigInteger.ONE)>0&& e.compareTo(z)<0)
{
e.add(BigInteger.ONE);

}
d=e.modInverse(z);
System.out.println("d:"+d+"\nn"+n);
Scanner in=new Scanner(System.in);
String text;
System.out.println("enter the text");
text=in.nextLine();
System.out.println("ASCII:"+BytestoString(text.getBytes ()));
encrypted=encrypt_decrypt(text.getBytes(), e,n,true);
decrypted=encrypt_decrypt(encrypted,d,n,false);
System.out.println("decrypted String:"+new String(decrypted));
}
public static String BytestoString(byte[] encrypted)
{
String test=" ";
for(int i=0;i<encrypted.length;i++)
{
test=test+encrypted[i]+"";
}
return test;

public static byte[] encrypt_decrypt(byte[] message,BigInteger e,BigInteger n,boolean t)


{
BigInteger c=new BigInteger(message).modPow(e,n);
if(t)
System.out.println("cipher text;"+c);
return c.toByteArray ();
}
}

CSE Page 26
Computer Network Laboratory- BCS502
OUTPUT:
enter the text
process
ASCII: 1121141119910111511532
Ciphertext;1212420823579174296000934469845349375331307846778807131424684358384
344 6587727
decrypted String:process

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

import java.util.Scanner;
public class Leakybuckett {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.println("enter the output rate");


int oprate=input.nextInt();

System.out.println("enter the bucket size");


int bktsize=input.nextInt();

for(int i=1;i<=5;i++)

System.out.println(" the packet number is "+i);

System.out.println("enter the packet size of "+i);


System.out.println();
int pktsize=input.nextInt();
CSE Page 27
Computer Network Laboratory- BCS502
System.out.println();

if(pktsize>bktsize)
System.out.println(" bucket overflow");

else
{
while(pktsize>oprate)
{
System.out.println(oprate+ "bytes outputted ");

try {
Thread.sleep(1000);
} catch (InterruptedException ie)

{
//Handle exception
}
System.out.println();
pktsize=pktsize-oprate;

if(pktsize>0)
{
System.out.println("last " +pktsize+ " bytes outputted");
System.out.println("bucket output sucessfull");
System.out.println();
}

}
}

CSE Page 28
Computer Network Laboratory- BCS502

OUTPUT:
CASE-1

enter the output rate


50
enter the bucket size
500
the packet number is 1
enter the packet size of 1

250

50bytes outputted

50bytes outputted

50bytes outputted

50bytes outputted

last 50 bytes outputted


bucket output sucessfull

the packet number is 2


enter the packet size of 2

510

bucket overflow
the packet number is 3
enter the packet size of 3

175

CSE Page 29
Computer Network Laboratory- BCS502

50bytes outputted

50bytes outputted

50bytes outputted

last 25 bytes outputted


bucket output sucessfull

the packet number is 4


enter the packet size of 4

30

last 30 bytes outputted


bucket output sucessfull

the packet number is 5


enter the packet size of 5

55

50bytes outputted

last 5 bytes outputted


bucket output successful

CASE- 2
enter the output rate
30
enter the bucket size
100
the packet number is 1

CSE Page 30
Computer Network Laboratory- BCS502
enter the packet size of 1

50

30bytes outputted

last 20 bytes outputted


bucket output sucessfull

the packet number is 2


enter the packet size of 2

30

last 30 bytes outputted


bucket output sucessfull

the packet number is 3


enter the packet size of 3

50

30bytes outputted

last 20 bytes outputted


bucket output sucessfull

the packet number is 4


enter the packet size of 4

60

30bytes outputted

last 30 bytes outputted


bucket output sucessfull

CSE Page 31
Computer Network Laboratory- BCS502

the packet number is 5


enter the packet size of 5

60

30bytes outputted

last 30 bytes outputted


bucket output sucessfull

CASE- 3
enter the output rate
50
enter the bucket size
300
the packet number is 1
enter the packet size of 1

100

50bytes outputted

last 50 bytes outputted


bucket output sucessfull

the packet number is 2


enter the packet size of 2

150

50bytes outputted

50bytes outputted

CSE Page 32
Computer Network Laboratory- BCS502

last 50 bytes outputted


bucket output sucessfull

the packet number is 3


enter the packet size of 3

350

bucket overflow
the packet number is 4
enter the packet size of 4

400

bucket overflow
the packet number is 5
enter the packet size of 5

180

50bytes outputted

50bytes outputted

50bytes outputted

last 30 bytes outputted


bucket output sucessfull

CSE Page 33

You might also like