0% found this document useful (0 votes)
646 views

ComputerNetworksLAB 18ECL76 7thsemester

This document is a lab manual for the Computer Networks course at SRI Venkateshwara College of Engineering. It provides information on course objectives, outcomes, syllabus, and experiments. The course aims to help students model and simulate networks using tools and understand protocols at various OSI layers. Students will simulate concepts like frame formats, routing algorithms, and analyze algorithms using C programming. The manual was prepared by faculty of the Electronics and Communication Engineering department and approved by the Head of Department.

Uploaded by

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

ComputerNetworksLAB 18ECL76 7thsemester

This document is a lab manual for the Computer Networks course at SRI Venkateshwara College of Engineering. It provides information on course objectives, outcomes, syllabus, and experiments. The course aims to help students model and simulate networks using tools and understand protocols at various OSI layers. Students will simulate concepts like frame formats, routing algorithms, and analyze algorithms using C programming. The manual was prepared by faculty of the Electronics and Communication Engineering department and approved by the Head of Department.

Uploaded by

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

Computer Networks LAB (18ECL76) 2021-22

SVCE/ECE/MNL./CNL/Ver-01
SRI VENKATESHWARA COLLEGE OF ENGINEERING
VIDYANAGAR, BENGALURU-562157

DEPARTMENT OF
ELECTRONICS & COMMUNICATIONENGINEERING

LAB MANUAL
Computer Network Lab
(18ECL76)
(VII SEMESTER)

Prepared by
Dr. Vijayashree R Budyal
Dr. Ravi Gatti
Prof. Nataraja N

2021-2022

Approved By:
Dr. Vijayashree R Budyal
HoD, E&CE, SVCE, Bengaluru

Department of E&CE, SVCE, Bengaluru Page 1


Computer Networks LAB (18ECL76) 2021-22

INDEX
Page
Sl. No. Description
No.
1 Course objectives 2
2 Course outcomes 2
3 ABET Student Outcomes, NBA Programme 3
4 Course Outcomes Mapping with Programme outcomes 5
5 Course Syllabus 6
6 Experiments Beyond the Syllabus 7

1. COURSE OBJECTIVES
This course will enable students to:
➢ Choose suitable tools to model a network and understand the protocols at various OSI
reference levels.
➢ Design a suitable network and simulate using a Network simulator tool.
➢ Simulate the networking concepts and protocols using C/C++ programming.
➢ Model the networks for different configurations and analyze the results.

2. COURSE OUTCOMES

Level of Blooms
COs After Completion of the course the student will be able to
Taxonomy
C367.1 Choose suitable tools to model a network and demonstrate and
calculate the network parameters of different protocols at various L3
OSI reference levels.

C367.2 Illustrate network performance for the transport layer protocols such
as UDP and TCP L3

C367.3 Simulate ESS and HDLC frame format for the Data communication
L3
over the Different Networks

C367.4 Analyze CRC and Leaky bucket algorithm for different network
L4
parameters using c language.

C367.5 Design and development of a different routing algorithms different


network topology and simulate using Network simulator tool. L5

Department of E&CE, SVCE, Bengaluru Page 2


Computer Networks LAB (18ECL76) 2021-22

3. ABET Student Outcomes

Student Outcomes are outcomes (1) through (7), plus any additional
outcomes that may be articulated by the program.

SO1. An ability to identify, formulate, and solve complex engineering


problems by applying principles of engineering, science, and
mathematics
SO2. An ability to apply engineering design to produce solutions that
meet specified needs with consideration of public health, safety, and
welfare, as well as global, cultural, social, environmental, and economic
factors
SO3. An ability to communicate effectively with a range of audiences
SO4. An ability to recognize ethical and professional responsibilities in
engineering situations and make informed judgments, which must
consider the impact of engineering solutions in global, economic,
environmental, and societal contexts
SO5. An ability to function effectively on a team whose members
together provide leadership, create a collaborative and inclusive
environment, establish goals, plan tasks, and meet objectives
SO6. An ability to develop and conduct appropriate experimentation,
analyze and interpret data, and use engineering judgment to draw
conclusions
SO7. An ability to acquire and apply new knowledge as needed, using
appropriate learning strategies.

Reference: https://www.abet.org/accreditation/accreditation-criteria/criteria-for-accrediting-
engineering-programs-2020-2021/

4. NBA Programme Outcome

Engineering Graduates will be able to:

PO1. Engineering knowledge: Apply the knowledge of mathematics,


science, engineering fundamentals, and an engineering specialization to
the solution of complex engineering problems.
PO2. Problem analysis: Identify, formulate, review research literature,
and analyze complex engineering problems reaching substantiated
conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
PO3. Design/development of solutions: Design solutions for complex
engineering problems and design system components or processes that
Department of E&CE, SVCE, Bengaluru Page 3
Computer Networks LAB (18ECL76) 2021-22

meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental
considerations.
PO4. Conduct investigations of complex problems: Use research-based
knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
PO5. Modern tool usage: Create, select, and apply appropriate
techniques, resources, and modern engineering and IT tools including
prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6. The engineer and society: Apply reasoning informed by the
contextual knowledge to assess societal, health, safety, legal and cultural
issues and the consequent responsibilities relevant to the professional
engineering practice.
PO7. Environment and sustainability: Understand the impact of the
professional engineering solutions in societal and environmental
contexts, and demonstrate the knowledge of, and need for sustainable
development.
PO8. Ethics: Apply ethical principles and commit to professional ethics
and responsibilities and norms of the engineering practice.
PO9. Individual and teamwork: Function effectively as an individual,
and as a member or leader in diverse teams, and in multidisciplinary
settings.
PO10. Communication: Communicate effectively on complex engineering
activities with the engineering community and with society at large, such
as, being able to comprehend and write effective reports and design
documentation, make effective presentations, and give and receive clear
instructions.
PO11. Project management and finance: Demonstrate knowledge and
understanding of the engineering and management principles and apply
these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
PO12. Life-long learning: Recognize the need for, and have the
preparation and ability to engage in independent and life-long learning
in the broadest context of technological change.

5. Program Specific Outcomes and Course Outcomes

1. Ability to learn and implement the new technologies in Electronics


and Communication domain.
2. Ability to Design and manufacture simple and complex Electronic
Circuit boards for various applications
Department of E&CE, SVCE, Bengaluru Page 4
Computer Networks LAB (18ECL76) 2021-22

CO-SO Mapping (Optional)

CO \ SO SO1 SO2 SO3 SO4 SO5 SO6 SO7

C367.1 3 3 3 2 1 3 3

C367.2 3 3 2 2 2 3 3

C367.3 3 3 2 2 1 3 3

C367.4 3 3 2 2 1 3 3

C367.5 3 3 2 2 1 3 3

3 Strong Correlation

2 Moderate Correlation

1 Weak / No Correlation

CO-PO & PSO Mapping

CO \ CK PO PO PO PO PO PO PO PO PO PO1 PO1 PO1 PSO PSO


PO L 1 2 3 4 5 6 7 8 9 0 1 2 1 2

PKL 3 5 6 5 6 3 3 3 NA M 3 M 3 3

C367.
3 3 3 3 2 1 2 2 2 1 2 2 2 3 3
1

C367.
3 3 3 3 1 1 3 3 3 1 2 2 1 3 2
2

C367.
3 3 3 3 1 1 3 3 3 1 2 2 1 2 2
3

C367.
4 3 3 3 1 3 2 2 2 1 2 2 2 2 2
4

C367.
5 3 3 3 1 3 3 1 3 1 2 2 2 2 2
5

3 Strong Correlation

2 Moderate Correlation

1 Weak / No Correlation

Department of E&CE, SVCE, Bengaluru Page 5


Computer Networks LAB (18ECL76) 2021-22

6. Course syllabus
Page
Sl. No. Description
No.
PART - A
Simulation experiments using NS2/ NS3/ OPNET/ NCTUNS/ NetSim/
QualNet/ Packet Tracer or any other equivalent tool
Implement a point to pint network with four nodes and duplex links between
1 them. Analyze the network performance by setting the queue size and varying 9
the bandwidth.
Implement a four node point to point network with links n0-n2, n1-n2 and n2-
n3. Apply TCP agent between n0-n3 and UDP between n1-n3. Apply relevant
2 11
applications over TCP and UDP agents changing the parameter and determine
the number of packets sent by TCP/UDP.
Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by
3 13
changing the error rate and data rate.
Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes 15
4
and obtain congestion window for different sources/ destinations.
Implement ESS with transmission nodes in Wireless LAN and obtain the
5
performance parameters. 17
6 Implementation of Link state routing algorithm 19
PART - B : Implement the following in C/C++
Write a program for a HLDC frame to perform the following.
1 i) Bit stuffing 22
ii) Character stuffing.
Write a program for distance vector algorithm to find suitable path for
2 26
transmission.
3 Implement Dijkstra’s algorithm to compute the shortest routing path. 28
For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the
program for the cases
4 30
a. Without error
b. With error
5 Implementation of Stop and Wait Protocol and Sliding Window Protocol 32
Write a program for congestion control using leaky bucket algorithm.
6 36
Experiments beyond the syllabus
Transmission of ping message
1 38
Using TCP/IP sockets, write a client server program to make client sending the
2 file name and the server to send back the contents of the requested file 42

Implement the TCP/IP sockets, write a client server program using as message
3 queues or FIFOs as IPC channels 48

Department of E&CE, SVCE, Bengaluru Page 6


Computer Networks LAB (18ECL76) 2021-22

7. LIST OF EXTRA PROGRAMS APART FROM THE CURRICULUM


LIST OF EXTRA PROGRAMS APART FROM THE CURRICULUM

Sl.No Programs Attainment of


POs

1. Transmission of Ping Message: Simulate the transmission of ping PO3, PO5, PO9,
messages over a network topology consisting of 6 nodes and find the PO11, PO12
number of packets dropped due to congestion. PSO1

2. Using TCP/IP sockets, write a client server program to make PO3, PO5, PO9,
client sending the file name and the server to send back the PO11, PO12

contents of the requested file if present PSO1

3. Implement the TCP/IP sockets, write a client server program PO3, PO5, PO9,
using as message queues or FIFOs as IPC channels PO11, PO12

PSO1

Programme Coordinator

Department of E&CE, SVCE, Bengaluru Page 7


Computer Networks LAB (18ECL76) 2021-22

Simulation Software used for the lab


All the experiments of Computer Networks laboratory will be executed using three
variants/methodology. Variant I & II will be executed through the use of NS 2 simulator and
variant III will be executed in NCTUNs simulator and Emulator. Details of both the simulators
are described in the following section as follows,

1. Network Simulator: N2 simulator


Network Simulator (Version 2), widely known as NS2, is simply an event driven simulation tool that
has proved useful in studying the dynamic nature of communication networks. Simulation of wired as well
as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can be done using NS2.
In general, NS2 provides users with a way of specifying such network protocols and simulating their
corresponding behaviors. Due to its flexibility and modular nature, NS2 has gained constant popularity in
the networking research community since its birth in 1989. Ever since, several revolutions and revisions
have marked the growing maturity of the tool, thanks to substantial contributions from the players in the
field. Among these are the University of California and Cornell University who developed the REAL
network simulator,1 the foundation which NS is based on. Since 1995 the Defense Advanced Research
Projects Agency (DARPA) supported development of NS through the Virtual Inter Network Testbed
(VINT) project. Currently the National Science Foundation (NSF) has joined the ride in development. Last
but not the least, the group of Researchers and developers in the community are constantly working to keep
NS2 strong and versatile.

1.1 Basic Architecture

Figure 1: Basic Architecture of N2

Department of E&CE, SVCE, Bengaluru Page 8


Computer Networks LAB (18ECL76) 2021-22

Figure 1 shows the basic architecture of NS2. NS2 provides users with an executable
command ns which takes on input argument, the name of a Tcl simulation scripting file. Users
are feeding the name of a Tcl simulation script as an input argument of an NS2 executable
command ns.
In most cases, a simulation trace file is created, and is used to plot graph and/or to
create animation. NS2 consists of two key languages: C++ and Object-oriented Tool Command
Language (OTcl). While the C++ defines the internal mechanism (i.e., a backend) of the
simulation objects, the OTcl sets up simulation by assembling and configuring the objects as
well as scheduling discrete events.
The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object,
variables in the OTcl domains are sometimes referred to as handles. Conceptually, a is just a
string in the OTcl domain, and does not contain any functionality. Instead, the functionality is
defined in the mapped C++ object. In the OTcl domain, a handle acts as a frontend which
interacts with users and other OTcl objects. It may define its own procedures and variables to
facilitate the interaction. Note that the member procedures and variables in the OTcl domain
are called instance procedures (instprocs) and instance variables respectively. Before
proceeding further, the readers are encouraged to learn C++ and OTcl languages. We refer the
readers to [14] for the detail of C++, while a brief tutorial of Tcl and OTcl tutorial are given in
Appendices A.1 and A.2, respectively.

NS2 provides a large number of built-in C++ objects. It is advisable to use these C++
objects to set up a simulation using a Tcl simulation script. However, advance users may find
these objects insufficient. They need to develop their own C++ objects, and use a OTcl
configuration interface to put together these objects. After simulation, NS2 outputs either text-
based or animation-based simulation results. To interpret these results graphically and
interactively, tools such as NAM (Network AniMator) and XGraph are used. To analyze a
particular behaviour of the network, users can extract a relevant subset of text-based data and
transform it to a more conceivable presentation.

CONCEPT OVERVIEW:
NS uses two languages because simulator has two different kinds of things it needs to
do. On one hand, detailed simulations of protocols require a systems programming language
which can efficiently manipulate bytes, packet headers, and implement algorithms that run

Department of E&CE, SVCE, Bengaluru Page 9


Computer Networks LAB (18ECL76) 2021-22

over large data sets. For these tasks run-time speed is important and turn-around time (run
simulation, find bug, fix bug, recompile, re-run) is less important. On the other hand, a large
part of network research involves slightly varying parameters or configurations, or quickly
exploring a number of scenarios.

Figure 2: Layered Architecture of communication Model

Start ns with the command 'ns <tclscript>' (assuming that you are in the directory with the ns
executable, or that your path points to that directory), where '<tclscript>' is the name of a Tcl
script file which defines the simulation scenario. You could also just start ns without any
arguments and enter the Tcl commands in the Tcl shell, but that is definitely less comfortable.
Everything else depends on the Tcl script. The script might create some output on stdout, it might
write a tracefile or it might start nam to visualize the simulation.

Starting nam

You can either start nam with the command 'nam <nam-file>' where '<nam-file>' is the name of
a nam trace file that was generated by ns, or you can execute it directly out of the Tcl simulation
script for the simulation which you want to visualize. Below you can see a screenshot of a nam
window where the most important functions arebeing explained.

Department of E&CE, SVCE, Bengaluru Page 10


Computer Networks LAB (18ECL76) 2021-22

Figure 3: Network Animator Window

Introduction to VI:
VI is a text editor that is available in all Unix systems, other editors such pico and emacs
can also be used instead. Before you start for the first time, you must note that vi is modal editor.
A mode is like an environment. Different modes in vi interpret the same key differently. For
example, if you're in insert mode, typing a adds an a to the text, whereas in command mode,
typing a puts you in insert mode because a is the key abbreviation for the append command. If
you get confused about what mode you're in, press the Escape key on your keyboard. Pressing
Escape always returns you to the command mode (and if you're already in command mode, it
simply beeps to remind you of that fact). When you are in command mode, you can manage you
document; this includes the capability to change text, rearrange it, and delete it.

A summary of the basic vi commands are available in Appendix A. Insert mode is when you are
adding text directly into your document from the keyboard and you can maneuver the text area
using the arrow keys. For starting a new document, simply type vi after the command prompt to
the start the vi editor. The cursor will be located in the top left corner and each of the following
lines will start with a tilde (~) denoting empty lines. Note that the vi editor in currently in
command mode. In order to edit an existing file, type the name of the file along with the extension
after typing vi and a space in the Unix command prompt. To enter into the insert mode simply
press a on the keyboard. Then to save the edited document, type a colon (:) and the cursor will be
located in the bottom left corner after the colon, followed by a w and then press enter. In order to
quit the vi editor type a colon followed by a q (:q), this will take you back to the Unix prompt.
You can also choose to quit editing without saving by typing :q!, and if you forget the
exclamation mark (!) the system will issue a warning. Note: Do not forget to change the
permission settings for the edited file. This is done from the Unix command prompt by typing

Department of E&CE, SVCE, Bengaluru Page 11


Computer Networks LAB (18ECL76) 2021-22

chmod ugo+wrx filename.extension. In ugo, u = user, g = group, o = other, and in wrx, w = write,
r = read, x = execute, and the above command can be interpreted as, the user, the group and
everyone else can read, write and execute the file. To remove permissions, just replace the plus
sign (+) with a minus sign (-). The .tcl file must be executable.

2. NCTUns Network Simulator and Emulator

NCTUns uses a novel kernel-reentering simulation. As a result, it provides several unique


advantages that cannot be easily achieved by traditional network simulators. NCTUns directly
uses the real-life Linux’s TCP/IP protocol stack to generate high-fidelity simulation results. By
using the novel kernel re-entering simulation methodology, a real-life UNIX (e.g., FreeBSD or
Linux) kernel’s protocol stack is directly used to generate high-fidelity simulation results.

In NCTUns, the configuration and operation for a simulated network are exactly the same as those
for a real-life IP network. This provides two advantages:

➢ If a user knows how to configure and operate a real-life IP network, he (she) immediately
knows how to configure and operate a simulated network in NCTUns,
➢ The configuration and operation of simulated networks in NCTUns are exactly the same as
those for real-life IP networks, NCTUns can be used as a training tool to educate people
how to configure and operate a real-life IP network. In NCTUns, many valuable real-life
UNIX network configuration tools (e.g., route, ifconfig, netstat) and performance
monitoring tools (e.g., ping, tcp dump, traceroute) can be directly run on a simulated
network to configure and monitor a simulated network.

NCTUns provides a highly-integrated and professional GUI environment in which a user can
easily conduct network simulations. The NCTUns GUI program is capable of,

➢ Drawing network topologies


➢ Configuring the protocol modules used inside a node
➢ Configuring the parameter values used inside a protocol module
➢ Specifying the initial locations and moving paths of mobile nodes
➢ Plotting network performance graphs
➢ Playing back the animation of a logged packet transfer trace
➢ Pasting a map graph on the background of the network topology
➢ Constructing a road network for wireless vehicular network simulations

Department of E&CE, SVCE, Bengaluru Page 12


Computer Networks LAB (18ECL76) 2021-22

By using a distributed architecture, each component of NCTUns can be run on a separate


machine as shown in Figure 2. (This is called the “multi-machine” mode.) As such, the
machine that runs the GUI program can be different from the machine that runs the simulation
engine. This capability sometimes can have an advantage; When simulating a very large case
with hundreds of mobile nodes, the GUI will consume many CPU cycles to draw the
movements of these mobile nodes during the simulation. However, this will leave few CPU
cycles for the simulation engine to simulate the network protocols. To overcome this
performance problem, using the multimachine mode to run the GUI program and the
simulation engine on two different machines is a good solution.

Figure 4: The distributed architecture of NCTUns

A quick idea about what the GUI environment looks like, some screenshots of NCTUns
are shown below

➢ Starting Screen: Every time when a user launches the GUI program, the following starting
screen will pop up.
➢ Topology Editor: The topology editor provides a convenient and intuitive way to
graphically construct a network topology. A constructed network can be a fixed wired
network or a mobile wireless network.
➢ Attribute Dialog Box: A network device (node) may have many attributes. Setting and
modifying the attributes of a network node can be easily done. Just double-clicking the
icon of a network node.

Department of E&CE, SVCE, Bengaluru Page 13


Computer Networks LAB (18ECL76) 2021-22

➢ Performance Monitor: The performance monitor can easily and graphically generate and
display plots of some monitored performance metrics over time. Examples include a link’s
utilization or a TCP connection’s achieved throughput.

➢ Node Editor: The node editor provides a convenient environment in which a user can
flexibly configure the protocol modules used inside a network node. By using this tool, a
user can easily add, delete, or replace a module with his (her) own module. This capability
enables a user to easily test the performance of a new protocol.

➢ Packet Animation: Player By using the packet animation player, a packet transfer trace
logged during a simulation can be replayed at a specified speed. Both wired and wireless
networks are supported. This capability is very useful because it helps a researcher
visually see and debug the behavior of a network protocol.

Department of E&CE, SVCE, Bengaluru Page 14


Computer Networks LAB (18ECL76) 2021-22

Experiment 1

Aim:
Implement a point to pint network with four nodes and duplex links between them. Analyze the network
performance by setting the queue size and varying the bandwidth.

Theory:

TCP (Transmission Control protocol) is a standard that defines how to establish and maintain a network
conversation via which application programs can exchange data. TCP works with the Internet Protocol
(IP), which defines how computers send packets of data to each other. Together, TCP and IP are the basic
rules defining the Internet.

Variant I: Interfacing of the nodes and setting data communication through UDP
Data type through NS-2 Simulator
set ns [new Simulator]
set f [open lab1.tr w]
$ns trace-all $f

set nf [open lab1.nam w]


$ns namtrace-all $nf

proc finish {} {
global f nf ns
$ns flush-trace
close $f
close $nf
exec nam lab1.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7
$ns duplex-link $n1 $n2 0.3Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7

Department of E&CE, SVCE, Bengaluru Page 15


Computer Networks LAB (18ECL76) 2021-22

$ns duplex-link $n2 $n3 0.3Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7

$ns queue-limit $n0 $n1 20


$ns queue-limit $n1 $n2 20
$ns queue-limit $n2 $n3 20
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005

set null0 [new Agent/Null]


$ns attach-agent $n3 $null0

$ns connect $udp0 $null0

$ns at 0.1 "$cbr0 start"


$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run

Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab1.tcl
➢ Save the program and quit.
➢ Run the simulation program
[root@localhost~]# ns lab1.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ To calculate the network performance. Execute the following command.
For calculating number of received packets
[root@localhost~]#grep ^r lab1.tr | grep “cbr” | awk ‘{s+=$6}END{print s}’
For calculating total time
[root@localhost~]#grep ^r lab1.tr | grep “cbr” | awk ‘{s+=$2}END{print s}’

Network performace = (Packet received/ Total Time) (bps)

➢ Write the value of network performance in observation sheet. Repeat the above step by changing the
bandwidth to [0.3Mb, 0.4Mb, 0.5Mb, 0.7Mb]to the following line of the program.
➢ $ns duplex-link $n0 $n1 0.7Mb 10ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7
➢ $ns duplex-link $n1 $n2 0.7Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7
➢ $ns duplex-link $n2 $n3 0.7Mb 20ms DropTail #vary bandwidth 0.3, 0.4, 0.5 0.7

Department of E&CE, SVCE, Bengaluru Page 16


Computer Networks LAB (18ECL76) 2021-22

Sl. Bandwidth Network performance


No.
1. 0.3
2. 0.4
3. 0.5
4. 0.7

➢ Plot a graph with x- axis with bandwidth and y-axis with network performance of UDP protocol.

Variant Type-II: Interfacing of the nodes and setting data communication through TCP Data type
through NS-2 Simulator

set ns [new Simulator]

set f [open lab2.tr w]


$ns trace-all $f

set nf [open lab2.nam w]


$ns namtrace-all $nf

proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exec nam lab2.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

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


$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 2.75Mb 20ms DropTail

$ns queue-limit $n2 $n3 50

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0

Department of E&CE, SVCE, Bengaluru Page 17


Computer Networks LAB (18ECL76) 2021-22

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set sink [new Agent/TCPSink]


$ns attach-agent $n3 $sink
$ns connect $tcp0 $sink

$ns at 0.1 "$ftp0 start"


$ns at 4.0 "$ftp0 stop"
$ns at 5.0 "finish"
$ns run
Variant Type-III: Interfacing of the nodes and setting data communication through TCP Data type
through Packet Tracer

Procedure:

➢ Develop a topology shown in the Figure given below


➢ Configure all the nodes
➢ Configure the switch & sniffer
➢ Varying the bandwidth from 100Mbps to 10Mbps in switch ports (Fast Ethernet 0/1 and Fast
Ethernet (1/1)
➢ Analyze the network performance by setting the queue size in sniffer 0

Figure 5: Packet Tracer Window for 4 nodes network

Department of E&CE, SVCE, Bengaluru Page 18


Computer Networks LAB (18ECL76) 2021-22

Experiments 2

Aim:
Implement a four-node point to point network with links n0-n2, n1-n2 and n2-n3. Apply TCP agent
between n0-n3 and UDP between n1-n3. Apply relevant applications over TCP and UDP agents changing
the parameter and determine the number of packets sent by TCP/UDP.

Theory:
UDP (User Datagram Protocol) is an alternative communications protocol to Transmission Control
Protocol (TCP) used primarily for establishing low-latency and loss-tolerating connections between
applications on the internet.

Variant Type-I: Interfacing of the 4 nodes and setting data communication through UDP Data type
through NS 2 Simulator

set ns [new Simulator]

set f [open lab2.tr w]


$ns trace-all $f

set nf [open lab2.nam w]


$ns namtrace-all $nf

$ns color 1 "Blue"


$ns color 2 "Red"

proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exec nam lab2.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
Department of E&CE, SVCE, Bengaluru Page 19
Computer Networks LAB (18ECL76) 2021-22

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


$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 2.75Mb 20ms DropTail

$ns queue-limit $n2 $n3 50

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
$tcp0 set class_ 1

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set sink [new Agent/TCPSink]


$ns attach-agent $n3 $sink
$ns connect $tcp0 $sink

set udp0 [new Agent/UDP]


$ns attach-agent $n1 $udp0
$udp0 set class_ 2

set cbr0 [new Application/Traffic/CBR]


$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set interval_ 0.005

set null0 [new Agent/Null]


$ns attach-agent $n3 $null0
$ns connect $udp0 $null0

$ns at 0.1 "$cbr0 start"


$ns at 1.0 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"


$ns run

Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab2.tcl
➢ Save the program and quit.

Department of E&CE, SVCE, Bengaluru Page 20


Computer Networks LAB (18ECL76) 2021-22

➢ Run the simulation program


[root@localhost~]# ns lab2.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ To calculate the number of packets sent by TCP. Execute the following command.

[root@localhost~]#grep ^r lab2.tr | grep “tcp” -c

➢ To calculate the number of packets sent by UDP. Execute the following command.

[root@localhost~]#grep ^r lab2.tr | grep “cbr” –c

Program Type-II: Interfacing of the 4 nodes and setting data communication through TCP Data
type through NS 2 Simulator

set ns [new Simulator]

set f [open lab2.tr w]


$ns trace-all $f

set nf [open lab2.nam w]


$ns namtrace-all $nf

$ns color 1 "Blue"


$ns color 2 "Red"

proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exec nam lab2.nam &
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

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


$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 2.75Mb 20ms DropTail

$ns queue-limit $n2 $n3 50

set tcp0 [new Agent/TCP]

Department of E&CE, SVCE, Bengaluru Page 21


Computer Networks LAB (18ECL76) 2021-22

$ns attach-agent $n0 $tcp0


$tcp0 set class_ 1

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set sink [new Agent/TCPSink]


$ns attach-agent $n3 $sink
$ns connect $tcp0 $sink

set tcp1 [new Agent/TCP]


$ns attach-agent $n1 $tcp1
$tcp0 set class_ 2

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1

$ns attach-agent $n3 $sink


$ns connect $tcp1 $sink

$ns at 0.1 "$ftp0 start"


$ns at 1.0 "$ftp1 start"
$ns at 4.0 "$ftp0 stop"
$ns at 4.5 "$fpt1 stop"

$ns at 5.0 "finish"


$ns run

Variant Type-III: Interfacing of the 4 nodes and setting data communication through UDP/TCP
Data type through Packet Tracer
Procedure:

1. Develop a topology shown in the Figure 6 given below


2. Configure all the nodes
3. Verify the continuity using ping command from node1 to Server
4. Enable the ‘HTTP’ service in Server (node 3).
5. Generate web (HTTP) traffic from node 1, the procedure is as follows
a) Switch to Simulation mode
b) Click node 1 and click the desktop tab > Web Browser
c) In the URL, enter server IP address and click Go. Envelops (TCP PDUs) will
appear in the simulation window.
d) Minimize, but do not close, the node 1 configuration window
6. Disable the ‘HTTP’ service in Server. Now enable the ‘DNS’ service in Server.

Department of E&CE, SVCE, Bengaluru Page 22


Computer Networks LAB (18ECL76) 2021-22

Give DNS name & IP address.


7. Generate DNS traffic from node 1, the procedure is as follows
a) Switch to Simulation mode
b) Click node 1 and click the desktop tab > Web Browser
c) In the URL, enter server IP address and click Go. Envelops (UDP PDUs) will appear in the
simulation window.
d) Minimize, but do not close, the node 1 configuration window
8. Repeat same steps for node 0, analyze the TCP/UDP packets

Figure 6: Network Topology for web traffic

Department of E&CE, SVCE, Bengaluru Page 23


Computer Networks LAB (18ECL76) 2021-22

Experiments 3
Aim:
Implement Ethernet LAN using n (6-10) nodes. Compare the throughput by changing the error rate and
data rate.

Theory:
Local Area Network (LAN) is a data communication network connecting various terminals or computers
within a building or limited geographical area. The connection among the devices could wired or wireless.
Ethernet, Token Ring and Wireless LAN using IEEE 802.11 are examples of standard LAN technologies.

Variant Type-I: Implementation of Ethernet LAN and setting data communication through TCP
Data type through NS 2 Simulator

set ns [new Simulator]


set trf [open lab3.tr w]
$ns trace-all $trf
set naf [open lab3.nam w]
$ns namtrace-all $naf

proc finish {} {
global nf ns tf
exec nam lab3.nam &
close $naf
close $trf
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

$n1 label "Source"


$n2 label "Error Node"
$n5 label "Destination"

$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6" 10Mb 10ms LL Queue/DropTail Mac/802_3

Department of E&CE, SVCE, Bengaluru Page 24


Computer Networks LAB (18ECL76) 2021-22

$ns duplex-link $n2 $n6 30Mb 100ms DropTail

set udp0 [new Agent/UDP]


$ns attach-agent $n1 $udp0
set cbr0 [ new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set null5 [new Agent/Null]
$ns attach-agent $n5 $null5
$ns connect $udp0 $null5

$cbr0 set packetSize_ 100


$cbr0 set interval_ 0.001
$udp0 set class_ 1

set err [new ErrorModel]


$ns lossmodel $err $n2 $n6
$err set rate_ 0.7 #vary error rate 0.1, 0.4, 0.5 and 0.7

$ns at 6.0 "finish"


$ns at 0.1 "$cbr0 start"
$ns run

Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab3.tcl
➢ Save the program and quit.
➢ Run the simulation program
[root@localhost~]# ns lab3.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ To calculate the throughput. Execute the following command.
For calculating number of received packets
[root@localhost~]#grep ^r lab3.tr | grep “2 6” | awk ‘{s+=$6}END{print s}’
For calculating total time
[root@localhost~]#grep ^r lab3.tr | grep “2 6” | awk ‘{s+=$2}END{print s}’

Throughput = (Packet received/ Total Time) (bps)

➢ Write the value of throughput in observation sheet. Repeat the above step by changing the error rate to the
following line of the program.

Department of E&CE, SVCE, Bengaluru Page 25


Computer Networks LAB (18ECL76) 2021-22

$err set rate_ 0.7 #vary error rate 0.1, 0.4, 0.5 and 0.7

Sl. Error rate Throughput


No.
1. 0.1
2. 0.4
3. 0.5
4. 0.7

➢ Plot a graph with x- axis with Error rate and y-axis with Throughput.

Variant Type-II: Implementation of Ethernet LAN and setting data communication through TCP
Data type through NS 2 Simulator

set ns [new Simulator]


set trf [open lab3.tr w]
$ns trace-all $trf
set naf [open lab3.nam w]
$ns namtrace-all $naf

proc finish {} {
global naf ns trf
exec nam lab3.nam &
close $naf
close $trf
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

$n1 label "Source"


$n2 label "Error Node"
$n5 label "Destination"

$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6" 10Mb 10ms LL Queue/DropTail Mac/802_3

Department of E&CE, SVCE, Bengaluru Page 26


Computer Networks LAB (18ECL76) 2021-22

$ns duplex-link $n2 $n6 30Mb 100ms DropTail

set tcp0 [new Agent/TCP]


$ns attach-agent $n1 $tcp0
$tcp0 set class_ 1

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set sink [new Agent/TCPSink]


$ns attach-agent $n5 $sink
$ns connect $tcp0 $sink

set err [new ErrorModel]


$ns lossmodel $err $n2 $n6
$err set rate_ 0.7 #vary error rate 0.1, 0.4, 0.5 and 0.7

$ns at 6.0 "finish"


$ns at 0.1 "$ftp0 start"
$ns run

Variant Type III: Using Packet Tracer

Procedure:
➢ Develop a topology shown in the figure given below
➢ Configure all the workstations
➢ Configure the switch & Router
➢ Verify the continuity using ping command
➢ Compare the throughput by changing the error rate and data rate

Department of E&CE, SVCE, Bengaluru Page 27


Computer Networks LAB (18ECL76) 2021-22

Figure 7: Network Topology for variable error rates

Department of E&CE, SVCE, Bengaluru Page 28


Computer Networks LAB (18ECL76) 2021-22

Experiment 4

Aim: Implement Ethernet LAN using n nodes and assign multiple traffic to the nodes and obtain
congestion window for different sources/ destinations.

Theory:

Network congestion occurs when packets sent from the source (the server) exceed what the destination
can handle (the user). When the buffers get filled up at the destination, the packets are temporarily stored
in buffers at both the source and destination as they wait to be forwarded to the upper layers. This can lead
to congestion and cause packet loss, retransmissions, reduced data throughput, performance degradation,
and even network collapse in extreme cases.
TCP can use the congestion window (cwnd) and the receiver window (rwnd) to control and avoid network
congestion. The amount of data that can be transmitted through a TCP connection is dependent on the
congestion window, which is maintained by the source. The receiver window is maintained by the
destination.
Variant Type I:

set ns [new Simulator]


set f [open lab4.tr w]
$ns trace-all $f

set nf [open lab4.nam w]


$ns namtrace-all $nf

proc finish {} {
global ns f nf outFile1 outFile2
$ns flush-trace
close $f
close $nf
exec nam lab4.nam &
exec xgraph Congestion1.xg Congestion2.xg -geometry 400x400&
exit 0
}

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
Department of E&CE, SVCE, Bengaluru Page 29
Computer Networks LAB (18ECL76) 2021-22

set n4 [$ns node]


set n5 [$ns node]

$n0 label "Src1"


$n4 label "Dst1"
$n1 label "Src2"
$n5 label "Dst2"

$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 " 10Mb 30ms LL Queue/DropTail Mac/802_3

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp1 attach-agent $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n4 $sink0
$ftp0 set maxPkts_ 1000
$ns connect $tcp1 $sink0

set tcp1 [new Agent/TCP/Reno]


$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ftp2 set maxPkts_ 1000
$ns connect $tcp2 $sink2

set outFile1 [open Congestion1.xg w]


set outFile2 [open Congestion2.xg w]

proc findWindowSize {tcpSource outFile} {


global ns
set now [$ns now]
set cWindSize [$tcpSource set cwnd_]
puts $outFile "$now $cWindSize"
$ns at [expr $now + 0.1] "findWindowSize $tcpSource $outFile"
}

$ns at 0.0 "findWindowSize $tcp1 $outFile1"


$ns at 0.1 "findWindowSize $tcp2 $outFile2"
$ns at 0.3 "$ftp0 start"
$ns at 0.5 "$ftp1 start"
$ns at 50.0 "$ftp0 stop"
Department of E&CE, SVCE, Bengaluru Page 30
Computer Networks LAB (18ECL76) 2021-22

$ns at 50.0 "$ftp1 stop"


$ns at 50.0 "finish"
$ns run

Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab4.tcl
➢ Save the program and quit.
➢ Run the simulation program
[root@localhost~]# ns lab4.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ The xgraph automatically calculates and plot the two graph of Congestion window with TCP1 and TCP2.
Variant Type II:
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files
set file1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace file
set file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam &
exit
}
#Create six 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]
$n1 color red
$n1 shape box

Department of E&CE, SVCE, Bengaluru Page 31


Computer Networks LAB (18ECL76) 2021-22

#Create links between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Ca Channel]
Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the
# tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file} {
global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
Department of E&CE, SVCE, Bengaluru Page 32
Computer Networks LAB (18ECL76) 2021-22

set wnd [$tcpSource set window_]


puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet drop\""
# PPP
$ns at 125.0 "finish"
$ns run

Variant Type III:


Procedure:

➢ Develop a topology shown in the figure given below


➢ Configure all the nodes
➢ Configure the switch & Router
➢ Verify the continuity using ping command
➢ Verify congestion window for different sources / destinations

Figure 8: Wireless LAN topology

Department of E&CE, SVCE, Bengaluru Page 33


Computer Networks LAB (18ECL76) 2021-22

Experiment 5

Aim: Implement ESS with transmission nodes in Wireless LAN and obtain the performance parameters.
Theory:
An extended service set (ESS) is one or more interconnected basic service sets (BSSs) and their associated
LANs. Each BSS consists of a single access point (AP) together with all wireless client devices (stations,
also called STAs) creating a local or enterprise 802.11 wireless LAN (WLAN). To the logical link control
layer, the ESS appears as a solitary BSS at any one of the STAs.

Variant Type I:

set ns [new Simulator]


set tf [open lab5.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1300 1300
set nf [open lab5.nam w]
$ns namtrace-all-wireless $nf 1300 1300

$ns node-config -adhocRouting DSDV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue\
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "ESS"
$n1 label "mob1"
$n2 label "mob2"
$n0 set X_ 10
$n0 set Y_ 600
Department of E&CE, SVCE, Bengaluru Page 34
Computer Networks LAB (18ECL76) 2021-22

$n0 set Z_ 0
$n1 set X_ 80
$n1 set Y_ 600
$n1 set Z_ 0
$n2 set X_ 1200
$n2 set Y_ 600
$n2 set Z_ 0
$ns at 0.1 "$n0 setdest 10 600 15"
$ns at 0.1 "$n1 setdest 80 600 25"
$ns at 0.1 "$n2 setdest 1200 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 $n0 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2

$ns at 2 "$ftp0 start"


$ns at 15 "$ftp1 start"
$ns at 3 "$n1 setdest 1000 600 250"
$ns at 3 "$n2 setdest 80 600 250"

proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab5.nam &
close $tf
exit 0
}
$ns at 20 "finish"
$ns run
Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab5.tcl

Department of E&CE, SVCE, Bengaluru Page 35


Computer Networks LAB (18ECL76) 2021-22

➢ Save the program and quit.


➢ Run the simulation program
[root@localhost~]# ns lab5.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ To calculate the throughput. Execute the following command.
For calculating number of received packets
[root@localhost~]#grep ^r lab5.tr | grep “AGT” | grep “tcp” | awk ‘{s+=$8}END{print s}’
For calculating total time
[root@localhost~]#grep ^r lab5.tr | grep “AGT” | grep “tcp” | awk ‘{s+=$2}END{print s}’

Throughput = (Packet received/ Total Time) (bps)

Variant Type II:

set ns [new Simulator]


set tf [open lab5.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1300 1300
set nf [open lab5.nam w]
$ns namtrace-all-wireless $nf 1300 1300

$ns node-config -adhocRouting DSDV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue\
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "ESS"
$n1 label "mob1"
$n2 label "mob2"
$n0 set X_ 10
$n0 set Y_ 600
$n0 set Z_ 0
$n1 set X_ 80

Department of E&CE, SVCE, Bengaluru Page 36


Computer Networks LAB (18ECL76) 2021-22

$n1 set Y_ 600


$n1 set Z_ 0
$n2 set X_ 1200
$n2 set Y_ 600
$n2 set Z_ 0
$ns at 0.1 "$n0 setdest 10 600 15"
$ns at 0.1 "$n1 setdest 80 600 25"
$ns at 0.1 "$n2 setdest 1200 600 25"

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005

set null0 [new Agent/Null]


$ns attach-agent $n1 $null0
$ns connect $udp0 $null0

set udp1 [new Agent/UDP]


$ns attach-agent $n0 $udp1
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005

set null0 [new Agent/Null]


$ns attach-agent $n2 $null1
$ns connect $udp1 $null1

$ns at 2 "$udp0 start"


$ns at 15 "$udp1 start"
$ns at 3 "$n1 setdest 1000 600 250"
$ns at 3 "$n2 setdest 80 600 250"

proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab5.nam &
close $tf
exit 0
}
$ns at 20 "finish"
$ns run

Department of E&CE, SVCE, Bengaluru Page 37


Computer Networks LAB (18ECL76) 2021-22

Variant Type III:

Procedure:
➢ Develop a topology shown in the figure given below
➢ Configure access points AP1 & AP2 with different SSID names. port status should be
turned on
➢ Connect a hosts and two WLAN access points to a router
➢ Setup nodes around the two WLAN access points and set the path for each node
Verify the 802.11 wireless parameters in simulation mode

Figure 9: ESS network topology

Department of E&CE, SVCE, Bengaluru Page 38


Computer Networks LAB (18ECL76) 2021-22

Experiment 6

Aim: Implementation of Link state routing algorithm.

Theory:

In link state routing, each router shares its knowledge of its neighborhood with every other router in the
internet work. (i) Knowledge about Neighborhood: Instead of sending its entire routing table a router
sends info about its neighborhood only. (ii) To all Routers: each router sends this information to every
other router on the internet work not just to its neighbor. It does so by a process called flooding.
(iii)Information sharing when there is a change: Each router sends out information about the neighbors
when there is change.
Variant Type I:

set ns [new Simulator]

$ns rtproto LS

set nf [open lab6.nam w]


$ns namtrace-all $nf

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam lab6.nam &
exit 0
}

for {set i 0} {$i < 7} {incr i} {


set n($i) [$ns node]
}

for {set i 0} {$i < 7} {incr i} {


$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

set udp0 [new Agent/UDP]


$ns attach-agent $n(0) $udp0

Department of E&CE, SVCE, Bengaluru Page 39


Computer Networks LAB (18ECL76) 2021-22

set cbr0 [new Application/Traffic/CBR]


$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]


$ns attach-agent $n(3) $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"


$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"

$ns run

Steps for execution:

➢ Open gedit editor and type program. Program name should have the extension “ .tcl ”
[root@localhost ~]# gedit lab6.tcl
➢ Save the program and quit.
➢ Run the simulation program
[root@localhost~]# ns lab6.tcl
➢ Here “ns” indicates network simulator. We get the topology shown in the network animator. Now press the
play button in the simulation window and the simulation will begins.
➢ Explain link state routing algorithm using animation. How link state break and rerouting take place.

Variant Type II:


set ns [new Simulator]

$ns rtproto LS

set nf [open lab6.nam w]


$ns namtrace-all $nf

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam lab6.nam &
exit 0

Department of E&CE, SVCE, Bengaluru Page 40


Computer Networks LAB (18ECL76) 2021-22

for {set i 0} {$i < 7} {incr i} {


set n($i) [$ns node]
}

for {set i 0} {$i < 7} {incr i} {


$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

set null0 [new Agent/Null]


$ns attach-agent $n(3) $null0

set tcp0 [new Agent/TCP]


$ns attach-agent $n(0) $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0

set sink0 [new Agent/TCPSink]


$ns attach-agent $n(3) $sink0
$ns connect $tcp0 $sink0

$ns at 0.5 "$ftp0 start"


$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$ftp0 stop"
$ns at 5.0 "finish"

$ns run

Variant Type III:


Procedure:

➢ Develop a topology shown in the figure given below


➢ Configure all the workstations
➢ Configure all switches
➢ Configure all Routers
➢ Implement OSPF protocols in Router to configure network

Department of E&CE, SVCE, Bengaluru Page 41


Computer Networks LAB (18ECL76) 2021-22

Figure 10: Link State Protocol Network Topology

Department of E&CE, SVCE, Bengaluru Page 42


Computer Networks LAB (18ECL76) 2021-22

PART-B:

Implement the following in C/C++


1. Write a program for a HLDC frame to perform the following.
i) Bit stuffing
ii) Character stuffing.
2. Write a program for distance vector algorithm to find suitable path for
transmission.
3. Implement Dijkstra’s algorithm to compute the shortest routing path.
4. For the given data, use CRC-CCITT polynomial to obtain CRC code.
Verify the program for the cases
a. Without error
b. With error
5. Implementation of Stop and Wait Protocol and Sliding Window Protocol
6. Write a program for congestion control using leaky bucket algorithm.

Department of E&CE, SVCE, Bengaluru Page 43


Computer Networks LAB (18ECL76) 2021-22

1. Write a program for a HLDC frame to perform the following.


i) Bit stuffing

Theory:
High-Level Data Link Control (HDLC) generally uses term “frame” to indicate and represent an entity of
data or a protocol of data unit often transmitted or transferred from one station to another station. Each
and every frame on link should begin and end with Flag Sequence Field (F). Each of frames in HDLC
includes mainly six fields. It begins with a flag field, an address field, a control field, an information field,
an frame check sequence (FCS) field, and an ending flag field. The ending flag field of one frame can
serve as beginning flag field of the next frame in multiple-frame transmissions.

HDLC is a bit-oriented code-transparent synchronous data link layer protocol developed by the
International Organization for Standardization. HDLC frames can be transmitted over synchronous or
asynchronous serial communication links. Those links have no mechanism to mark the beginning or end
of a frame, so the beginning and end of each frame has to be identified. This is done by using a frame
delimiter, or flag, which is a unique sequence of bits that is guaranteed not to be seen inside a frame.
This sequence is '01111110', or, in hexadecimal notation, 0x7E. Each frame begins and ends with a
frame delimiter. A frame delimiter at the end of a frame may also mark the start of the next frame. A
sequence of 7 or more consecutive 1-bits within a frame will cause the frame to be aborted.

When no frames are being transmitted on a simplex or full-duplex synchronous link, a frame delimiter is
continuously transmitted on the link. Using the standard NRZI encoding from bits to line levels (0 bit =
transition, 1 bit = no transition), this generates one of two continuous waveforms, depending on the initial
state:

This is used by modems to train and synchronize their clocks via phase-locked loops. Some protocols
allow the 0-bit at the end of a frame delimiter to be shared with the start of the next frame delimiter, i.e.
'011111101111110'.

Department of E&CE, SVCE, Bengaluru Page 44


Computer Networks LAB (18ECL76) 2021-22

For half-duplex or multi-drop communication, where several transmitters share a line, a receiver on the
line will see continuous idling 1-bits in the inter-frame period when no transmitter is active.

Since the flag sequence could appear in user data, such sequences must be modified during transmission
to keep the receiver from detecting a false frame delimiter. The receiver must also detect when this has
occurred so that the original data stream can be restored before it is passed to higher layer protocols. This
can be done using bit stuffing, in which a "0" is added after the occurrence of every "11111" in the data.
When the receiver detects these "11111" in the data, it removes the "0" added by the transmitter. Bit
stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5 consecutive 1's.

Variant Type I:
#include<stdio.h>
int main()
{
int a15];
int i,j,k,n,c=0,pos=0;
printf("\n Enter the number of bits");
scanf("%d",&n);
printf("\n Enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}

printf("\n DATA AFTER STUFFING \n");

Department of E&CE, SVCE, Bengaluru Page 45


Computer Networks LAB (18ECL76) 2021-22

for(i=0;i<n;i++) {
printf("%d",a[i]);
}

Execution:

[root@localhost code]# gcc 1a.c


[root@localhost code]# ./a.out

Enter the number of bits


12

Enter the bits


1
0
1
1
1
1
1
1
1
0
1
1

DATA AFTER STUFFING


1011111011011

Variant Type II:


#include<string.h>
#include<stdio.h>
void main()
{
int si=0, di=0, count=0, StuffedLen; char src[max],des[max];
char flag[max]="01111110";

printf("enter the msg bits:\n");


for (si= 0; si<8; si++)
scanf("%s",&src[si]);
si=0;
di = strlen(flag) ;

Department of E&CE, SVCE, Bengaluru Page 46


Computer Networks LAB (18ECL76) 2021-22

strcpy(des,flag);
di=strlen(flag);
while(src[si]!='\0')
{
if(src[si]=='1')
count ++;
else
count = 0;
des[di++]=src[si++];
if (count == 5)
{
count = 0;
des[di++]='0';
}
}

des[di++]='\0';
printf("stuffed message is %s ", strcat(des,flag));
si = strlen(flag);
StuffedLen = strlen(des)-2*si;
for(di=0;di<StuffedLen; si++, di++)
src[di]=des[si];
src[di] = '\0'; count = 0;
si = 0; di =0;
while (src[si]!='\0')
{
if(src[si]=='1')
count++;
else
count = 0;
des[di++]=src[si++]; if(count==5)
{
count = 0; si++;
//des[di++]=src[si++];
}
}
des[di]='\0';
printf("The destuffed data is %s" , des);

OUTPUT:
Enter the msg bits 01111111
Stuffed message is 0111111001111101101111110
Destuffed msg is 01111111
Department of E&CE, SVCE, Bengaluru Page 47
Computer Networks LAB (18ECL76) 2021-22

ii) Character stuffing.


Character Stuffing is process in which DLESTX and DLEETX are used to denote start and end of character data
with some constraints imposed on repetition of characters.

Variant Type I:
Program:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
char c[50],d[50],t[50];
int i,m,j;
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;

Department of E&CE, SVCE, Bengaluru Page 48


Computer Networks LAB (18ECL76) 2021-22

d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i <m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
{printf("%c",t[i]);
}
}

Execution:
[root@localhost code]# gcc 1b.c
[root@localhost code]# ./a.out
enter the number of characters
9

enter the characters


abcdabcde

original data

abcdabcde

transmitted data:
dlestx
abcdabcdedleetx

received data:
abcdabcde

Department of E&CE, SVCE, Bengaluru Page 49


Computer Networks LAB (18ECL76) 2021-22

Variant Type II:


#include<stdio.h>
#include<conio.h>
#include<string.h> #define
max 100
void main()

{
clrscr();
int si=0, di =0;
char flag1[max]="DLESTX", flag2[max]="DLEETX", des[max], src[max];
printf("enter the string data:\n"); scanf("%s",&src);
/*stuffing process*/
strcpy(des,flag1);
di=strlen(flag1);
while(src[si]!='\0')
{
if(src[si]=='D'&& src[si+1]=='L'&& src[si+2]=='E')
{
des[di+0]='D', des[di+1]='L', des[di+2]='E',
des[di+3]='D', des[di+4]='L', des[di+5]='E';
di+=6;
si+=3;
}
else des[di++]=src[si++];
}
des[di]='\0';
strcat(des,flag2);
printf("\nthe stuffed string is %s\n",des);

/*destuffing process*/

di = strlen(des)-strlen(flag2);
des[di]='\0';
di=strlen(flag1); for(si=0;des[di]!='\0';si++,di++)
src[si]=des[di];
si=di=0;
while(src[si]!='\0')
{
if(src[si]=='D'&&src[si+1]=='L'&&src[si+2]=='E'&&
src[si+3]=='D'&&src[si+4]=='L'&&src[si+5]=='E')
{

dedi]='D', des[di+1]='L', des[di+2]='E'; di+=3;


si+=6;
else

Department of E&CE, SVCE, Bengaluru Page 50


Computer Networks LAB (18ECL76) 2021-22

des[di++=src[si++];
}
des[di]='\0';
printf("the destuffed string is %s", des); getch();
}

Department of E&CE, SVCE, Bengaluru Page 51


Computer Networks LAB (18ECL76) 2021-22

Variant Type III:

set ns [new Simulator]


#Tell the simulator to use dynamic routing
$ns rtproto DV
$ns macType Mac/Sat/UnslottedAloha
#Open the nam trace file
set nf [open aloha.nam w]
$ns namtrace-all
$nf #Open the output files
set f0 [open aloha.tr w]
$ns trace-all $f0
#Define a finish procedure
proc finish {} {
global ns f0 nf
$ns flush-trace #Close the trace file close $f0
close $nf
exec nam aloha.nam & exit 0
}
# Create six 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]
# Create duplex links between nodes with bandwidth and distance
$ns duplex-link $n0 $n4 1Mb 50ms DropTail
$ns duplex-link $n1 $n4 1Mb 50ms DropTail
$ns duplex-link $n2 $n5 1Mb 1ms DropTail
$ns duplex-link $n3 $n5 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 50ms DropTail
$ns duplex-link $n2 $n3 1Mb 50ms DropTail
# Create a duplex link between nodes 4 and 5 as queue position
$ns duplex-link-op $n4 $n5 queuePos 0.5
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n(2)
set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
Department of E&CE, SVCE, Bengaluru Page 52
Computer Networks LAB (18ECL76) 2021-22

$ns at 0.5 "$cbr0 start"


$ns rtmodel-at 1.0 down $n5 $n2
$ns rtmodel-at 2.0 up $n5 $n2
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

OUTPUT:

Figure 11: HLDC dataflow in distance vector protocol

Department of E&CE, SVCE, Bengaluru Page 53


Computer Networks LAB (18ECL76) 2021-22

2. Write a program for distance vector algorithm to find suitable path for
transmission.

Theory:

Distance vector routing algorithms operate by having each router maintain a table (i.e.,
vector) giving the best-known distance to each destination and which line to get there. These
tables are updated by exchanging information with the neighbors. A distance-vector routing
protocol in data networks determines the best route for data packets based on distance.
Distance-vector routing protocols measure the distance by the number of routers a packet has
to pass, one router counts as one hop.

Variant Type I:

#include<stdio.h>

struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];

int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix (999 for no link):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
Department of E&CE, SVCE, Bengaluru Page 54
Computer Networks LAB (18ECL76) 2021-22

rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d", j+1,rt[i].from[j]+1, rt[i].dist[j]);
}
}
printf("\n\n");
}

Execution:

[root@localhost code]# gcc 2.c


[root@localhost code]# ./a.out

Enter the number of nodes : 4

Enter the cost matrix (999 for no link):

0 2 999 1
2052
999 5 0 6
1260

State value for router 1 is

node 1 via 1 Distance0


node 2 via 2 Distance2
node 3 via 2 Distance7
node 4 via 4 Distance1

State value for router 2 is

node 1 via 1 Distance2


node 2 via 2 Distance0
node 3 via 3 Distance5
node 4 via 4 Distance2

State value for router 3 is

node 1 via 2 Distance7


node 2 via 2 Distance5
node 3 via 3 Distance0
node 4 via 4 Distance6

State value for router 4 is

Department of E&CE, SVCE, Bengaluru Page 55


Computer Networks LAB (18ECL76) 2021-22

node 1 via 1 Distance1


node 2 via 2 Distance2
node 3 via 3 Distance6
node 4 via 4 Distance0

Variant Type II:

#include<stdio.h>
struct rtable
{
int dist[20],nextnode[20];
}table[20];
int cost[10][10],n;
void distvector()
{
int i,j,k,count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
table[i].dist[j]=cost[i][j];
table[i].nextnode[j]=j;
}
}
do
{
count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(table[i].dist[j]>cost[i][k]+table[k].dist[j])
{
table[i].dist[j]=table[i].dist[k]+table[k].dist[j];
table[i].nextnode[j]=k;
count++;
}
}
}
}
}while(count!=0);
}
int main()
{
int i,j;
printf("\nenter the no of vertices:\t");
scanf("%d",&n);
printf("\nenter the cost matrix\n");
for(i=0;i<n;i++)
Department of E&CE, SVCE, Bengaluru Page 56
Computer Networks LAB (18ECL76) 2021-22

for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
distvector();
for(i=0;i<n;i++)
{
printf("\nstate value for router %c \n",i+65);
printf("\ndestnode\tnextnode\tdistance\n");
for(j=0;j<n;j++)
{
if(table[i].dist[j]==99)
printf("%c\t\t-\t\tinfinite\n",j+65);
else
printf("%c\t\t%c\t\t%d\n",j+65,table[i].nextnode[j]+65,table[i].dist[j]);
}}
return 0;
}

OUTPUT:

Figure 12: Output window for the Distance vector routing protocol
Variant Type III:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf

proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {

$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }


$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail

Department of E&CE, SVCE, Bengaluru Page 57


Computer Networks LAB (18ECL76) 2021-22

$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail


$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail

set udp0 [new Agent/UDP]


$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]


$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0

set udp1 [new Agent/UDP]


$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0

$ns rtproto DV
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)

$udp0 set fid_ 1


$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green

$ns at 1.0 "$cbr0 start"


$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run

OUTPUT:

Figure 13: Animator window for the DS protocol


Department of E&CE, SVCE, Bengaluru Page 58
Computer Networks LAB (18ECL76) 2021-22

3. Implement Dijkstra’s algorithm to compute the shortest routing path


