Comprehensive Computer Networks Laboratory Manual (BCS502) – A Practical Guide to Network Design, Simulation, Protocol Implementation, and Analysis
AslamNandyal1
149 views
31 slides
Aug 29, 2025
Slide 1 of 31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
About This Presentation
The Computer Networks Laboratory (BCS502) is a core practical component of the 5th-semester Computer Science & Engineering curriculum under VTU. This lab manual has been meticulously prepared to guide students through hands-on experiments that complement the theoretical concepts studied in the C...
The Computer Networks Laboratory (BCS502) is a core practical component of the 5th-semester Computer Science & Engineering curriculum under VTU. This lab manual has been meticulously prepared to guide students through hands-on experiments that complement the theoretical concepts studied in the Computer Networks course.
Networking has become the backbone of all modern computing systems, enabling communication, data transfer, and distributed processing across global platforms. A solid understanding of networks not only requires theoretical knowledge but also strong practical skills to configure, simulate, and troubleshoot real-world networking scenarios. This manual addresses that need by blending algorithmic study, protocol implementation, and system-level experimentation.
The lab manual is structured to provide a progressive learning path. Initial experiments introduce students to basic networking utilities and commands, error detection techniques such as CRC (Cyclic Redundancy Check), and framing methods in the Data Link Layer. The subsequent exercises focus on routing algorithms (Bellman-Ford, Distance Vector, and Path Vector), congestion control mechanisms, and protocol simulation using NS2.
Students will also gain extensive exposure to socket programming, where they will learn to establish client-server communication using TCP and UDP protocols. This builds the foundation for understanding transport layer reliability and application layer services such as file transfer, remote communication, and message exchange. Advanced experiments integrate concepts like RSA cryptography for secure data transfer and simulation of traffic shaping techniques (Leaky Bucket and Token Bucket algorithms).
The manual also emphasizes the importance of network simulation and analysis tools. By working with NS2, Packet Tracer, and Wireshark, students will be able to visualize packet flow, analyze network performance, and observe how protocols operate under different configurations.
By completing the prescribed experiments, students are expected to achieve the following outcomes:
Hands-on mastery over designing, simulating, and implementing computer networks.
The ability to apply theoretical concepts of OSI and TCP/IP layered architectures in practice.
Development of problem-solving and troubleshooting skills relevant to real-world networking issues.
Confidence in working with industry-standard tools for packet analysis, protocol testing, and network design.
A foundation for pursuing careers in network administration, cybersecurity, cloud computing, and distributed systems.
This manual not only serves as a step-by-step guide for laboratory exercises but also as a reference for future projects, competitive exams, and industry readiness. It is intended to transform students from learners of networking theory into practitioners capable of designing and analyzing networks at various levels of abstraction.
Size: 363.06 KB
Language: en
Added: Aug 29, 2025
Slides: 31 pages
Slide Content
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
BCS502: COMPUTER
NETWORKS LAB
MANUAL
DEPARTMENT OF CSE, AIET
COURSE COORDINATORS:
DR. ASLAM B NANDYAL |
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VISION OF THE INSTITUTE
“Transformative education by pursuing excellence in engineering and management
through enhancing skills to meet the evolving needs of the community”
MISSION OF THE INSTITUTE
• To bestow quality technical education to imbibe knowledge, creativity and ethos
to students’ community.
• To inculcate the best engineering practices through transformative education.
• To develop a knowledgeable individual for a dynamic industrial scenario
• To inculcate research, entrepreneurial skills and human values in order to cater
the needs of the society.
VISION OF THE DEPARTMENT
“Engendering competent, excellent professionals by transforming the knowledge and
computing skills to individuals through modern innovative tools and techniques”
MISSION OF THE DEPARTMENT
1. To produce skilled, creative software developers through rigorous training.
2. To conduct specific technical courses to keep abreast to the latest technological
developments and transformations in the domain.
3. To implement the ideas of research and innovations in interdisciplinary domains.
4. To establish Industry-Institute Interaction programs to enhance the skills of
employability and entrepreneurship.
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM SPECIFIC OUTCOMES (PSO’ S)
A graduate of the computer science and engineering program will exhibit:
PSO1 Professional Skills: The ability to understand and implement the computer
programs in the areas of Computer Architecture, System Software, Database
Management Systems, Web Design, Multimedia and Computer Networking.
PSO2 Problem-Solving Skills: The ability to solve real-world problems by suitable
mathematical model with strong technological concepts in rapidly growing
arena of computer technology.
PSO3 Successful Career and Entrepreneurship: Knowledge in diverse areas of
Software Engineering and Management & Entrepreneurship for IT industry,
conducive in cultivating skills for successful career development.
PROGRAM EDUCATIONAL OBJECTIVES (PEO’ S)
PEO1 To provide students with fundamental strength in core disciplines of computer
engineering to solve the problems of computing world.
PEO2 To ensure that graduates conquer the difficulties of emerging adaptive
technological changes.
PEO3 To prepare students for successful career in the industry of international
standard.
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SEMESTER V
Course Code: BCS502 Course Name: COMPUTER NETWORKS
Course Teacher: Dr. Aslam B Nandyal
Course Outcomes: After studying this course, students will be able to:
CO
Numbers
Course Outcomes
Blooms
Level
Target
Level
BCS502.1
Apply their understanding of network models (TCP/IP and OSI) to
analyze and design network architectures for various applications.
Apply
(L3)
2
BCS502.2
Implement and evaluate different error detection and correction
techniques, such as block coding and cyclic codes, in network
protocols.
Apply
(L3)
2
BCS502.3
Analyze and Compare various routing algorithms, such as DVR,
LSR, PVR, RIP, OSPF, and BGP, to determine their suitability for
different network topologies and traffic patterns.
Analyze
(L4)
2 BCS502.4
Design and Implement transport-layer protocols (UDP and TCP)
to provide reliable and efficient data transfer in various network
environments.
Apply
(L3)
2
BCS502.5
Understand the principles and functionalities of common
application-layer protocols (HTTP, FTP, SMTP, DNS, TELNET,
SSH) and their role in network communication.
Understand
(L2)
2
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Rubrics used for continuous evaluation in every lab session
Parameters Allocated
Marks
Low Medium High
Test on
previous lab
program
10 Given experiment was
not written
Given experiment was
written with
programming errors
Given experiment
was written
without errors.
0 Marks 2-6 Marks 7-10 Marks
Record /
Observation
writing
10 Record and
Observation was not submitted in the lab
session
Record / Observation
was submitted but incomplete
Completed record
/ observation was submitted
0 Marks 1-6 Marks 7-10 Marks
Rubrics used for continuous Evaluation of lab internals
Parameters Allocated
Marks
Low Medium High
Procedure
write up
05 Student was not able
to write algorithm
/program / circuit
diagram
Student was able to
write the algorithm
/program/ circuit
diagram with small
logical errors
Student was able to
write the algorithm /program/ circuit
diagram as per the
desired result.
0 Mark 1-4 Marks 5 Marks
Execution 10 Student was not able
conduct the experiment
Student was partially
able to conduct the experiment with logical
errors
Student was able to
conduct the
experiment as per the
expected output of the
problem
0 Mark 2- 5 Marks 6 - 10 Marks
Viva Voce 05 Student did not
answer any question
Student answered only
few question
Student answered all
the questions.
0 Mark 1-3 Marks 4-5 Marks
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
TABLE OF CONTENT
Program
No.
Name of Program CO’s
Page
no.
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.
BCS502.1
BCS502.5
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.
BCS502.1
BCS502.5
3.
Implement an Ethernet LAN using n nodes and set multiple traffic
nodes and plot congestion window for different source / destination.
BCS502.1
BCS502.5
4.
Develop a program for error detecting code using CRC-CCITT (16-
bits).
BCS502.2
5.
Develop a program to implement a sliding window protocol in the
data link layer.
BCS502.4
6.
Develop a program to find the shortest path between vertices using
the Bellman-Ford and path vector routing algorithm.
BCS502.3
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.
BCS502.4
8.
Develop a program on a datagram socket for client/server to
display the messages on client side, typed at the server side.
BCS502.4
9.
Develop a program for a simple RSA algorithm to encrypt and decrypt the data.
BCS502.2
BCS502.4
BCS502.5
10.
Develop a program for congestion control using a leaky bucket
algorithm.
BCS502.4
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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.
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
#Give node position (for NAM)
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n1 $n2 orient right
#######################
#1.to create drop scenario at first node itself ----- >> change the
packet size of application protocol and packet size of Transport
# layer e.g packet size of cbr =10000 , packet size of tcp =100
#2. Drop at n1 = set queue size ratio to be 5:2 , BWXDelay between
no and n1 = 10Mb X 0.05ms , between n1 and n2 0.05Mb X 100ms
#3 . to count the number of packets dropped grep -c "^d" out.tr
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a CBR Application over TCP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $tcp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 0.1Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 10.0 "$cbr0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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.
#Create a ns simulator
set ns [new Simulator]
#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns color 1 Red
$ns color 2 Green
#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
## to create congestion and to depict the packet drop
# 1. BW X Delay [n0->n1 10MB X 0.05 ms ,Queue Size =5 ] + [ n1->n2
0.05Mb X 100 ms , Queue size =2 ]
# add 4 sends from p0 at 1.0 , similarly add 4 sends from p2 at
1.0 === drop at n1
# repeat the same scenario for p2 , p3 , p4 and p5 to create
congestion scenario
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
#Give node position (for NAM)
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n1 $n2 orient right
#$ns duplex-link-op $n2 $n3 orient right-down
#$ns duplex-link-op $n3 $n4 orient left
#$ns duplex-link-op $n4 $n5 orient left
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from
$from with round-trip-time $rtt ms."
}
#Connect the two agents
$ns connect $p0 $p1
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# to create drop at n1 following sends
$ns at 0.2 "$p0 send"
$ns at 0.2 "$p0 send"
$ns at 0.2 "$p0 send"
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 2.0 "finish"
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
$ns run
OUTPUT:-
student@unixlab:~$ gedit 2.tcl
student@unixlab:~$ ns 2.tcl
node 0 received ping answer from #5 with round- trip-time 227.0 ms.
node 0 received ping answer from #5 with round- trip-time 237.2 ms.
node 5 received ping answer from #0 with round- trip-time 227.0 ms.
node 5 received ping answer from #0 with round- trip-time 237.2 ms.
node 5 received ping answer from #0 with round- trip-time 247.5 ms.
node 5 received ping answer from #0 with round- trip-time 257.7 ms.
student@unixlab:~$ grep -c “^d” out.tr
2
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
set ns [new Simulator]
#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
## The code you need to add –Change 1
set winFile0 [open WinFile0 w]
set winFile1 [open WinFile1 w]
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns simplex-link-op $n2 $n3 orient right
$ns simplex-link-op $n3 $n2 orient left
## change 2 –setting up the lan
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail
MAC/802_3 Channel]
#===================================
# Agents Definition
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
#===================================
#Setup a TCP/Newreno connection
set tcp0 [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n4 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500
$tcp0 set window 5000 # change 3 –set the tcp window size
#Setup a TCP/Newreno connection
set tcp1 [new Agent/TCP/Newreno]
$ns attach-agent $n5 $tcp1
set sink3 [new Agent/TCPSink]
$ns attach-agent $n1 $sink3
$ns connect $tcp1 $sink3
$tcp1 set packetSize_ 1500
$tcp1 set window 500 # change 4 –set the tcp window size
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP/Newreno connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 10.0 "$ftp0 stop"
#Setup a FTP Application over TCP/Newreno connection
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 "$ftp1 start"
$ns at 10.0 "$ftp1 stop"
# change 4 –setting up error model between $n2 $ n3 in random
fashion
set var [new ErrorModel]
$var ranvar [new RandomVariable/Uniform]
$var drop-target [new Agent/Null]
$ns lossmodel $var $n2 $n3
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
4. Develop a program for error detecting code using CRC-CCITT (16- bits).
import java.util.Scanner;
import java.io.*;
public class CRC1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
//Input Data Stream
System.out.print("Enter message bits: ");
String message = sc.nextLine();
System.out.print("Enter generator: ");
String generator = sc.nextLine();
int data[] = new int[message.length() + generator.length() - 1];
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
System.out.print("The checksum code is: ");
for(int i=0;i<message.length();i++)
data[i] = Integer.parseInt(message.charAt(i)+"");
for(int i=0;i<data.length;i++)
System.out.print(data[i]);
System.out.println();
//Check for input CRC code
System.out.print("Enter checksum code: ");
message = sc.nextLine();
System.out.print("Enter generator: ");
generator = sc.nextLine();
data = new int[message.length() + generator.length() - 1];
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
if(data[i]==1){
valid = false;
break;
}
if(valid==true)
System.out.println("Data stream is valid");
else
System.out.println("Data stream is invalid. CRC error occurred.");
}
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
5. Develop a program to implement a sliding window protocol in the data link layer.
ALGORITHM:
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request. 4.To send frames to server
from the client side.
5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK
signal to
client.
6.Stop the program
Program :
import java.net.*;
import java.io.*;
import java.rmi.*;
public class slidsender
{
public static void main(String a[])throws Exception
{
ServerSocket ser=new ServerSocket(10);
Socket s=ser.accept();
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new
DataInputStream(s.getInputStream());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("Enter theno. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter "+nf+" Messages to be send\n"); for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
System.out.println(" for "+ano+" frames"); sws+=nf;
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
}
else
{
System.out.println("The no. of frames exceeds window size"); break;
}
System.out.print("\ nDo you wants to send some more frames : "); ch=in.readLine(); p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}
RECEIVER PROGRAM
import java.net.*;
import java.io.*;
class slidreceiver
{
public static void main(String a[])throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new
DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch;
System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The received Frame " +rptr+" is : "+rbuf[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgmentsent\n");
p.println(rptr+1); rws+=nf; }
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
}
OUTPUT:
//SENDER OUTPUT
Enter the no. of frames : 4
Enter 4 Messages to be send
hiii
how r u
i am fine
how is evryone
Acknowledgment received for 4 frames
Do you wants to send some more frames : no
//RECEIVER OUTPUT
The received Frame 0 is : hiii
The received Frame 1 is : how r u
The received Frame 2 is : i am fine
The received Frame 3 is : how is everyone
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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 ford
{
private int D[];
private int num_ver;
public static final int MAX_VALUE = 999;
public ford(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];
}
}
}
}
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++)
{
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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++)
{
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();
ford b = new ford (num_ver);
b.BellmanFordEvaluation(source, A);
scanner.close();
}
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:-
student@unixlab:~$ javac BellmanFord.java
student@unixlab:~$ java BellmanFord
Enter the number of vertices
4
Enter the adjacency matrix
0 5 0 0
5 0 3 4
0 3 0 2
0 4 2 0
Enter the source vertex
2
distance of source 2 to 1 is 5
distance of source 2 to 2 is 0
distance of source 2 to 3 is 3
distance of source 2 to 4 is 4
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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:-
import java.net.*;
import java.io.*;
public class ContentsServer
{
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 successful and wating for
chatting");
import java.net.*; import java.io.*;
public class ContentsClient
{
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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));
student@unixlab:~$ javac ContentsServer.java
student@unixlab:~$ java ContentsServer
Server ready for connection
Connection is successful and wating for chatting
student@unixlab:~$ javac ContentsClient.java
student@unixlab:~$ java ContentsClient
Enter the file name
abc.txt
TCP is Reliable Protocol
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
8. Develop a program on a datagram socket for client/server to display the messages on
client side, typed at the server side.
Server Side:-
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(6789);
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());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println("enter message to echo:");
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
String Sentence = inFromUser.readLine();
sendData = Sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,port);
serverSocket.send(sendPacket);
}
}
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Client Side:-
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = "hi";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,6789);
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();
}
}
OUTPUT:-
student@unixlab:~$ javac UDPServer.java
student@unixlab:~$ java UDPServer
enter message to echo:
TCP is Reliable Protocol
student@unixlab:~$ javac UDPClient.java
student@unixlab:~$ java UDPClient
FROM SERVER:TCP is Reliable Protocol
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
9. Develop a program for a simple RSA algorithm to encrypt and decrypt the data.
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
public class RSA {
private BigInteger p,q,N,phi,e,d;
private int bitlength=1024;
private Random r;
public RSA()
{
r=new Random();
p=BigInteger.probablePrime(bitlength,r);
q=BigInteger.probablePrime(bitlength,r);
System.out.println("Prime number p is"+p);
System.out.println("prime number q is"+q);
N=p.multiply(q);
phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e=BigInteger.probablePrime(bitlength/2,r);
while(phi.gcd(e).compareTo(BigInteger.ONE)>0&&e.compareTo(phi)<0)
{
e.add(BigInteger.ONE);
}
System.out.println("Public key is"+e); d=e.modInverse(phi);
System.out.println("Private key is"+d);
}
public RSA(BigInteger e,BigInteger d,BigInteger N)
{
this.e=e;
this.d=d;
this.N=N;
}
public static void main(String[] args)throws IOException
{
RSA rsa=new RSA();
DataInputStream in=new DataInputStream(System.in);
String testString;
System.out.println("Enter the plain text:");
testString=in.readLine();
System.out.println("Encrypting string:"+testString);
System.out.println("string in bytes:"+bytesToString(testString.getBytes()));
byte[] encrypted=rsa.encrypt(testString.getBytes());
byte[] decrypted=rsa.decrypt(encrypted);
System.out.println("Dcrypting Bytes:"+bytesToString(decrypted));
System.out.println("Dcrypted string:"+new String(decrypted));
}
private static String bytesToString(byte[] encrypted)
{
String test=" ";
for(byte b:encrypted)
{
test+=Byte.toString(b);
}
return test;
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
public byte[]encrypt(byte[]message)
{
return(new BigInteger(message)).modPow(e,N).toByteArray();
}
public byte[]decrypt(byte[]message)
{
return(new BigInteger(message)).modPow(d,N).toByteArray();
}
}
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
10. Develop a program for congestion control using a leaky bucket algorithm.
import java.util.Scanner;
import java.lang.*;
public class leakybucket {
public static void main(String[] args)
{
int i;
int a[]=new int[20];
int buck_rem=0,buck_cap=4,rate=3,sent,recv;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of packets");
int n = in.nextInt();
System.out.println("Enter the packets");
for(i=1;i<=n;i++)
a[i]= in.nextInt();
System.out.println("Clock \t packet size \ t accept \ t sent \ t
remaining");
for(i=1;i<=n;i++)
{
if(a[i]!=0)
{
if(buck_rem+a[i]>buck_cap)
recv=- 1;
else
{
recv=a[i];
buck_rem+=a[i];
}
}
else
recv=0;
if(buck_rem!=0)
{
if(buck_rem<rate)
{
sent=buck_rem;
buck_rem=0;
}
else
{
sent=rate;
buck_rem=buck_rem- rate;
}
}
else
sent=0;
ALVA’S INSTITUTE OF ENGINEERING & TECHNOLOGY
Shobhavana Campus Mijar, Moodbidri , D.K. Karnataka 574225
(Autonomous Institution affiliated to VTU, Belagavi)
(Accredited by NAAC with A+ Grade, and Accredited by NBA New Delhi 2019 -2025)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING