5 Sem Cse Computer Networks Lab Manual BCS502
5 Sem Cse Computer Networks Lab Manual BCS502
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
CSE Page 3
Computer Network Laboratory- BCS502
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
CSE Page 4
Computer Network Laboratory- BCS502
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
CSE Page 6
Computer Network Laboratory- BCS502
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
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
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 {
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
CSE Page 13
Computer Network Laboratory- BCS502
0
1
1
1
0
0
Error
THANK YOU.... :)
CASE-2
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;
CSE Page 15
Computer Network Laboratory- BCS502
}
}
// 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();
}
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
CSE Page 19
Computer Network Laboratory- BCS502
3 9999 9999 9999 9999
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
import java.net.*;
import java.io.*;
public class Server1 {
String str;
while((str = contentRead.readLine()) != null) // reading line-by-line from file
{
pwrite.println(str); // sending each line to client
}
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);
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
Client side: (After the sever is ready, run the client program...... note file need to saved at the
sever side)
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 {
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);
}
}
}
import java.io.*;
import java.net.*;
dsock.receive(dpack);
}
}
}
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)
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 {
}
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;
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 {
for(int i=1;i<=5;i++)
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
250
50bytes outputted
50bytes outputted
50bytes outputted
50bytes outputted
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
30
55
50bytes outputted
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
30
50
30bytes outputted
60
30bytes outputted
CSE Page 31
Computer Network Laboratory- BCS502
60
30bytes outputted
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
150
50bytes outputted
50bytes outputted
CSE Page 32
Computer Network Laboratory- BCS502
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
CSE Page 33