All packet needs to be routed from the source to the destination and in most cases multiple
hopes are required. Routing Algorithms are required to decide on which output line and
incoming pocket should be forwarded into. Two types Routing Algorithms now come in-one
where the decision is fixed and one where the route is constantly recalculated for the best
route in other words static and dynamic routing algorithms or non-adaptive and adaptive
algorithms. Routing algorithms can be made to adapt to average traffic/loss/distance queue
length etc.

Dijkstra’s method of computing the shortest path is a static routing algorithm. It involves
building a graph of the subnet, with each node of the graph representing a router and each are
representing a communication line or a link. to find a route between a pair of routers the
algorithm just finds the shortest path between the them on the graph.

In Dijkstra’s algorithms the metric used for calculation is distance. Each node is labled with
its distance from the source node along with the previous node in the path. initially no paths
are known so all nodes are labled with infinity. As the algorithm proceeds and paths are
found, the labels may change reflecting better paths. A label may be either tentative or
permanent. Initially all nodes are tentative and once it is discovered that the shortest possible
path to a node is got it is made permanent and never changed after that Dijkstra algorithm is
also called single source shortest path algorithm. The algorithm maintains a list visited [] of
vertices, whose shortest distance from the source is calculated.

Variant Type I:
#include<stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);

Department of E&CE, SVCE, Bengaluru Page 59


Computer Networks LAB (18ECL76) 2021-22

dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;

for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}

visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);

Department of E&CE, SVCE, Bengaluru Page 60


Computer Networks LAB (18ECL76) 2021-22

}while(j!=startnode);
}
}
Execution:

[root@localhost code]# gcc 3.c


[root@localhost code]# ./a.out
Enter no. of vertices:5

Enter the adjacency matrix:


0 10 0 30 100
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
100 0 10 60 0

Enter the starting node:0

Distance of node1=10
Path=1<-0
Distance of node2=50
Path=2<-3<-0
Distance of node3=30
Path=3<-0
Distance of node4=60
Path=4<-2<-3<-0

Variant Type II:


#include <stdio.h>
#include <conio.h>
int p[10][10];
void main()
{
int i,j,k,n,t; int m[10][10], w[10][10];
void path(int a,int b);
clrscr();
printf("enter the number of nodes");
scanf ("%d",&n);
printf("\n Enter the nodes connection matrix");
printf("\n To indicate no connection between two nodes, enter weight as 100\n\n");
/* prints the outline of the matrix */
for(i=1;i<=n;i++)
printf("\t %d",i);
for(i=1;i<=n;i++)
{
printf("\n %d \t",i);
/* Takes the entered values as input */
for(j=1;j<=n;j++)
{
scanf("%d",&w[i][j]);
m[i][j]=w[i][j];
p[i][j]=0;
}
}

Department of E&CE, SVCE, Bengaluru Page 61


Computer Networks LAB (18ECL76) 2021-22

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(m[i][k]+m[k][j]<m[i][j])

{
m[i][j] =m[i][k] + m[k][j];
p[i][j] = k;
}
do
{
printf("\n enter the source & Destination nodes\n");
scanf("%d %d",&i,&j);
printf("\n the weight is %d", m[i][j]);
printf("\n the path is");
printf( "%d-->",i);
path(i,j);
printf("%d", j);
printf("\n to repeat press R");
}
while(getch()=='R');
}
void path(int i, int j)
{
int k;
k=p[i][j];
if(k!=0)
{
path(i,k);
printf("%d-->",k);
path(k,j);
}
}
OUTPUT:

Figure 14: Network Topology


Variant Type III:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
Department of E&CE, SVCE, Bengaluru Page 62
Computer Networks LAB (18ECL76) 2021-22

$ns namtrace-all $nf


proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
for {set i 0} { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto LS
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run

Department of E&CE, SVCE, Bengaluru Page 63


Computer Networks LAB (18ECL76) 2021-22

Figure 15: Animator window for the Dijkstra’s algorithm

Department of E&CE, SVCE, Bengaluru Page 64


Computer Networks LAB (18ECL76) 2021-22

4. For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify
the program for the cases
a. Without error
b. With error

The polynomial code (also known as a Cyclic Redundancy code or CRC code) is widely
used. polynomial codes are based upon treating bit string as representations of polynomials
with coefficients of 0 and 1 only. A k-bit frame is regarded as the coefficient list for a
polynomial with k terms, ranging from x k-1 to x0. Such a polynomial is said to be of a
degree k-1. The high order (left most) bit is the coefficient of x k-1; the next bit is the
coefficient of x k-2 and so on. For example, 110001 has 6 bits and thus represents a six-term
polynomial with coefficients.1,1,0,0,0 and 1: x5+x4+1.
When the polynomial code method is employed, the centre and receiver must agree upon a
generator polynomial, G(x) in advanced, both the high and low order bits of the generator
must be one. To compute the checksum for some frame with m bits, corresponding to the
polynomial M(x) the frame must be longer than the generator polynomial. the idea is so to
append a checksum to the end of the frame in such a way that the polynomial represented by
the check summed frame is divisible by G(x).When the receiver gets the check summed
frame, it tries dividing it by G(x).If there is a remember there has been a transmission error.
Cyclic Redundancy Check is error checking technique it does error checking via polynomial
division.
Variant Type I:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N strlen(g)
char t[128],cs[128],g[]="1000100000010001";
int a,e,c;
void xor(){
for(c=1;c<N;c++)
cs[c]=((cs[c]==g[c])?'0':'1');
}
void crc(){
for(e=0;e<N;e++)
cs[e]=t[e];
do{
if(cs[0]=='1')
xor();
for(c=0;c<N-1;c++)
Department of E&CE, SVCE, Bengaluru Page 65
Computer Networks LAB (18ECL76) 2021-22

cs[c]=cs[c+1];
cs[c]=t[e++];
}while(e<=a+N-1);
}
int main(){
printf("\nEnter poly:");
scanf("%s",t);
printf("\nGenerating Polynomial is: %s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\nModified t[u] is: %s",t);
crc();
printf("\nChecksum is: %s",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\nFinal Codeword is: %s",t);
printf("\nTest Error detection 0 (Yes) 1 (No) ? : ");
scanf("%d",&e);
if(e==0){
printf("Enter postion where error is to be inserted:");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Erroneous Data: %s\n",t);
}
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1)
printf("Error Detected");
else printf("No Error Detected");
return 0;
}

Execution:
[root@localhost code]# gcc 4.c
[root@localhost code]# ./a.out

Enter poly : 1011101


Generating Polynomial is : 10001000000100001
Modified t[u] is : 10111010000000000000000
Checksum is : 1000101101011000
Final Codeword is : 10111011000101101011000
Test Error detection 0(yes) 1(no) ? : 0
Enter position where you want to insert error : 3
Errorneous data : 10101011000101101011000
Error detected.

Department of E&CE, SVCE, Bengaluru Page 66


Computer Networks LAB (18ECL76) 2021-22

Enter poly : 1011101


Generating Polynomial is : 10001000000100001
Modified t[u] is : 10111010000000000000000
Checksum is : 1000101101011000
Final Codeword is : 10111011000101101011000
Test Error detection 0(yes) 1(no) ? : 1
0No Error Detected.

Variant Type II:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define degree 3
int res[30];
int getnext(int array[],int);
void crc(int len)
{
int cp[]={1,0,1,1};
int i=0,pos=0,newpos;
while(pos<len-degree)
{
for(i= pos;i<pos+degree+1;++i)
res[i]=res[i]^cp[i-pos];
newpos=getnext(res,pos);
if(newpos>pos+1)
pos= newpos-1;
++pos;
}
}
int getnext(int array[], int pos)
{
int i = pos;
while( array[i] ==0)
++i;
return i;
}
void main()
{
int array[30];
char ch;
int len,i=0;
puts("enter the data stream:");
while((ch =getche())!='\r')
array[i++] = ch-'0';
len=i;
for(i=0;i<degree; ++i)

Department of E&CE, SVCE, Bengaluru Page 67


Computer Networks LAB (18ECL76) 2021-22

array[i+len]=0;
len+= degree;
for(i=0;i<len;i++)
res[i]=array[i];
crc(len);
printf("\n the transmitted frame is: ");
for(i=0; i<len-degree; ++i)
printf("%d", array[i]);
for(i=len-degree; i<len;++i)
printf("%d", res[i]);
printf("\nEnter the stream for which crc to be checked");
i=0;
while((ch=getche())!='\r')
array[i++]= ch-'0';
len=i;
for(i=0;i<len;i++)
res[i]=array[i];
crc(len);
printf("\n checksum: ");
for(i=len-degree; i<len;i++)
printf("%d", res[i]);
}

OUTPUT:

Without error
Enter data stream 10011
Transmitted frame is 10011100
Enter the stream for which CRC to be checked 10011100
Checksum: 000

With error
Enter data stream 10011
Transmitted frame is 10011100
Enter the stream for which crc to be checked 10011101
Checksum: 001

Variant Type III:

#include<stdio.h>
char m[50],g[50],r[50],q[50],temp[50];
void caltrans(int);
void crc(int);
void calram();
void shiftl();
int main()
{

Department of E&CE, SVCE, Bengaluru Page 68


Computer Networks LAB (18ECL76) 2021-22

int n,i=0;
char ch,flag=0;
printf("Enter the frame bits:");
while((ch=getc(stdin))!='\n')
m[i++]=ch;
n=i;
for(i=0;i<16;i++)
m[n++]='0';
m[n]='\0';
printf("Message after appending 16 zeros:%s",m);
for(i=0;i<=16;i++)
g[i]='0';
g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
printf("\ngenerator:%s\n",g);
crc(n);
printf("\n\nquotient:%s",q);
caltrans(n);
printf("\ntransmitted frame:%s",m);
printf("\nEnter transmitted frame:");
scanf("\n%s",m);
printf("CRC checking\n");
crc(n);
printf("\n\nlast remainder:%s",r);
for(i=0;i<16;i++)
if(r[i]!='0')
flag=1;
else
continue;
if(flag==1)
printf("Error during transmission");
else
printf("\n\nReceived freme is correct");
}
void crc(int n)
{
int i,j;
for(i=0;i<n;i++)
temp[i]=m[i];
for(i=0;i<16;i++)
r[i]=m[i];
printf("\nintermediate remainder\n");
for(i=0;i<n-16;i++)
{
if(r[0]=='1')
{
q[i]='1';
calram();

Department of E&CE, SVCE, Bengaluru Page 69


Computer Networks LAB (18ECL76) 2021-22

}
else
{
q[i]='0';
shiftl();
}
r[16]=m[17+i];
r[17]='\0';
printf("\nremainder %d:%s",i+1,r);
for(j=0;j<=17;j++)
temp[j]=r[j];
}
q[n-16]='\0';
}
void calram()
{
int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}
void shiftl()
{
int i;
for(i=1;i<=16;i++)
r[i-1]=r[i];
}
void caltrans(int n)
{
int i,k=0;
for(i=n-16;i<n;i++)
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}

Enter the Frame Bits:


1011
The msg after appending 16 zeros:
10110000000000000000
The Transmitted frame is:10111011000101101011
Enter the transmitted Frame
10111011000101101011
Received msg:10111011000101101011
The Remainder is:0000000000000000
Received frame is correct.

5. Implementation of Stop and Wait Protocol and Sliding Window Protocol

Department of E&CE, SVCE, Bengaluru Page 70


Computer Networks LAB (18ECL76) 2021-22

5a. Stop and Wait protocol

Theory:
In computer networks sliding window protocol is a method to transmit data on a
network. Stop and wait protocol and Sliding window protocol are applied on the Data
Link Layer of OSI model. At data link layer data is in the form of frames. In
Networking, Window simply means a buffer which has data frames that needs to be
transmitted.

Flow Control:
Consider a situation in which the sender transmits frames faster than the receiver
can accept them. If the sender keeps pumping out frames at high rate, at some point the
receiver will be completely swamped and will start losing some frames. This problem
may be solved by introducing flow control. Most flow control protocols contain a
feedback mechanism to inform the sender when it should transmit the next frame.

Variant Type I:

#include<stdio.h>
#define w 1
int main()
{
int i,f,frames[50];
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith stop and wait protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);
printf("if error occur negative acknowledge is detected same frame is resend back\n");
for(i=1;i<=f;i++)
{
if((random()%2)==1)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
else
{
sleep(3);
printf("negative acknowledge resend %d frame\n",i);
i=i-1;
sleep(1);
Department of E&CE, SVCE, Bengaluru Page 71
Computer Networks LAB (18ECL76) 2021-22

}
return 0;
}

Execution:

[root@localhost code]# gcc 5b.c


[root@localhost code]# ./a.out

Enter number of frames to transmit: 6

Enter 6 frames: 1
4
5
7
8
9

With stop and wait protocol the frames will be sent in the following manner (assuming no corruption
of frames)

After sending 1 frames at each stage sender waits for acknowledgement sent by the receiver

if error occur negative acknowledge is detected same frame is resend back


1
Acknowledgement of above frames sent is received by sender

negative acknowledge resend 2 frame


4
Acknowledgement of above frames sent is received by sender

5
Acknowledgement of above frames sent is received by sender

7
Acknowledgement of above frames sent is received by sender

8
Acknowledgement of above frames sent is received by sender

negative acknowledge resend 6 frame


negative acknowledge resend 6 frame
9
Acknowledgement of above frames sent is received by sender

Variant Type II:


Void main( )
{
int i,j,noframes,x,x1=10,x2;
clrscr();
for(i=0;i<200;i++)

Department of E&CE, SVCE, Bengaluru Page 72


Computer Networks LAB (18ECL76) 2021-22

rand();
noframes=rand()/200;
i=1;j=1;
noframes=noframes/8;
printf(“\n Number of frames is %d”,noframes);
while(noframes>0)
{
printf(“\n Sending frames %d”,i);
strand(x1++);
x=rand()%10;
if(x%2==0)
{
for(x2=1;x2<2;x2++)
{
Printf(“Waiting for %d seconds \n”,x2);
sleep(x2);
}
printf(“\n sending frame %d”,i);

strand(x1++);
x=rand()%10;
}
printf(“\n ack for frame %d”.j);
noframes-=1;
i++;j++;
}
printf(“\n end of stop and wait protocol”);
}

Type III:
# stop and wait protocol in normal situation
# features : labeling, annotation, nam-graph, and window size monitoring
set ns [new Simulator]

set n0 [$ns node]


set n1 [$ns node]

$ns at 0.0 "$n0 label Sender"


$ns at 0.0 "$n1 label Receiver"

set nf [open stop.nam w]


$ns namtrace-all $nf
set f [open stop.tr w]
$ns trace-all $f

$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail


$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true


set tcp [new Agent/TCP]
Department of E&CE, SVCE, Bengaluru Page 73
Computer Networks LAB (18ECL76) 2021-22

$tcp set window_ 1


$tcp set maxcwnd_ 1
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]


