CN 7-10 Theory
CN 7-10 Theory
CN 7-10 Theory
-7
AIM
Write a program to find the shortest path between any two routers in a given subnet using
Dijkstra’s shortest path routing algorithm.
Theory:
The main idea in shortest path routing is to build a graph of the subnet with each node
of the graph representing a router and each edge representing a communication line (often
called a link). To choose a route between a given pair of routers, the algorithm just finds the
shortest path between them on the graph.
One-way of measuring path length is the number of hops. Another metric is the
geographic distance in kilometers. With this graph labeling, the shortest path is the fastest
path rather than the path with the fewest arcs or kilometers.
In the general case, the labels on the arcs could be average computed as a function of
the distance, bandwidth, average traffic, communication cost, mean queue length, measured
delay, and other factors. By changing the weighting function, the algorithm would then
compute the “shortest” path measured according to any one of a number of criteria or to a
combination of criteria.
Each node is labeled with its distance from the source node along the best-known
path. Initially, no paths are known, so all nodes are labeled 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 labels are tentative. When it is discovered that a
label represents the shortest possible path from the source to that node, it is made permanent
and never changed thereafter.
Input Specification:
1. Number of routers in the subnet.
2. Number of communication lines connecting different routers.
3. Weight (i.e. distance) of each communication line.
4. Source router and Destination router
Output Specification:
Dijkstra’s algorithm is used to find shortest path between the source and destination router.
This algorithm is static algorithm and do not take current network load into account. Also, this
algorithm is not robust to failure of even one of the links.
EXPERIMENT NO.-8
AIM :
Write a program to implement error-detecting code i.e. Cyclic Redundancy
Check.
Theory:
Error detection is the ability to detect the presence of errors caused by noise or other
impairments during transmission from the transmitter to the receiver
Error correction is the additional ability to reconstruct the original, error-free data
Error detection codes (which include all error-detection-and-correction codes) transmit
more bits than were in the original data Most codes are "systematic" i e the transmitter
sends a fixed number of original data bits, followed by fixed number of check bits (usually
referred to as redundancy) which are derived from the data bits by some deterministic
algorithm The receiver applies the same algorithm to the received data bits and compares
its output to the received check bits; if the values do not match, an error has occurred at
some point during the transmission
A] CHECKSUM:
The goal of checksum algorithms is to detect accidental modification such as corruption to stored
data or errors in a communication channel
On the receiver side, a new checksum may be calculated from the extended message If the
new checksum is not 0, an error has been detected
Checksum schemes include parity bits, check digits, and longitudinal redundancy check
Parity bit:
A parity bit is a bit that is added to ensure that the number of bits with value of one in a
given set of bits is always even or odd Parity bits are used as the simplest error detecting
code
As for binary digits, there are two variants of parity bits: even parity bit and odd parity bit
An even parity bit is set to 1 if the number of ones in a given set of bits is odd (making the
total number of ones, including the parity bit, even) An odd parity bit is set to 1 if the
number of ones in a given set of bits is even (making the total number of ones, including the
parity bit, odd) Parity bit is an error detecting code, but is not an error correcting code as
there is no way to determine which particular bit is corrupted The data must be discarded
entirely, and re-transmitted from scratch
Example:
Transmission sent using even parity:
B] CRC:
The CRC is a very powerful but easily implemented technique to obtain data reliability. The CRC
technique is used to protect blocks of data called frames. Using this technique, the transmitter
appends an extra n-bit sequence to every frame called Frame check sequence (FCS). The FCS holds
redundant information about the frame that helps the transmitter detect error in frame. The CRC is
one of the most used techniques for error detection in data communication. The technique gained
its popularity because it combines three advantages:
Given the original frame, the transmitter generates the FCS for that frame. The FCS is generated so
that the resulting frame (the cascade of original frame and the FCS), is exactly devisable by some
pre-defined polynomial. This pre-defined polynomial is called the devisor or CRC polynomial.
M- The original frame to be transmitted, before adding the FCS. It is k-bits long.
CRC Example
Assume that:
P=110101 (n+1=6)
Then the FCS to be calculated by the transmitter will be n=5 bits in length. Let’s assume that the
transmitter has calculated the FCS to be:
F=1110 (n=5)
T=10100011011110
The main idea behind the CRC algo is that the FCS is generated so that the remainder of T\P is zero.
It’s clear that
1) T=M*x^n+F
This is because by cascading F to M we have shifted T by n bits to the left and then added F
to the result. We want the transmitted frame, T, to be exactly divisible by the pre-defined
polynomial P, so we would have to find a suitable frame check sequence(F) for every raw
message(M).
Suppose we divided only M*x^n by P, we would get:
2) M*x^n/P= Q+R\P
3) There is a quotient and a remainder. We will use this remainder, as our FCS (F). Returning to
Eq.1:
4) T/P= (M*x^n+R)/p=M*x^n/P+R/P=Q+R/P+R/P=Q+(R+R)/P
But any binary number added to itself in a modulo 2 field yields zero so:
5) T/P=Q, with no remainder.
Sample Input: Input should be in polynomial form. Both generator polynomial and frame polynomial
should be given.
Sample Output: The algorithm should generate the polynomial for the frame to be
transmitted after CRC checksum has been added.
C] HAMMING CODE:
A Hamming code is a linear error-correcting code named after its inventor, Richard
Hamming Hamming codes can detect up to two simultaneous bit errors, and correct single-
bit errors; thus, reliable communication is possible when the Hamming distance between the
transmitted and received bit patterns is less than or equal to one By contrast, the simple
parity code cannot correct errors, and can only detect an odd number of errors
General algorithm
If more error-correcting bits are included with a message, and if those bits can be arranged
such that different incorrect bits produce different error results, then bad bits could be
identified In a 7-bit message, there are seven possible single bit errors, so three error control
bits could potentially specify not only that an error occurred but also which bit caused the
error
Following general algorithm generates a single-error correcting (SEC) code for any number
of bits
To check for errors, check all of the parity bits The pattern of errors, called the error
syndrome, identifies the bit in error If all parity bits are correct, there is no error Otherwise,
the sum of the positions of the erroneous parity bits identifies the erroneous bit For example,
if the parity bits in positions 1, 2 and 8 indicate an error, then bit 1+2+8=11 is in error If
only one parity bit indicates an error, the parity bit itself is in error
Cyclic Redundancy Check (CRC) is a technique used to detect accidental data transmission
errors. Using CRC error checking, the sending device performs a calculation on each block of
data before it is sent. The calculation generates a number based on the data to be
transmitted. The CRC calculation result is then appended to the end of the data block as an
extra n-bit sequence, which is then sent together with the data to the receiving device.
The receiving device repeats the same calculation on the data block after it is received. If
the result of the receiving device’s CRC calculation matches the result appended to the
block by the transmitting device, it can be assumed that the transmission was error-free
and that no data had been accidentally changed or deleted during transmission.
Note that CRC can only be used to detect accidental errors in data transmission; it is not
able to detect intentional changes, which require the use of cryptographic hash algorithms.
(To understand how we can detect error in the received bit stream using cyclic
redundancy check.)
Error detecting and correcting codes are widely used on wireless links, which are noisy and
error prone when compared to copper wire or optical fibers. Over copper wire or fiber, the error
rate is much lower, so error detection and retransmission is usually more efficient there for dealing
with the occasional error.
The mostly used method for error-detection is polynomial code, also known as CRC (Cyclic
Redundancy Check). Polynomial codes are based upon treating bit strings 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 xk-1 to x0.
Such a polynomial is said to be of degree k – 1. The high-order (leftmost) 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: x 5 + x4 + x0. Polynomial arithmetic is done
modulo 2, according to the rules of algebraic field theory. There are no carries for addition or
borrows for subtraction. Both addition and subtractions are identical to exclusive OR.
When the polynomial code method is employed, the sender and receiver must agree
upon a generator polynomial, G(x), in advance. Both the high and low order bits of the
generator must be 1. 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 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 remainder, there has been a transmission error.
Algorithm
1. Let r be the degree of G(x). Append r zero bits to the low-order end of the frame so it now
contains m + r bits and corresponds to the polynomial xr M(x).
2. Divide the bit string corresponding to G(x) into the bit string corresponding to xr M(x), using
module 2 division.
3. Subtract the remainder from the bit string corresponding to xr M(x) using modulo 2 subtractions.
The result is the check summed frame to be transmitted. Call its polynomial T(x).
Calculation for frame 1101011011 using generator frame G(x)=x4 + x+1 is as follows
Frame 1101011011
Generator : 1 0 0 1 1
10011((11010110110000)) 1100001010
10011
10011
10011
00001
00000
00010
00000
00101
00000
01011
00000
10110
10011
01010
00000
10100
10011
01110
00000
1110(remainder)
Upon receiving the checksummed frame, the receiver divides it by G(x); that is it computes [T(x) +
E(x)]/G(x) is 0, so the result of the computation is simply E(x)/G(x).
Conclusion:
CRC code is used to detect error in the transmitted frame at the data link layer. CRC can’t
correct error. Thus, after detecting the error (if any) at the receiver’s side sender should retransmit
that particular frame.
Program:
EXPERIMENT NO.- 9
AIM :
To study and Write a program to implement Sliding window protocol for flow control.
Learning Objective:
To understand how this sliding window protocol work to control the flow of data between
two computers.
Theory:
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(overflow). This problem may be solved by introducing
flow control. Most flow control protocols contain a feedback mechanism to inform the senderwhen
it should transmit the next frame.
In this simple example, there is a 4-byte sliding window. Moving from left to right, the
window "slides" as bytes in the stream are sent and acknowledged.
Most sliding window protocols also employ ARQ ( Automatic Repeat reQuest ) mechanism.
In ARQ, the sender waits for a positive acknowledgement before proceeding to the next
frame. If no acknowledgement is received within a certain time interval it retransmits the
frame. ARQ is of two types :
1. Go Back 'n': If a frame is lost or received in error, the receiver may simply discard all
subsequent frames, sending no acknowledgments for the discarded frames. In this case the receive
window is of size 1. Since no acknowledgements are being received the sender's window will fill up,
the sender will eventually time out and retransmit all the unacknowledged frames in order starting
from the damaged or lost frame. The maximum window size for this protocol can be obtained as
follows. Assume that the window size of the sender is n. So the window will initially contain the
frames with sequence numbers from 0 to (w-1). Consider that the sender transmits all these
frames and the receiver's data link layer receives all of them correctly. However, the sender's data
link layer does not receive any acknowledgements as all of them are lost. So the sender will
retransmit all the frames after its timer goes off. However the receiver window has already
advanced to w. Hence to avoid overlap , the sum of the two windows should be less than the
sequence number space.
w-1 + 1 < Sequence Number Space
i.e., w < Sequence Number Space
Maximum Window Size = Sequence Number Space - 1
2. Selective Repeat :In this protocol rather than discard all the subsequent frames following a
damaged or lost frame, the receiver's data link layer simply stores them in buffers. When the
sender does not receive an acknowledgement for the first frame it's timer goes off after a certain
time interval and it retransmits only the lost frame. Assuming error - free transmission this time,
the sender's data link layer will have a sequence of a many correct frames which it can hand over
to the network layer. Thus there is less overhead in retransmission than in the case of Go Back n
protocol.
In case of selective repeat protocol the window size may be calculated as follows. Assume that the
size of both the sender's and the receiver's window is w. So initially both of them contain the
values 0 to (w-1). Consider that sender's data link layer transmits all the w frames, the receiver's
data link layer receives them correctly and sends acknowledgements for each of them. However,
all the acknowledgemnets are lost and the sender does not advance it's window. The receiver
window at this point contains the values w to (2w-1). To avoid overlap when the sender's data link
layer retransmits, we must have the sum of these two windows less than sequence number space.
Hence, we get the condition
Maximum Window Size = Sequence Number Space / 2
Conclusion:
Thus the program for sliding window is done and understood how this sliding window is
used to control the flow f data between two hosts.
EXPERIMENT NO.-10
AIM :
Learning Objective:
To understand how two processes communicate with each other and what are the system
calls needed for the communication.
Theory:
Sockets are interfaces that can "plug into" each other over a network Once so "plugged in", the
programs so connected communicate
Normally, a server runs on a specific computer and has a socket that is bound to a specific port
number The server just waits, listening to the socket for a client to make a connection request
On the client-side: The client knows the hostname of the machine on which the server is
running and the port number on which the server is listening To make a connection
request, the client tries to rendezvous with the server on the server's machine and port
The client also needs to identify itself to the server so it binds to a local port number that it
will use during this connection This is usually assigned by the system
If everything goes well, the server accepts the connection Upon acceptance, the server
gets a new socket bound to the same local port and also has its remote endpoint set to the
address and port of the client It needs a new socket so that it can continue to listen to the
original socket for connection requests while tending to the needs of the connected client
On the client side, if the connection is accepted, a socket is successfully created and the
client can use the socket to communicate with the server
The client and server can now communicate by writing to or reading from their sockets
An endpoint is a combination of an IP address and a port number Every TCP connection can
be uniquely identified by its two endpoints That way you can have multiple connections
between your host and the server
The java net package in the Java platform provides a class, Socket, that implements one
side of a two-way connection between your Java program and another program on the
network The Socket class sits on top of a platform-dependent implementation, hiding the
details of any particular system from your Java program By using the java net Socket class
instead of relying on native code, your Java programs can communicate over the network in
a platform-independent fashion
Additionally, java net includes the ServerSocket class, which implements a socket that
servers can use to listen for and accept connections to clients
1. Open a socket
2. Open an input stream and output stream to the socket
3. Read from and write to the stream according to the server's protocol
4. Close the streams
5. Close the socket
These lines establish the socket connection between the client and the server and open a
PrintWriter and a BufferedReader on the socket:
The second statement gets the socket's output stream and opens a PrintWriter on it
Similarly, the third statement gets the socket's input stream and opens a BufferedReader
on it The example uses readers and writers so that it can write Unicode characters over the
socket
To send data through the socket to the server, EchoClient simply needs to write to the
PrintWriter To get the server's response, EchoClient reads from the BufferedReader
A socket is a communication endpoint to which an application can write data that are to be
send out over the underlying network, and from which incoming data can be read. A socket forms
an abstraction over the actual communication endpoint that is used by the local operating system
for a specific transport protocol.
Primitive Meaning
Servers generally execute the first four primitives, normally in the order given. When calling the
socket primitive, the caller creates a new communication endpoint for a specific transport protocol.
The bind primitive associates a local address with the newly created socket.
Conclusion
Once established, sockets can be used like low-level file descriptors, providing two-way data
communication. Though bare UNIX system calls are used here, it doesn’t show the platform
independence.