CN 7-10 Theory

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

EXPERIMENT NO.

-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:

1. Weight on the Shortest path between source and destination.


Conclusion:

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

A checksum of a message is an arithmetic sum of message code words of a certain word


length, for example byte values, and their carry value The sum is negated by means of ones-
complement, and stored or transferred as an extra code word extending the message

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:

A wants to transmit: 1001


A computes parity bit value: 1^0^0^1 = 0
A adds parity bit and sends: 10010
B receives: 10010
B computes parity: 1^0^0^1^0 = 0
B reports correct transmission after observing expected even result

Transmission sent using odd parity:

A wants to transmit: 1001


A computes parity bit value: ~(1^0^0^1) = 1
A adds parity bit and sends: 10011
B receives: 10011
B computes overall parity: 1^0^0^1^1 = 1
B reports correct transmission after observing expected odd result

Transmission sent using even parity:

A wants to transmit: 1001


A computes parity bit value: 1^0^0^1 = 0
A adds parity bit and sends: 10010
*** TRANSMISSION ERROR ***
B receives: 11010
B computes overall parity: 1^1^0^1^0 = 1
B reports incorrect transmission after observing unexpected odd result

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:

 Extreme error detection capabilities.


 Little overhead.
 Ease of implementation.

The following section will explain the CRC more.

How the CRC algo works

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.

For the formal explanation we will define the foll:

M- The original frame to be transmitted, before adding the FCS. It is k-bits long.

F-The resulting FCS to be added to M. It is n bits long.


T-The cascading of M and F. This is the resulting frame that will be transmitted. It is k+n bits long.

P- The pre-defined CRC Polynomial. A pattern of n+1 bits.

CRC Example

Assume that:

M= 1010001101 (k=10) and’

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)

Then the transmitted frame will be:

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.

CRC creation process:

1. Get the raw frame.


2. Left shift the raw frame by n bits and the divide it by P.
3. The remainder of the last action is the FCS.
4. Append the FCS to the raw frame. The result is the frame to transmit.

CRC check process:


1. Receive the frame.
2. Divide it by P.
3. Check the remainder. If not zero then there is an error in the frame.

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

1. Number the bits starting from 1: bit 1, 2, 3, 4, 5, etc


2. Write the bit numbers in binary 1, 10, 11, 100, 101, etc
3. All bit positions that are powers of two (have only one 1 bit in the binary form of their
position) are parity bits
4. All other bit positions, with two or more 1 bits in the binary form of their position, are data
bits
5. Each data bit is included in a unique set of 2 or more parity bits, as determined by the
binary form of its bit position
1. Parity bit 1 covers all bit positions which have the least significant bit set: bit 1 (the
parity bit itself), 3, 5, 7, 9, etc
2. Parity bit 2 covers all bit positions which have the second least significant bit set: bit
2 (the parity bit itself), 3, 6, 7, 10, 11, etc
3. Parity bit 4 covers all bit positions which have the third least significant bit set: bits
4–7, 12–15, 20–23, etc
4. Parity bit 8 covers all bit positions which have the fourth least significant bit set: bits
8–15, 24–31, 40–47, etc
5. In general each parity bit covers all bits where the binary AND of the parity position
and the bit position is non-zero

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

Thus the message after four bit appended is: 1 1 0 1 0 1 1 0 1 1 0 0 0 0

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.

Mechanisms for Flow Control:


 Stop and Wait Protocol: This is the simplest file control protocol in which the sender
transmits a frame and then waits for an acknowledgement, either positive or negative, from
the receiver before proceeding. If a positive acknowledgement is received, the sender
transmits the next packet; else it retransmits the same frame. However, this protocol has
one major flaw in it. If a packet or an acknowledgement is completely destroyed in transit
due to a noise burst, a deadlock will occur because the sender cannot proceed until it
receives an acknowledgement. This problem may be solved using timers on the sender's
side. When the frame is transmitted, the timer is set. If there is no response from the
receiver within a certain time interval, the timer goes off and the frame may be
retransmitted.
 Sliding Window Protocols: Inspite of the use of timers, the stop and wait protocol still
suffers from a few drawbacks. Firstly, if the receiver had the capacity to accept more than
one frame, its resources are being underutilized. Secondly, if the receiver was busy and did
not wish to receive any more packets, it may delay the acknowledgement. However, the
timer on the sender's side may go off and cause an unnecessary retransmission. These
drawbacks are overcome by the sliding window protocols.
In sliding window protocols the sender's data link layer maintains a 'sending window' which
consists of a set of sequence numbers corresponding to the frames it is permitted to send.
Similarly, the receiver maintains a 'receiving window' corresponding to the set of frames it is
permitted to accept. The window size is dependent on the retransmission policy and it may
differ in values for the receiver's and the sender's window. The sequence numbers within
the sender's window represent the frames sent but as yet not acknowledged. Whenever a
new packet arrives from the network layer, the upper edge of the window is advanced by
one. When an acknowledgement arrives from the receiver the lower edge is advanced by
one. The receiver's window corresponds to the frames that the receiver's data link layer
may accept. When a frame with sequence number equal to the lower edge of the window is
received, it is passed to the network layer, an acknowledgement is generated and the
window is rotated by one. If however, a frame falling outside the window is received, the
receiver's data link layer has two options. It may either discard this frame and all
subsequent frames until the desired frame is received or it may accept these frames and
buffer them until the appropriate frame is received and then pass the frames to the
network layer in sequence.

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 :

Write a program to implement a client-server socket programming.

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

Definition: A socket is one endpoint of a two-way communication link between two


programs running on the network A socket is bound to a port number so that the TCP layer
can identify the application that data is destined to be sent

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:

echoSocket = new Socket("taranis", 7);


out = new PrintWriter(echoSocket getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket getInputStream()));
The first statement in this sequence creates a new Socket object and names it echoSocket
The Socket constructor used here requires the name of the machine and the port number
to which you want to connect The example program uses the host name taranis This is the
name of a hypothetical machine on our local network When you type in and run this
program on your machine, change the host name to the name of a machine on your
network Make sure that the name you use is the fully qualified IP name of the machine to
which you want to connect The second argument is the port number Port number 7 is the
port on which the Echo server listens

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.

The following are the socket primitives for TCP/IP:

Primitive Meaning

Socket Create a new communication end point.

Bind Attach a local address to a socket.

Listen Announce willingness to accept connections.

Accept Block caller until a connection request arrives.

Connect Actively attempt to establish a connection.


Send Send some data over the connection.

Receive Receive some data over the connection

Close Release the connection.

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.

socke bind listen accep read write close

socke connect write read close

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.

You might also like