$ns attach-agent $n1 $sink
$ns connect $tcp $sink

set ftp [new Application/FTP]


$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_

$ns at 0.1 "$ftp start"


$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"

$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet_0\""
$ns at 0.35 "$ns trace-annotate \"Receive Ack_0\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_1\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack_1\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_2\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack_2 \""
$ns at 1.43 "$ns trace-annotate \"Send Packet_3\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_3\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_4\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_4\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_5\""
$ns at 2.55 "$ns trace-annotate \"Receive Ack_5 \""
$ns at 2.75 "$ns trace-annotate \"Send Packet_6\""
$ns at 2.99 "$ns trace-annotate \"Receive Ack_6\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
proc finish {} {
global ns nf
$ns flush-trace
close $nf
puts "running nam..."
exec nam stop.nam &
exit 0
}
$ns run

OUTPUT:

Department of E&CE, SVCE, Bengaluru Page 74


Computer Networks LAB (18ECL76) 2021-22

Figure 16: Animator Window for the simple Protocol

5b. Sliding window protocol program

A sliding window protocol is a feature of packet-based data transmission protocols. Sliding


window protocols are used where reliable in-order delivery of packets is required, such as in
the Data Link Layer (OSI model) as well as in the Transmission Control Protocol.
Conceptually, each portion of the transmission (packets in most data link layers, but bytes in
TCP is assigned a unique consecutive sequence number, and the receiver uses the numbers to
place received packets in the correct order, discarding duplicate packets and identifying
missing ones. The problem with this is that there is no limit on the size of the sequence
number that can be required. By placing limits on the number of packets that can be
transmitted or received at any given time, a sliding window protocol allows an unlimited
number of packets to be communicated using fixed-size sequence numbers. The term
"window" on the transmitter side represents the logical boundary of the total number of
packets yet to be acknowledged by the receiver. The receiver informs the transmitter in each
acknowledgment packet the current maximum receiver buffer size (window boundary).

Variant Type I:

#include<stdio.h>

int main()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
Department of E&CE, SVCE, Bengaluru Page 75
Computer Networks LAB (18ECL76) 2021-22

scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}

Execution:

[root@localhost code]# gcc 5b.c


[root@localhost code]# ./a.out
Enter window size: 3

Enter number of frames to transmit: 5

Enter 5 frames: 12 20 87 65 4

With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)

After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver

12 20 87
Acknowledgement of above frames sent is received by sender

65 4
Acknowledgement of above frames sent is received by sender

Variant Type II:

#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

Department of E&CE, SVCE, Bengaluru Page 76


Computer Networks LAB (18ECL76) 2021-22

printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}

Variant Type III:

# sliding window mechanism with some features


# such as labeling, annotation, nam-graph, and window size monitoring
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"
set nf [open sliding.nam w]
$ns namtrace-all $nf
set f [open sliding.tr w]
$ns trace-all $f
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10
Agent/TCP set nam_tracevar_ true
set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_
$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""

Department of E&CE, SVCE, Bengaluru Page 77


Computer Networks LAB (18ECL76) 2021-22

$ns at 0.11 "$ns trace-annotate \"Send Packet_0,1,2,3\""


$ns at 0.34 "$ns trace-annotate \"Receive Ack_0,1,2,3\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_4,5,6,7\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack_4,5,6,7\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_8,9,10,11\""

$ns at 1.23 "$ns trace-annotate \"Receive Ack_8,9,10,11 \""


$ns at 1.43 "$ns trace-annotate \"Send Packet_12,13,14,15\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_12,13,14,15\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_16,17,18,19\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_16,17,18,19\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_20,21,22,23\""
$ns at 2.56 "$ns trace-annotate \"Receive Ack_24,25,26,27\""
$ns at 2.76 "$ns trace-annotate \"Send Packet_28,29,30,31\""
$ns at 3.00 "$ns trace-annotate \"Receive Ack_28\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
proc finish {} {
global ns
$ns flush-trace
# close $nf
puts "running nam..."
exec nam sliding.nam &
exit 0
}
$ns run

OUTPUT:

Figure 17: Sliding window Protocol

Department of E&CE, SVCE, Bengaluru Page 78


Computer Networks LAB (18ECL76) 2021-22

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

Theory:
The leaky-bucket implementation is used to control the rate at which traffic is sent to the
network. A leaky bucket provides a mechanism by which bursty traffic can be shaped to
present a steady stream of traffic to the network, as opposed to traffic with erratic bursts of
low-volume and high-volume flows.
Traffic analogy an appropriate analogy for the leaky bucket is a scenario in which four lanes
of automobile traffic converge into a single lane. A regulated admission interval into the
single lane of traffic flow helps the traffic move. The benefit of this approach is that traffic
flow into the major arteries (the network) is predictable and controlled. The major liability is
that when the volume of traffic is vastly greater than the bucket size, in conjunction with the
drainage-time interval, traffic backs up in the bucket beyond bucket capacity and is
discarded. In Leaky bucket, each host is connected to the network by an interface containing
a leaky bucket, that is, a finite internal queue. If a packet arrives at the queue when it is full,
the packet is discarded.

Figure 18: Leaky bucket Algorithm

Variant Type I:
#include<stdio.h>
#include<stdlib.h>
int bucket_size;
void bucket_input ( int pkt_sz, int op_rt )
{
if( pkt_sz > bucket_size )

Department of E&CE, SVCE, Bengaluru Page 79


Computer Networks LAB (18ECL76) 2021-22

printf(" \n\nBucket overflow\n ");


else
{
sleep(1);
while ( pkt_sz > op_rt )
{
printf(" \n %d bytes outputted ", op_rt );
pkt_sz-= op_rt;
sleep(1);
}
if ( pkt_sz > 0 )
printf(" \nLast %d bytes sent\n", pkt_sz );
printf(" \n Bucket output successful \n" );
}
}
int main()
{
int i, op_rate, packet_size;
printf("\n Enter Bucket Size: " );
scanf( "%d", &bucket_size );
printf(" \n Enter output rate: " );
scanf( "%d", &op_rate );
for( i=1; i<=5; i++ )
{
sleep(1);
packet_size = random()%1000;
printf(" \n Packet number [%d] \t Packet size = %d ", i, packet_size );
bucket_input( packet_size, op_rate );
}
return 0;
}
Execution:
[root@localhost code]# gcc 6.c
[root@localhost code]# ./a.out

Enter Bucket Size: 500

Enter output rate: 80

Packet number [1] Packet size = 383


80 bytes outputted
80 bytes outputted
80 bytes outputted
80 bytes outputted
Last 63 bytes sent

Bucket output successful

Department of E&CE, SVCE, Bengaluru Page 80


Computer Networks LAB (18ECL76) 2021-22

Packet number [2] Packet size = 886

Bucket overflow

Packet number [3] Packet size = 777

Bucket overflow

Packet number [4] Packet size = 915

Bucket overflow

Packet number [5] Packet size = 793

Bucket overflow

Variant Type II:

#include<stdio.h>
#include<stdlib.h>
int trand(int a)
{
int rn;
rn=random()%10;
rn=rn%a;
if(rn==0)
rn=1;
return(rn);
}
int main()
{
int i,packet[5],psz,bsz,pszrn=0,clk,ptime,premain,orate,flag=0;
for(i=0;i<5;i++)
packet[i]=trand(6)*10;
printf("\nenter the o/p rate");
scanf("%d",&orate);
printf("\nenter the bucket size");
scanf("%d",&bsz);
for(i=0;i<5;i++)
{
if((packet[i]+pszrn)>bsz)
printf("\n incoming packet size %d is greater than bucket size_reject",packet[i]);
else
{
for(;;)

Department of E&CE, SVCE, Bengaluru Page 81


Computer Networks LAB (18ECL76) 2021-22

{
premain=4-i;
psz=packet[i];
pszrn+=psz;
printf("\nincoming packet size is %d",psz);
printf("\n no.of packets waiting for transmission= %d",pszrn);
ptime=trand(4)*10;
printf("\nnext packet will come at %d",ptime);
for(clk=0;clk<=ptime;clk++)
{
printf("\ntime left =%d",ptime-clk);
sleep(1);
if(pszrn)
{
if(pszrn==0)
printf("\nbucket is empty");
else
{
if(pszrn>=orate)
printf("%d bytes are tranmited",orate);
else
printf("\n %d bytes are transmitted",pszrn);
}
if(pszrn<=orate)
pszrn=0;
else
pszrn-=orate;
printf("\nbytes remaining %d ",pszrn);
}
else
printf("\nbytes remaing %d ",pszrn);
}
if(pszrn!=0)
flag=1;
break;
}
}
}
printf("\n\n");
return 0;
}

Output:
enter the o/p rate10
enter the bucket size5
incoming packet size 30 is greater than bucket size_reject
incoming packet size 10 is greater than bucket size_reject

Department of E&CE, SVCE, Bengaluru Page 82


Computer Networks LAB (18ECL76) 2021-22

incoming packet size 10 is greater than bucket size_reject


incoming packet size 50 is greater than bucket size_reject
incoming packet size 30 is greater than bucket size_reject

Variant Type III:


include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do
{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}

while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
for(i=0;count || i<nsec;i++)
{
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
count=cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
Department of E&CE, SVCE, Bengaluru Page 83
Computer Networks LAB (18ECL76) 2021-22

}
else
{
drop=0;
count=0;
}
printf(" \t %d \t %d \n",drop,count);
}
return 0;
}

Output: Enter The Bucket Size


5
Enter The Operation Rate
2
Enter The No. Of Seconds You Want To Stimulate
3
Enter The Size Of The Packet Entering At 1 sec
5
Enter The Size Of The Packet Entering At 1 sec
4
Enter The Size Of The Packet Entering At 1 sec
3

Department of E&CE, SVCE, Bengaluru Page 84


Computer Networks LAB (18ECL76) 2021-22

Additional Programs

Experiment No: 1
TRANSMISSION OF PING MESSAGE

Aim: Simulate the transmission of ping messages over a network topology


consisting of 6 nodes and find the number of packets dropped due to congestion.
set nf [ open lab4.nam w ]
$ns namtrace-all $nf

set tf [ open lab4.tr w ]


$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$ns duplex-link $n0 $n4 1005Mb 1ms DropTail


$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p1 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001

set p2 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n1 $p2

set p3 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n2 $p3
$p3 set packetSize_ 30000
$p3 set interval_ 0.00001

set p4 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n3 $p4

set p5 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n5 $p5
$ns queue-limit $n0 $n4 5
$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2

Department of E&CE, SVCE, Bengaluru Page 85


Computer Networks LAB (18ECL76) 2021-22

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id]received answer from $from with round trip time $rtt msec"
}
# please provide space between $node_ and id. No space between $ and from. No space
between and $ and rtt */

$ns connect $p1 $p5


$ns connect $p3 $p4

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab4.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
$ns at 2.1 "$p1 send"
$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"

Department of E&CE, SVCE, Bengaluru Page 86


Computer Networks LAB (18ECL76) 2021-22

$ns at 0.3 "$p3 send"


$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"

$ns at 0.9 "$p3 send"


$ns at 1.0 "$p3 send"
$ns at 1.1 "$p3 send"
$ns at 1.2 "$p3 send"
$ns at 1.3 "$p3 send"
$ns at 1.4 "$p3 send"
$ns at 1.5 "$p3 send"
$ns at 1.6 "$p3 send"
$ns at 1.7 "$p3 send"
$ns at 1.8 "$p3 send"
$ns at 1.9 "$p3 send"
$ns at 2.0 "$p3 send"
$ns at 2.1 "$p3 send"
$ns at 2.2 "$p3 send"
$ns at 2.3 "$p3 send"
$ns at 2.4 "$p3 send"
$ns at 2.5 "$p3 send"
$ns at 2.6 "$p3 send"
$ns at 2.7 "$p3 send"
$ns at 2.8 "$p3 send"
$ns at 2.9 "$p3 send"

$ns at 3.0 "finish"


$ns run

AWK file: (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)

BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}

END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);

Department of E&CE, SVCE, Bengaluru Page 87


Computer Networks LAB (18ECL76) 2021-22

Experiment No: 2
Client-server socket programming

Aim: Using TCP/IP Sockets, write a client-server program to make client


sending the file name and the server to send back the contents of the requested
file if present.
Sockets are a protocol independent method of creating a connection between processes.
Sockets can be either
➢ Connection based or connectionless: Is a connection established before
communication or does each packet describe the destination?
➢ Packet based or streams based: Are there message boundaries or is it one stream?
➢ Reliable or unreliable: Can messages be lost, duplicated, reordered, or corrupted?

Socket characteristics
Sockets are characterized by their domain, type and transport protocol. Common domains
are:
➢ AF_UNIX: address format is UNIX pathname
➢ AF_INET: address format is host and port number

Common types are:


➢ virtual circuit: received in order transmitted and reliably
➢ datagram: arbitrary order, unreliable

Use of sockets:
➢ Connection–based sockets communicate client-server: the server waits for a
connection from the client
➢ Connectionless sockets are peer-to-peer: each process is symmetric.

Department of E&CE, SVCE, Bengaluru Page 88


Computer Networks LAB (18ECL76) 2021-22

socket API
#include<sys/types.h>
#include<sys /socket.h>

Sockaddr :

For the internet family:


struct sockaddr_in {
sa_family_t sin_family; // = AF INET
in_port_t sin_port; // is a port number
struct in_addr sin_addr; // an IP address
}

listen
#include <sys / types.h>
#include <sys / socket.h>
int listen (int sid, int size) ;
Where size it the number of pending connection requests allowed (typically limited by Unix
kernels to 5). Returns the 0 on success, or -1 if failure.

accept
#include <sys / types.h>
#include <sys / socket.h>
int accept(int sid ,struct sockaddr *addrPtr , int *lenPtr )
Returns the socketId and address of client connecting to socket.

if lenPtr or addrPtr equal zero, no address structure is returned.


lenPtr is the maximum size of address structure that can be called, returns the actual value.
Waits for an incoming request, and when received creates a socket for it.

send
#include <sys / types.h>
#include <sys / socket.h>
int send(int sid ,const char *bufferPtr ,int len ,int flag)
Send a message. Returns the number of bytes sent or -1 if failure.
(Must be a bound socket).

flag is either
« 0: default
« MSG OOB: Out-of-band high priority communication
recv
#include <sys / types.h>
#include <sys / socket.h>
int recv ( int sid , char *bufferPtr , int len , int flags)
Receive up to len bytes in bufferPtr. Returns the number of bytes received or -1 on failure.
flags can be either

Shutdown:
include <sys / types.h>
#include <sys / socket.h>
Department of E&CE, SVCE, Bengaluru Page 89
Computer Networks LAB (18ECL76) 2021-22

int shutdown ( int sid , int how)


Disables sending (how=1 or how=2) or receiving (how=0 or how=2). Returns -1 on failure.
Connect
-this is the first of the client calls
#include <sys / types.h>

Source Code:
Client Side:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#define SERV_TCP_PORT 6880
#define SERV_HOST_ADDR "127.0.0.1"
int main()
{
int sockfd;
struct sockaddr_in serv_addr,cli_addr;
char filename[100],buf[1000];
int n;
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
serv_addr.sin_port=htons(SERV_TCP_PORT);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("Client:cant open stream socket\n");
else
printf("Client:stream socket opened successfully\n");
if(connect(sockfd,(struct sockaddr *)&serv_addr,
sizeof(serv_addr))<0)
printf("Client:cant connect to server\n");
else
printf("Client:connected to server successfully\n");
printf("\n Enter the file name to be displayed :");
scanf("%s",filename);
write(sockfd,filename,strlen(filename));
printf("\n filename transfered to server\n");
n=read(sockfd,buf,1000);
if(n < 0)
printf("\n error reading from socket");
printf("\n Client : Displaying file content of %s\n",filename);
fputs(buf,stdout);
close(sockfd);
exit(0);

Department of E&CE, SVCE, Bengaluru Page 90


Computer Networks LAB (18ECL76) 2021-22

AT CLIENT SIDE
[root@localhost ]# cc tcpc.c
[root@localhost ]# ./a.out
Data Sent
File Content....
Sockets are a mechanism for exchanging data between processes. These processes can
either be on the same machine, or on different machines connected via a network. Once a socket
connection is established, data can be sent in both directions until one of the endpoints closes the
connection.
I needed to use sockets for a project I was working on, so I developed and refined a few
C++ classes to encapsulate the raw socket API calls. Generally, the application requesting the
data is called the client, and the application servicing the request is called the server. I created two
primary classes, ClientSocket and ServerSocket, that the client and server could use to exchange
data.
SERVER SIDE:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#define SERV_TCP_PORT 6880
#define SERV_HOST_ADDR "127.0.0.1"
int main()
{ int sockfd,newsockfd,clilen;
struct sockaddr_in cli_addr,serv_addr;
char filename[25],buf[1000];
int n,m=0;
int fd;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("server:cant open stream socket\n");
else
printf("server:stream socket opened successfully\n");
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

Department of E&CE, SVCE, Bengaluru Page 91


Computer Networks LAB (18ECL76) 2021-22

serv_addr.sin_port=htons(SERV_TCP_PORT);
if((bind(sockfd,(struct sockaddr *)
&serv_addr,sizeof(serv_addr)))<0)
printf("server:cant bind local address\n");
else
printf("server:bound to local address\n");
listen(sockfd,5);
printf("\n SERVER : Waiting for client...\n");
for(;;)
{
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if(newsockfd<0)
printf("server:accept error\n");
else
printf("server:accepted\n");
n=read(newsockfd,filename,25);
filename[n]='\0';
printf("\n SERVER : %s is found and ready to transfer
\n",filename);
fd=open(filename,O_RDONLY);
n=read(fd,buf,1000);
buf[n]='\0';
write(newsockfd,buf,n);
printf("\n transfer success\n");
close(newsockfd);
exit(0)
}
}
Output:
[root@localhost ]# cc tcps.c
[root@localhost ]# ./a.out
Received the file name : data.txt
File content sent.

Department of E&CE, SVCE, Bengaluru Page 92


Computer Networks LAB (18ECL76) 2021-22

Experiment No: 3
Client-Server Communication Using Message Queues or FIFO

Aim: C Program for CLIENT SERVER communication using message Queues or FIFOs as IPC
channels that client sends the file name and the server to send back the contents of the requested
file if present.
Source Code:
#SERVER*/
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
char filename[100],buf[300],buf1[300];
int num,num2,n,filesize,fl,fd,fd2;
mknod("fifo1",S_IFIFO | 0666,0);
mknod("fifo2",S_IFIFO | 0666,0);
printf("\n Server Online\n");
fd=open("fifo1",O_RDONLY);
printf("Client Online! Waiting for request...\n\n");
while(1)
{
num = read(fd,filename,100);
Department of E&CE, SVCE, Bengaluru Page 93
Computer Networks LAB (18ECL76) 2021-22

filename[num]='\0';
fl=open(filename,O_RDONLY);
printf("\n Sever: %s is found!\n transferring the contents\n",filename);
filesize=lseek(fl,0,2);
printf("\n File size is %d\n",filesize);
lseek(fl,0,0);
n=read(fl,buf1,filesize);
buf1[n]='\0';
fd2=open("fifo2",O_WRONLY);
write(fd2,buf1,strlen(buf1));
printf("\n SERVER :Transfer completed\n");
exit(1);
}
unlink("fifo1");
unlink("fifo2");
}
Output:
AT SERVER SIDE
[root@localhost ]# cc prg6s.c
[root@localhost ]# ./a.out
SERVER online!
CLIENT online!

Department of E&CE, SVCE, Bengaluru Page 94

You might also like